Files changed (1) hide show
  1. app.py +0 -140
app.py DELETED
@@ -1,140 +0,0 @@
1
- import gradio as gr
2
- import whisper
3
- from gtts import gTTS
4
- from groq import Groq
5
- import os
6
- import numpy as np
7
- import soundfile as sf
8
- import logging
9
-
10
- # Initialize the Groq API key
11
- GROQ_API_KEY = "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"
12
-
13
- # Configure logging
14
- logging.basicConfig(level=logging.DEBUG)
15
-
16
- # Initialize Whisper model (No API key required)
17
- try:
18
- whisper_model = whisper.load_model("base")
19
- logging.info("Whisper model loaded successfully.")
20
- except Exception as e:
21
- raise RuntimeError(f"Error loading Whisper model: {e}")
22
-
23
- # Initialize Groq client (API key required for Groq API)
24
- try:
25
- client = Groq(
26
- api_key=GROQ_API_KEY # Directly use the API key from the variable
27
- )
28
- logging.info("Groq client initialized successfully.")
29
- except Exception as e:
30
- raise RuntimeError(f"Error initializing Groq client: {e}")
31
-
32
- # Function to transcribe audio using Whisper
33
- def transcribe_audio(audio):
34
- try:
35
- # Load audio file with soundfile
36
- logging.debug(f"Loading audio file: {audio}")
37
- audio_data, sample_rate = sf.read(audio, dtype='float32') # Ensure dtype is float32
38
- logging.debug(f"Audio loaded with sample rate: {sample_rate}, data shape: {audio_data.shape}")
39
-
40
- # Whisper expects a specific sample rate
41
- if sample_rate != 16000:
42
- logging.debug(f"Resampling audio from {sample_rate} to 16000 Hz")
43
- # Resample audio data to 16000 Hz
44
- num_samples = int(len(audio_data) * (16000 / sample_rate))
45
- audio_data_resampled = np.interp(np.linspace(0, len(audio_data), num_samples),
46
- np.arange(len(audio_data)),
47
- audio_data)
48
- audio_data = audio_data_resampled.astype(np.float32) # Ensure dtype is float32
49
- sample_rate = 16000
50
-
51
- # Perform the transcription
52
- result = whisper_model.transcribe(audio_data)
53
- logging.debug(f"Transcription result: {result['text']}")
54
- return result['text']
55
- except Exception as e:
56
- logging.error(f"Error during transcription: {e}")
57
- return f"Error during transcription: {e}"
58
-
59
- # Function to get response from LLaMA model using Groq API
60
- def get_response(text):
61
- try:
62
- logging.debug(f"Sending request to Groq API with text: {text}")
63
- chat_completion = client.chat.completions.create(
64
- messages=[
65
- {
66
- "role": "user",
67
- "content": text, # Using the transcribed text as input
68
- }
69
- ],
70
- model="llama3-8b-8192", # Ensure the correct model is used
71
- )
72
-
73
- # Extract and return the model's response
74
- response_text = chat_completion.choices[0].message.content
75
- logging.debug(f"Received response from Groq API: {response_text}")
76
- return response_text
77
- except Exception as e:
78
- logging.error(f"Error during model response generation: {e}")
79
- return f"Error during model response generation: {e}"
80
-
81
- # Function to convert text to speech using gTTS
82
- def text_to_speech(text):
83
- try:
84
- tts = gTTS(text)
85
- tts.save("response.mp3")
86
- logging.debug("Text-to-speech conversion completed successfully.")
87
- return "response.mp3"
88
- except Exception as e:
89
- logging.error(f"Error during text-to-speech conversion: {e}")
90
- return f"Error during text-to-speech conversion: {e}"
91
-
92
- # Combined function for Gradio
93
- def chatbot(audio):
94
- try:
95
- # Step 1: Transcribe the audio input using Whisper
96
- user_input = transcribe_audio(audio)
97
-
98
- # Check if transcription returned an error
99
- if "Error" in user_input:
100
- return user_input, None
101
-
102
- logging.debug(f"Transcribed text: {user_input}")
103
-
104
- # Step 2: Get response from the LLaMA model using Groq API
105
- response_text = get_response(user_input)
106
-
107
- # Check if the response generation returned an error
108
- if "Error" in response_text:
109
- return response_text, None
110
-
111
- logging.debug(f"Response text: {response_text}")
112
-
113
- # Step 3: Convert the response text to speech using gTTS
114
- response_audio = text_to_speech(response_text)
115
-
116
- # Check if the text-to-speech conversion returned an error
117
- if "Error" in response_audio:
118
- return response_audio, None
119
-
120
- # Step 4: Return the response text and response audio file
121
- return response_text, response_audio
122
-
123
- except Exception as e:
124
- logging.error(f"Unexpected error occurred: {e}")
125
- return f"Unexpected error occurred: {e}", None
126
-
127
- # Gradio Interface
128
- iface = gr.Interface(
129
- fn=chatbot,
130
- inputs=gr.Audio(type="filepath"),
131
- outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
132
- live=True,
133
- title="Voice-to-Voice Chatbot",
134
- description="Speak to the bot, and it will respond with voice.",
135
- )
136
-
137
- try:
138
- iface.launch()
139
- except Exception as e:
140
- logging.error(f"Error launching Gradio interface: {e}")