Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import whisper
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
from deep_translator import GoogleTranslator
|
| 7 |
+
|
| 8 |
+
# Initialize models
|
| 9 |
+
whisper_model = whisper.load_model("small") # Faster Whisper model
|
| 10 |
+
tts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
| 11 |
+
|
| 12 |
+
# Groq API Key and Base URL
|
| 13 |
+
groq_api_key = "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"
|
| 14 |
+
groq_base_url = "https://api.groq.com" # Base URL from Groq documentation
|
| 15 |
+
|
| 16 |
+
# Functions for the Chatbot
|
| 17 |
+
def voice_to_text(audio_path):
|
| 18 |
+
"""Convert voice input to text and detect language using Whisper."""
|
| 19 |
+
result = whisper_model.transcribe(audio_path)
|
| 20 |
+
detected_language = result["language"]
|
| 21 |
+
return result["text"], detected_language
|
| 22 |
+
|
| 23 |
+
def process_text_with_groq(input_text):
|
| 24 |
+
"""Process user text input using Groq LLM."""
|
| 25 |
+
url = f"{groq_base_url}/chat/completions" # Update endpoint if needed
|
| 26 |
+
headers = {"Authorization": f"Bearer {groq_api_key}"}
|
| 27 |
+
payload = {
|
| 28 |
+
"messages": [{"role": "user", "content": input_text}],
|
| 29 |
+
"model": "llama3-8b-8192",
|
| 30 |
+
"stream": False,
|
| 31 |
+
}
|
| 32 |
+
try:
|
| 33 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 34 |
+
response.raise_for_status() # Raise an error for HTTP issues
|
| 35 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 36 |
+
except requests.exceptions.RequestException as e:
|
| 37 |
+
return f"Error: {str(e)}"
|
| 38 |
+
|
| 39 |
+
def translate_text(text, target_lang):
|
| 40 |
+
"""Translate text to the target language using deep-translator."""
|
| 41 |
+
translated_text = GoogleTranslator(source="auto", target=target_lang).translate(text)
|
| 42 |
+
return translated_text
|
| 43 |
+
|
| 44 |
+
def text_to_voice(output_text, language_code):
|
| 45 |
+
"""Convert text response to voice using Coqui TTS."""
|
| 46 |
+
audio_path = "response.wav"
|
| 47 |
+
tts_model.tts_to_file(text=output_text, file_path=audio_path)
|
| 48 |
+
return audio_path
|
| 49 |
+
|
| 50 |
+
# Gradio Interface
|
| 51 |
+
def chatbot(audio_file):
|
| 52 |
+
# Step 1: Convert audio to text and detect language
|
| 53 |
+
user_input, detected_language = voice_to_text(audio_file)
|
| 54 |
+
|
| 55 |
+
# Step 2: Process the text with Groq LLM
|
| 56 |
+
bot_response = process_text_with_groq(user_input)
|
| 57 |
+
|
| 58 |
+
# Step 3: Translate the response if the detected language is not English
|
| 59 |
+
if detected_language != "en": # Translate only if language is not English
|
| 60 |
+
bot_response = translate_text(bot_response, detected_language)
|
| 61 |
+
|
| 62 |
+
# Step 4: Convert the response to voice
|
| 63 |
+
audio_response_path = text_to_voice(bot_response, detected_language)
|
| 64 |
+
|
| 65 |
+
return bot_response, audio_response_path
|
| 66 |
+
|
| 67 |
+
# Gradio UI
|
| 68 |
+
ui = gr.Interface(
|
| 69 |
+
fn=chatbot,
|
| 70 |
+
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 71 |
+
outputs=[
|
| 72 |
+
gr.Textbox(label="Chatbot Response"),
|
| 73 |
+
gr.Audio(label="Chatbot Voice Response")
|
| 74 |
+
],
|
| 75 |
+
title="Zeeshan Voice-to-Voice Chatbot",
|
| 76 |
+
description="Upload an audio file to interact with Zeeshan. Zeeshan will listen, process your query, and respond in the same language with both text and voice."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Launch Gradio app
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
ui.launch()
|