Zeeshan24 commited on
Commit
d06e8d0
·
verified ·
1 Parent(s): 716e25d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -40
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 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" # Replace this with the correct base URL if needed
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" # Replace with the correct endpoint from Groq documentation
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().get("choices", [{}])[0].get("message", {}).get("content", "No response received.")
36
- except requests.exceptions.HTTPError as http_err:
37
- return f"HTTP error occurred: {http_err}"
 
38
  except Exception as e:
39
- return f"An error occurred: {str(e)}"
40
 
41
- def translate_text(text, target_lang):
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
- # Step 1: Convert audio to text and detect language
55
- user_input, detected_language = voice_to_text(audio_file)
56
-
57
- # Step 2: Process the text with Groq LLM
58
  bot_response = process_text_with_groq(user_input)
59
-
60
- # Step 3: Translate the response if the detected language is not English
61
- if detected_language != "en": # Translate only if language is not English
62
- bot_response = translate_text(bot_response, detected_language)
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 in the same language with both text and voice."
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