Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,37 @@
|
|
| 1 |
import whisper
|
| 2 |
from TTS.api import TTS
|
| 3 |
-
import requests
|
| 4 |
import gradio as gr
|
| 5 |
from pydub import AudioSegment
|
| 6 |
-
from
|
| 7 |
|
| 8 |
# Initialize models
|
| 9 |
-
whisper_model = whisper.load_model("small") #
|
| 10 |
tts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
| 11 |
|
| 12 |
-
# Groq
|
| 13 |
groq_api_key = "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"
|
| 14 |
-
|
| 15 |
|
| 16 |
# Functions for the Chatbot
|
| 17 |
def voice_to_text(audio_path):
|
| 18 |
-
"""Convert voice input to text
|
| 19 |
result = whisper_model.transcribe(audio_path)
|
| 20 |
-
|
| 21 |
-
return result["text"], detected_language
|
| 22 |
|
| 23 |
def process_text_with_groq(input_text):
|
| 24 |
"""Process user text input using Groq LLM."""
|
| 25 |
-
|
| 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 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
-
return f"
|
| 40 |
|
| 41 |
-
def
|
| 42 |
-
"""Translate text to the target language using deep-translator."""
|
| 43 |
-
translated_text = GoogleTranslator(source="auto", target=target_lang).translate(text)
|
| 44 |
-
return translated_text
|
| 45 |
-
|
| 46 |
-
def text_to_voice(output_text, language_code):
|
| 47 |
"""Convert text response to voice using Coqui TTS."""
|
| 48 |
audio_path = "response.wav"
|
| 49 |
tts_model.tts_to_file(text=output_text, file_path=audio_path)
|
|
@@ -51,19 +39,15 @@ def text_to_voice(output_text, language_code):
|
|
| 51 |
|
| 52 |
# Gradio Interface
|
| 53 |
def chatbot(audio_file):
|
| 54 |
-
#
|
| 55 |
-
user_input
|
| 56 |
-
|
| 57 |
-
#
|
| 58 |
bot_response = process_text_with_groq(user_input)
|
| 59 |
-
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Step 4: Convert the response to voice
|
| 65 |
-
audio_response_path = text_to_voice(bot_response, detected_language)
|
| 66 |
-
|
| 67 |
return bot_response, audio_response_path
|
| 68 |
|
| 69 |
# Gradio UI
|
|
@@ -75,7 +59,7 @@ ui = gr.Interface(
|
|
| 75 |
gr.Audio(label="Chatbot Voice Response")
|
| 76 |
],
|
| 77 |
title="Zeeshan Voice-to-Voice Chatbot",
|
| 78 |
-
description="Upload an audio file to interact with Zeeshan. Zeeshan will listen, process your query, and respond
|
| 79 |
)
|
| 80 |
|
| 81 |
# Launch Gradio app
|
|
|
|
| 1 |
import whisper
|
| 2 |
from TTS.api import TTS
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from pydub import AudioSegment
|
| 5 |
+
from groq import Groq
|
| 6 |
|
| 7 |
# Initialize models
|
| 8 |
+
whisper_model = whisper.load_model("small") # Use a smaller Whisper model for faster processing
|
| 9 |
tts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
| 10 |
|
| 11 |
+
# Initialize Groq Client
|
| 12 |
groq_api_key = "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"
|
| 13 |
+
client = Groq(api_key=groq_api_key)
|
| 14 |
|
| 15 |
# Functions for the Chatbot
|
| 16 |
def voice_to_text(audio_path):
|
| 17 |
+
"""Convert voice input to text using Whisper."""
|
| 18 |
result = whisper_model.transcribe(audio_path)
|
| 19 |
+
return result["text"]
|
|
|
|
| 20 |
|
| 21 |
def process_text_with_groq(input_text):
|
| 22 |
"""Process user text input using Groq LLM."""
|
| 23 |
+
messages = [{"role": "user", "content": input_text}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
+
chat_completion = client.chat.completions.create(
|
| 26 |
+
messages=messages,
|
| 27 |
+
model="llama3-8b-8192",
|
| 28 |
+
stream=False
|
| 29 |
+
)
|
| 30 |
+
return chat_completion.choices[0].message.content
|
| 31 |
except Exception as e:
|
| 32 |
+
return f"Error: {str(e)}"
|
| 33 |
|
| 34 |
+
def text_to_voice(output_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
"""Convert text response to voice using Coqui TTS."""
|
| 36 |
audio_path = "response.wav"
|
| 37 |
tts_model.tts_to_file(text=output_text, file_path=audio_path)
|
|
|
|
| 39 |
|
| 40 |
# Gradio Interface
|
| 41 |
def chatbot(audio_file):
|
| 42 |
+
# Convert audio to text
|
| 43 |
+
user_input = voice_to_text(audio_file)
|
| 44 |
+
|
| 45 |
+
# Get Groq LLM response
|
| 46 |
bot_response = process_text_with_groq(user_input)
|
| 47 |
+
|
| 48 |
+
# Convert text response to audio
|
| 49 |
+
audio_response_path = text_to_voice(bot_response)
|
| 50 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return bot_response, audio_response_path
|
| 52 |
|
| 53 |
# Gradio UI
|
|
|
|
| 59 |
gr.Audio(label="Chatbot Voice Response")
|
| 60 |
],
|
| 61 |
title="Zeeshan Voice-to-Voice Chatbot",
|
| 62 |
+
description="Upload an audio file to interact with Zeeshan. Zeeshan will listen, process your query using Groq's LLM, and respond with both text and voice."
|
| 63 |
)
|
| 64 |
|
| 65 |
# Launch Gradio app
|