Moezulhaq24 commited on
Commit
9d5b1af
1 Parent(s): 737c0fe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import whisper
4
+ from gtts import gTTS
5
+ import io
6
+ from groq import Groq
7
+
8
+ # Initialize the Groq client
9
+ client = Groq(api_key="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")
10
+
11
+ # Load the Whisper model
12
+ model = whisper.load_model("base") # You can choose other models like "small", "medium", "large"
13
+
14
+ def process_audio(file_path):
15
+ try:
16
+ # Load the audio file
17
+ audio = whisper.load_audio(file_path)
18
+
19
+ # Transcribe the audio using Whisper
20
+ result = model.transcribe(audio)
21
+ text = result["text"]
22
+
23
+ # Generate a response using Groq
24
+ chat_completion = client.chat.completions.create(
25
+ messages=[{"role": "user", "content": text}],
26
+ model="llama3-8b-8192", # Replace with the correct model if necessary
27
+ )
28
+
29
+ # Access the response using dot notation
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) # Save the audio to the BytesIO object
36
+ response_audio_io.seek(0)
37
+
38
+ # Save audio to a file to ensure it's generated correctly
39
+ with open("response.mp3", "wb") as audio_file:
40
+ audio_file.write(response_audio_io.getvalue())
41
+
42
+ # Return the response text and the path to the saved audio file
43
+ return response_message, "response.mp3"
44
+
45
+ except Exception as e:
46
+ return f"An error occurred: {e}", None
47
+
48
+ iface = gr.Interface(
49
+ fn=process_audio,
50
+ inputs=gr.Audio(type="filepath"), # Use type="filepath"
51
+ outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
52
+ live=True,
53
+ title="Voice-to-Voice Chatbot"
54
+ )
55
+
56
+ iface.launch()