EngrGullu commited on
Commit
1935cdf
·
verified ·
1 Parent(s): d01926e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from io import BytesIO
3
+ import gradio as gr
4
+ from gtts import gTTS
5
+ from pydub import AudioSegment
6
+ import whisper
7
+ import openai # Using OpenAI as a replacement for Groq
8
+
9
+ GROQ_API_KEY = "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"
10
+
11
+ client = Groq(api_key=GROQ_API_KEY)
12
+
13
+ # Initialize models
14
+ whisper_model = whisper.load_model("base") # Load Whisper model
15
+
16
+ # Define the voice-to-voice workflow
17
+ def voice_to_voice(audio):
18
+ # 1. Transcribe audio using Whisper
19
+ transcription_result = whisper_model.transcribe(audio, fp16=False)
20
+ user_input = transcription_result["text"]
21
+
22
+ # 2. Get response from OpenAI's GPT (Replacing Groq's LLM)
23
+ response = openai.ChatCompletion.create(
24
+ model="gpt-4",
25
+ messages=[{"role": "user", "content": user_input}],
26
+ )
27
+ response_text = response.choices[0].message["content"]
28
+
29
+ # 3. Convert LLM response to audio using gTTS
30
+ tts = gTTS(text=response_text, lang="en")
31
+ audio_fp = BytesIO()
32
+ tts.write_to_fp(audio_fp)
33
+ audio_fp.seek(0)
34
+
35
+ # Convert gTTS output to a playable format using pydub
36
+ audio_segment = AudioSegment.from_file(audio_fp, format="mp3")
37
+ output_fp = BytesIO()
38
+ audio_segment.export(output_fp, format="mp3")
39
+ output_fp.seek(0)
40
+
41
+ return response_text, output_fp
42
+
43
+ # Gradio interface
44
+ iface = gr.Interface(
45
+ fn=voice_to_voice,
46
+ inputs=gr.Audio(type="filepath"), # Removed 'source' argument
47
+ outputs=[gr.Textbox(label="Transcription"), gr.Audio(label="Response Audio")],
48
+ live=True,
49
+ title="Real-Time Voice-to-Voice Chatbot",
50
+ description="Speak into the microphone and get a spoken response from the chatbot.",
51
+ )
52
+
53
+ # Launch Gradio app
54
+ iface.launch()