QuaidKhalid commited on
Commit
8345bbf
·
verified ·
1 Parent(s): 7fd2fd9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["GROQ_API_KEY"] = "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"
3
+ import gradio as gr
4
+ import whisper
5
+ from gtts import gTTS
6
+ import io
7
+ from groq import Groq
8
+
9
+ # Initialize the Whisper model
10
+ model = whisper.load_model("base")
11
+
12
+ # Initialize the Groq client with the API key from environment variables
13
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
14
+
15
+ def process_audio(file_path):
16
+ try:
17
+ if file_path is None:
18
+ raise ValueError("No audio file received. Please upload or record your voice.")
19
+
20
+ # Load the audio file and transcribe using Whisper
21
+ audio = whisper.load_audio(file_path)
22
+ result = model.transcribe(audio)
23
+ text = result["text"]
24
+
25
+ # Generate a response using Groq
26
+ chat_completion = client.chat.completions.create(
27
+ messages=[{"role": "user", "content": text}],
28
+ model="llama3-8b-8192",
29
+ )
30
+ response_message = chat_completion.choices[0].message.content.strip()
31
+
32
+ # Convert the response text to speech
33
+ tts = gTTS(response_message)
34
+ response_audio_io = io.BytesIO()
35
+ tts.write_to_fp(response_audio_io)
36
+ response_audio_io.seek(0)
37
+
38
+ # Save the audio to a file
39
+ audio_file_path = "response.mp3"
40
+ with open(audio_file_path, "wb") as audio_file:
41
+ audio_file.write(response_audio_io.getvalue())
42
+
43
+ # Return the response text and path to the saved audio file
44
+ return response_message, audio_file_path
45
+
46
+ except Exception as e:
47
+ return f"An error occurred: {e}", None
48
+
49
+ # Gradio interface
50
+ iface = gr.Interface(
51
+ fn=process_audio,
52
+ inputs=gr.Audio(type="filepath"),
53
+ outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
54
+ live=True, # Enables real-time processing
55
+ title="Voice-to-Voice Chatbot",
56
+ description="A real-time voice-to-voice chatbot using OpenAI Whisper, Groq LLaMA model, and GTTS."
57
+ )
58
+
59
+ # Launch the Gradio interface
60
+ iface.launch()