muhammadshaheryar commited on
Commit
a7d34ca
·
verified ·
1 Parent(s): 7624f31

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Step 2: Import necessary libraries
2
+ import os
3
+ import gradio as gr
4
+ from groq import Groq
5
+ from gtts import gTTS
6
+ import whisper
7
+ from io import BytesIO
8
+ import soundfile as sf
9
+
10
+ # Step 3: Set up Groq client
11
+ # Make sure to replace 'YOUR_GROQ_API_KEY' with your actual Groq API key
12
+ os.environ["GROQ_API_KEY"] = "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"
13
+ client = Groq(api_key=os.environ["GROQ_API_KEY"])
14
+
15
+ # Step 4: Load Whisper model
16
+ whisper_model = whisper.load_model("base")
17
+
18
+ # Step 5: Function for transcribing audio (adjusted for filepath)
19
+ def transcribe_audio(audio_filepath):
20
+ audio_data, _ = sf.read(audio_filepath) # Read audio file from path
21
+ result = whisper_model.transcribe(audio_data)
22
+ return result["text"]
23
+
24
+
25
+ # Step 6: Function to get a response from Groq LLM
26
+ def get_groq_response(user_input):
27
+ chat_completion = client.chat.completions.create(
28
+ messages=[{"role": "user", "content": user_input}],
29
+ model="llama3-8b-8192"
30
+ )
31
+ return chat_completion.choices[0].message.content
32
+
33
+ # Step 7: Function to convert text to speech using gTTS
34
+ def text_to_speech(text):
35
+ tts = gTTS(text)
36
+ audio_output = BytesIO()
37
+ tts.write_to_fp(audio_output)
38
+ audio_output.seek(0)
39
+ return audio_output
40
+
41
+ # Step 8: Gradio interface function
42
+ def chatbot_pipeline(audio):
43
+ # Step 8a: Transcribe the input audio
44
+ transcribed_text = transcribe_audio(audio)
45
+
46
+ # Step 8b: Get response from Groq
47
+ response_text = get_groq_response(transcribed_text)
48
+
49
+ # Step 8c: Convert response to speech
50
+ response_audio = text_to_speech(response_text)
51
+
52
+ return response_text, response_audio
53
+
54
+ # Step 9: Define Gradio interface (fix for audio output type)
55
+ interface = gr.Interface(
56
+ fn=chatbot_pipeline,
57
+ inputs=gr.Audio(type="filepath"),
58
+ outputs=[gr.Textbox(), gr.Audio(type="numpy")],
59
+ title="Real-Time Voice-to-Voice Chatbot",
60
+ description="Talk to a real-time chatbot that transcribes your voice, generates responses using Groq API, and reads them back to you!"
61
+ )
62
+
63
+ # Step 10: Launch Gradio interface
64
+ interface.launch(debug=True)