Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import whisper
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from groq import Groq
|
| 5 |
+
from TTS.api import TTS
|
| 6 |
+
|
| 7 |
+
# β
Set up Groq API Key
|
| 8 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 9 |
+
|
| 10 |
+
# β
Load OpenAI Whisper model
|
| 11 |
+
whisper_model = whisper.load_model("base")
|
| 12 |
+
|
| 13 |
+
# β
Function to Transcribe Speech to Text
|
| 14 |
+
def transcribe_audio(audio):
|
| 15 |
+
print("π Transcribing...")
|
| 16 |
+
result = whisper_model.transcribe(audio)
|
| 17 |
+
return result["text"]
|
| 18 |
+
|
| 19 |
+
# β
Function to Get Response from Groq API
|
| 20 |
+
def get_groq_response(prompt):
|
| 21 |
+
print("π€ Generating Response...")
|
| 22 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 23 |
+
chat_completion = client.chat.completions.create(
|
| 24 |
+
messages=[{"role": "user", "content": prompt}],
|
| 25 |
+
model="llama-3.3-70b-versatile"
|
| 26 |
+
)
|
| 27 |
+
return chat_completion.choices[0].message.content
|
| 28 |
+
|
| 29 |
+
# β
Function to Convert Text to Speech using Coqui TTS
|
| 30 |
+
def text_to_speech(text):
|
| 31 |
+
print("π Generating Speech...")
|
| 32 |
+
tts = TTS(model_name="tts_models/en/ljspeech/glow-tts")
|
| 33 |
+
output_file = "response.wav"
|
| 34 |
+
tts.tts_to_file(text=text, file_path=output_file)
|
| 35 |
+
return output_file
|
| 36 |
+
|
| 37 |
+
# β
Gradio Interface Function
|
| 38 |
+
def chatbot_pipeline(audio):
|
| 39 |
+
# Step 1: Convert Speech to Text
|
| 40 |
+
text = transcribe_audio(audio)
|
| 41 |
+
|
| 42 |
+
# Step 2: Get Response from Groq API
|
| 43 |
+
response = get_groq_response(text)
|
| 44 |
+
|
| 45 |
+
# Step 3: Convert Text Response to Speech
|
| 46 |
+
speech_file = text_to_speech(response)
|
| 47 |
+
|
| 48 |
+
return text, response, speech_file
|
| 49 |
+
|
| 50 |
+
# β
Build Gradio UI
|
| 51 |
+
interface = gr.Interface(
|
| 52 |
+
fn=chatbot_pipeline,
|
| 53 |
+
inputs=gr.Audio(type="filepath"), # Mic input
|
| 54 |
+
outputs=[
|
| 55 |
+
gr.Textbox(label="Transcribed Text"),
|
| 56 |
+
gr.Textbox(label="Chatbot Response"),
|
| 57 |
+
gr.Audio(label="Generated Speech")
|
| 58 |
+
],
|
| 59 |
+
title="π£οΈ Speech-to-Text AI Chatbot",
|
| 60 |
+
description="ποΈ Speak into the microphone β Get AI response β Listen to the reply!"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# β
Launch Gradio UI
|
| 64 |
+
interface.launch(share=True)
|