fargerm commited on
Commit
86c0140
·
verified ·
1 Parent(s): f81d596

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import whisper
4
+ from groq import Groq
5
+ from gtts import gTTS
6
+ import tempfile
7
+
8
+ # Load the open-source Whisper model
9
+ model = whisper.load_model("base") # Options: "tiny", "base", "small", "medium", "large"
10
+
11
+ # Set your Groq API key directly
12
+ client = Groq(api_key="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")
13
+
14
+ # Function to transcribe, generate response, and convert to speech
15
+ def chat_with_bot(audio_input):
16
+ try:
17
+ # Step 1: Transcribe audio input using open-source Whisper
18
+ try:
19
+ result = model.transcribe(audio_input)
20
+ user_input = result['text']
21
+ except Exception as e:
22
+ return "Error during transcription: " + str(e), "", None
23
+
24
+ # Step 2: Generate response using Groq API with Llama 8B model
25
+ try:
26
+ chat_completion = client.chat.completions.create(
27
+ messages=[
28
+ {
29
+ "role": "user",
30
+ "content": user_input,
31
+ }
32
+ ],
33
+ model="llama3-8b-8192",
34
+ )
35
+ response_text = chat_completion.choices[0].message.content
36
+ except Exception as e:
37
+ return "Error during Groq API call: " + str(e), "", None
38
+
39
+ # Step 3: Convert the response text to speech using gTTS
40
+ try:
41
+ tts = gTTS(text=response_text, lang='en')
42
+ # Save the TTS output to a temporary file
43
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
44
+ tts.save(f.name)
45
+ output_audio = f.name
46
+ except Exception as e:
47
+ return "Error during text-to-speech conversion: " + str(e), "", None
48
+
49
+ # Step 4: Return the transcription, response, and audio file for display in Gradio UI
50
+ return user_input, response_text, output_audio
51
+
52
+ except Exception as e:
53
+ return "An unexpected error occurred: " + str(e), "", None
54
+
55
+ # Gradio Interface
56
+ iface = gr.Interface(
57
+ fn=chat_with_bot,
58
+ inputs=gr.Audio(type="filepath"), # Use 'type="filepath"' for audio input
59
+ outputs=[
60
+ gr.Textbox(label="Transcription"),
61
+ gr.Textbox(label="Response"),
62
+ gr.Audio(label="Generated Speech") # Output to replay the generated speech
63
+ ],
64
+ live=True,
65
+ title="Real-Time Voice-to-Voice Chatbot",
66
+ description="Speak into the microphone to chat with the Llama 8B model via Groq API."
67
+ )
68
+
69
+ # Launch the Gradio Interface
70
+ iface.launch()