MSaadZia commited on
Commit
55b4bbc
1 Parent(s): 64400b9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ from transformers import pipeline
4
+ from gtts import gTTS
5
+ import gradio as gr
6
+ from groq import Groq
7
+
8
+ # Load Whisper model from Hugging Face
9
+ try:
10
+ pipe = pipeline(model="openai/whisper-small", device="cuda" if torch.cuda.is_available() else "cpu")
11
+ except Exception as e:
12
+ print(f"Error loading Whisper model: {e}")
13
+ raise
14
+
15
+ GROQ_API_KEY = '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'
16
+
17
+ Client = Groq(api_key=GROQ_API_KEY)
18
+
19
+ # Function to get response from Groq LLM
20
+ def get_llm_response(transcribed_text):
21
+ try:
22
+ chat_completion = client.chat.completions.create(
23
+ messages=[{"role": "user", "content": transcribed_text}],
24
+ model="llama3-8b-8192",
25
+ )
26
+ return chat_completion.choices[0].message.content
27
+ except Exception as e:
28
+ print(f"Error getting response from LLM: {e}")
29
+ return "Sorry, I couldn't process your request."
30
+
31
+ # Function to convert text to speech
32
+ def text_to_speech(response_text):
33
+ try:
34
+ tts = gTTS(response_text, lang='en')
35
+ tts.save("response_audio.mp3")
36
+ return "response_audio.mp3" # Returning the file path
37
+ except Exception as e:
38
+ print(f"Error converting text to speech: {e}")
39
+ return "Sorry, I couldn't convert the response to audio."
40
+
41
+ # Function to handle the entire voice chat process
42
+ def voice_chat(audio_input):
43
+ try:
44
+ # Transcribe the input audio using Hugging Face Whisper model
45
+ result = pipe(audio_input)["text"]
46
+ transcribed_text = result
47
+ print(f"Transcribed Text: {transcribed_text}")
48
+
49
+ # Get the LLM response
50
+ response_text = get_llm_response(transcribed_text)
51
+ print(f"LLM Response: {response_text}")
52
+
53
+ # Convert the response text to speech and return the audio file
54
+ response_audio = text_to_speech(response_text)
55
+
56
+ return response_audio
57
+ except Exception as e:
58
+ print(f"Error in voice chat process: {e}")
59
+ return "Sorry, there was an error processing your audio."
60
+
61
+ # Create the Gradio interface
62
+ iface = gr.Interface(
63
+ fn=voice_chat,
64
+ inputs=gr.Audio(type="filepath"), # Specify input type only
65
+ outputs="audio"
66
+ )
67
+
68
+ # Launch the Gradio interface
69
+ iface.launch()