Talha812 commited on
Commit
79bdf4c
Β·
verified Β·
1 Parent(s): c980543

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import whisper
3
+ import gradio as gr
4
+ from groq import Groq
5
+ from TTS.api import TTS
6
+
7
+ # βœ… Set up Groq API Key
8
+ os.environ["GROQ_API_KEY"] = "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"
9
+
10
+ # βœ… Load OpenAI Whisper model
11
+ whisper_model = whisper.load_model("base")
12
+
13
+ # βœ… Function to Transcribe Speech to Text
14
+ def transcribe_audio(audio):
15
+ print("πŸ“ Transcribing...")
16
+ result = whisper_model.transcribe(audio)
17
+ return result["text"]
18
+
19
+ # βœ… Function to Get Response from Groq API
20
+ def get_groq_response(prompt):
21
+ print("πŸ€– Generating Response...")
22
+ client = Groq(api_key=os.environ["GROQ_API_KEY"])
23
+ chat_completion = client.chat.completions.create(
24
+ messages=[{"role": "user", "content": prompt}],
25
+ model="llama-3.3-70b-versatile"
26
+ )
27
+ return chat_completion.choices[0].message.content
28
+
29
+ # βœ… Function to Convert Text to Speech using Coqui TTS
30
+ def text_to_speech(text):
31
+ print("πŸ”Š Generating Speech...")
32
+ tts = TTS(model_name="tts_models/en/ljspeech/glow-tts")
33
+ output_file = "response.wav"
34
+ tts.tts_to_file(text=text, file_path=output_file)
35
+ return output_file
36
+
37
+ # βœ… Gradio Interface Function
38
+ def chatbot_pipeline(audio):
39
+ # Step 1: Convert Speech to Text
40
+ text = transcribe_audio(audio)
41
+
42
+ # Step 2: Get Response from Groq API
43
+ response = get_groq_response(text)
44
+
45
+ # Step 3: Convert Text Response to Speech
46
+ speech_file = text_to_speech(response)
47
+
48
+ return text, response, speech_file
49
+
50
+ # βœ… Build Gradio UI
51
+ interface = gr.Interface(
52
+ fn=chatbot_pipeline,
53
+ inputs=gr.Audio(type="filepath"), # Mic input
54
+ outputs=[
55
+ gr.Textbox(label="Transcribed Text"),
56
+ gr.Textbox(label="Chatbot Response"),
57
+ gr.Audio(label="Generated Speech")
58
+ ],
59
+ title="πŸ—£οΈ Speech-to-Text AI Chatbot",
60
+ description="πŸŽ™οΈ Speak into the microphone β†’ Get AI response β†’ Listen to the reply!"
61
+ )
62
+
63
+ # βœ… Launch Gradio UI
64
+ interface.launch(share=True)