Safwanahmad619 commited on
Commit
8afdb95
1 Parent(s): 6098e77

Update app.py

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