RizwanSajad commited on
Commit
4b47098
·
verified ·
1 Parent(s): 0190a67

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Import necessary libraries
3
+ import whisper
4
+ from gtts import gTTS
5
+ import os
6
+ import tempfile
7
+ import gradio as gr
8
+ from groq import Groq
9
+ from dotenv import load_dotenv
10
+
11
+ # Load environment variables from .env file (if available)
12
+ load_dotenv()
13
+
14
+ # Set up Groq API key (fallback method if not set as an environment variable)
15
+ if "GROQ_API_KEY" not in os.environ:
16
+ os.environ["GROQ_API_KEY"] = "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" # Replace with your Groq API key
17
+
18
+ # Initialize Groq client
19
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
20
+
21
+ # Load the Whisper model
22
+ whisper_model = whisper.load_model("base") # "base" is lightweight; use "large" for higher accuracy
23
+
24
+ # Define chatbot function
25
+ def voice_to_voice_chatbot(input_audio):
26
+ """
27
+ Transcribes the audio input using Whisper, queries Groq's LLM API,
28
+ converts the response to audio, and returns the audio file.
29
+ """
30
+ # Step 1: Transcribe audio to text using Whisper
31
+ try:
32
+ transcription_result = whisper_model.transcribe(input_audio)
33
+ user_query = transcription_result["text"]
34
+ print("User Query:", user_query)
35
+ except Exception as e:
36
+ return f"Error in transcription: {str(e)}"
37
+
38
+ # Step 2: Query the Groq API with the transcribed text
39
+ try:
40
+ chat_completion = client.chat.completions.create(
41
+ messages=[{"role": "user", "content": user_query}],
42
+ model="llama3-8b-8192",
43
+ stream=False
44
+ )
45
+ bot_response = chat_completion.choices[0].message.content
46
+ print("Bot Response:", bot_response)
47
+ except Exception as e:
48
+ return f"Error in LLM response: {str(e)}"
49
+
50
+ # Step 3: Convert the LLM response to speech using gTTS
51
+ try:
52
+ tts = gTTS(text=bot_response, lang="en")
53
+ output_audio_path = tempfile.NamedTemporaryFile(suffix=".mp3").name
54
+ tts.save(output_audio_path)
55
+ except Exception as e:
56
+ return f"Error in text-to-speech conversion: {str(e)}"
57
+
58
+ # Return the audio file path
59
+ return output_audio_path
60
+
61
+ # Create Gradio interface for the chatbot
62
+ interface = gr.Interface(
63
+ fn=voice_to_voice_chatbot,
64
+ inputs=gr.Audio(type="filepath"), # Input: File path of recorded audio
65
+ outputs=gr.Audio(type="filepath"), # Output: File path of the generated audio
66
+ live=True, # Enable real-time interaction
67
+ description="This Voice to Voice Chatbot is created by Rizwan Sajad using OpenAI Whisper, Groq API, and gTTS."
68
+ )
69
+
70
+ # Launch Gradio interface
71
+ interface.launch()