Engr-Saeed commited on
Commit
2954152
·
verified ·
1 Parent(s): 07734ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py CHANGED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import whisper
4
+ from gtts import gTTS
5
+ from groq import Groq
6
+ import numpy as np
7
+
8
+
9
+
10
+ # Set your Groq API key
11
+ os.environ['GROQ_API_KEY'] = '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'
12
+
13
+ # Initialize Whisper and Groq
14
+ whisper_model = whisper.load_model("base")
15
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
16
+
17
+ def chatbot(audio_input):
18
+ try:
19
+ # Debug: Check the type and content of audio_input
20
+ print(f"Audio input type: {type(audio_input)}")
21
+ if audio_input is None:
22
+ raise ValueError("Audio input is None. Please provide a valid audio file.")
23
+
24
+ # Step 1: Transcribe audio input using OpenAI Whisper
25
+ transcription_result = whisper_model.transcribe(audio_input)
26
+ if transcription_result is None or not transcription_result.get("text"):
27
+ raise ValueError("Whisper transcription failed or returned empty text.")
28
+
29
+ transcription = transcription_result["text"]
30
+
31
+ # Step 2: Generate response using LLaMA 8B model via Groq API
32
+ chat_completion = client.chat.completions.create(
33
+ messages=[
34
+ {
35
+ "role": "user",
36
+ "content": transcription,
37
+ }
38
+ ],
39
+ model="llama3-8b-8192",
40
+ )
41
+
42
+ # Check if the response from Groq is valid
43
+ if chat_completion and chat_completion.choices:
44
+ response_text = chat_completion.choices[0].message.content
45
+ else:
46
+ raise ValueError("Invalid response from Groq API")
47
+
48
+ # Step 3: Convert text response to speech using GTTS
49
+ if response_text.strip():
50
+ tts = gTTS(response_text)
51
+ tts.save("response.mp3")
52
+ else:
53
+ raise ValueError("Response text is empty or invalid")
54
+
55
+ # Step 4: Return the response audio and text transcription
56
+ return "response.mp3", transcription, response_text
57
+
58
+ except Exception as e:
59
+ # Handle and display the error
60
+ return None, transcription if 'transcription' in locals() else None, f"Error: {str(e)}"
61
+
62
+ # Define the Gradio interface
63
+ interface = gr.Interface(
64
+ fn=chatbot,
65
+ inputs=gr.Audio(type="filepath"),
66
+ outputs=[gr.Audio(type="filepath"), "text", "text"],
67
+ title="Voice-to-Voice Chatbot",
68
+ description="Speak to the chatbot and get a real-time response.",
69
+ live=True # Automatically processes input without requiring a button click
70
+ )
71
+
72
+ # Launch the Gradio app
73
+ interface.launch()