syedmudassir16 commited on
Commit
196e87a
·
verified ·
1 Parent(s): b18f307

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -161
app.py CHANGED
@@ -3,8 +3,13 @@ import gradio as gr
3
  import whisper
4
  from gtts import gTTS
5
  import io
 
6
  from huggingface_hub import InferenceClient
7
 
 
 
 
 
8
  # Initialize the Hugging Face Inference Client
9
  client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
10
 
@@ -16,172 +21,29 @@ def format_prompt(message, history):
16
  You are a smart mood analyser, who determines user mood. Based on the user input, classify the mood of the user into one of the four moods {Happy, Sad, Instrumental, Party}. If you are finding it difficult to classify into one of these four moods, keep the conversation going on until we classify the user's mood. Return a single-word reply from one of the options if you have classified. Suppose you classify a sentence as happy, then just respond with "happy".
17
 
18
  Note: Do not write anything else other than the classified mood if classified.
19
-
20
  Note: If any question or any user text cannot be classified, follow up with a question to know the user's mood until you classify the mood.
21
-
22
  Note: Mood should be classified only from any of these 4 classes {Happy, Sad, Instrumental, Party}, if not any of these 4 then continue with a follow-up question until you classify the mood.
23
-
24
  Note: if user asks something like i need a coffee then do not classify the mood directly and ask more follow-up questions as asked in examples.
25
 
26
- Examples
27
- User: What is C programming?
28
- LLM Response: C programming is a programming language. How are you feeling now after knowing the answer?
29
-
30
- User: Can I get a coffee?
31
- LLM Response: It sounds like you're in need of a little pick-me-up. How are you feeling right now? Are you looking for something upbeat, something to relax to, or maybe some instrumental music while you enjoy your coffee?
32
- User: I feel like rocking
33
- LLM Response: Party
34
-
35
- User: I'm feeling so energetic today!
36
- LLM Response: Happy
37
-
38
- User: I'm feeling down today.
39
- LLM Response: Sad
40
-
41
- User: I'm ready to have some fun tonight!
42
- LLM Response: Party
43
-
44
- User: I need some background music while I am stuck in traffic.
45
- LLM Response: Instrumental
46
-
47
- User: Hi
48
- LLM Response: Hi, how are you doing?
49
-
50
- User: Feeling okay only.
51
- LLM Response: Are you having a good day?
52
- User: I don't know
53
- LLM Response: Do you want to listen to some relaxing music?
54
- User: No
55
- LLM Response: How about listening to some rock and roll music?
56
- User: Yes
57
- LLM Response: Party
58
-
59
- User: Where do I find an encyclopedia?
60
- LLM Response: You can find it in any of the libraries or on the Internet. Does this answer make you happy?
61
-
62
- User: I need a coffee
63
- LLM Response: It sounds like you're in need of a little pick-me-up. How are you feeling right now? Are you looking for something upbeat, something to relax to, or maybe some instrumental music while you enjoy your coffee?
64
-
65
- User: I just got promoted at work!
66
- LLM Response: Happy
67
-
68
- User: Today is my birthday!
69
- LLM Response: Happy
70
-
71
- User: I won a prize in the lottery.
72
- LLM Response: Happy
73
-
74
- User: I am so excited about my vacation next week!
75
- LLM Response: Happy
76
-
77
- User: I aced my exams!
78
- LLM Response: Happy
79
-
80
- User: I had a wonderful time with my family today.
81
- LLM Response: Happy
82
-
83
- User: I just finished a great workout!
84
- LLM Response: Happy
85
-
86
- User: I am feeling really good about myself today.
87
- LLM Response: Happy
88
-
89
- User: I finally finished my project and it was a success!
90
- LLM Response: Happy
91
-
92
- User: I just heard my favorite song on the radio.
93
- LLM Response: Happy
94
-
95
- User: My pet passed away yesterday.
96
- LLM Response: Sad
97
-
98
- User: I lost my job today.
99
- LLM Response: Sad
100
-
101
- User: I'm feeling really lonely.
102
- LLM Response: Sad
103
-
104
- User: I didn't get the results I wanted.
105
- LLM Response: Sad
106
-
107
- User: I had a fight with my best friend.
108
- LLM Response: Sad
109
 
110
- User: I'm feeling really overwhelmed with everything.
111
- LLM Response: Sad
112
 
113
- User: I just got some bad news.
114
- LLM Response: Sad
115
 
116
- User: I'm missing my family.
117
- LLM Response: Sad
118
 
119
- User: I am feeling really down today.
120
- LLM Response: Sad
121
 
122
- User: Nothing seems to be going right.
123
- LLM Response: Sad
124
-
125
- User: I need some music while I study.
126
- LLM Response: Instrumental
127
-
128
- User: I want to listen to something soothing while I work.
129
- LLM Response: Instrumental
130
-
131
- User: Do you have any recommendations for background music?
132
- LLM Response: Instrumental
133
-
134
- User: I'm looking for some relaxing tunes.
135
- LLM Response: Instrumental
136
-
137
- User: I need some music to focus on my tasks.
138
- LLM Response: Instrumental
139
-
140
- User: Can you suggest some ambient music for meditation?
141
- LLM Response: Instrumental
142
-
143
- User: What's good for background music during reading?
144
- LLM Response: Instrumental
145
-
146
- User: I need some calm music to help me sleep.
147
- LLM Response: Instrumental
148
-
149
- User: I prefer instrumental music while cooking.
150
- LLM Response: Instrumental
151
-
152
- User: What's the best music to play while doing yoga?
153
- LLM Response: Instrumental
154
-
155
- User: Let's have a blast tonight!
156
- LLM Response: Party
157
-
158
- User: I'm in the mood to dance!
159
- LLM Response: Party
160
-
161
- User: I want to celebrate all night long!
162
- LLM Response: Party
163
-
164
- User: Time to hit the club!
165
- LLM Response: Party
166
-
167
- User: I feel like partying till dawn.
168
- LLM Response: Party
169
-
170
- User: Let's get this party started!
171
- LLM Response: Party
172
-
173
- User: I'm ready to party hard tonight.
174
- LLM Response: Party
175
-
176
- User: I'm in the mood for some loud music and dancing!
177
- LLM Response: Party
178
-
179
- User: Tonight's going to be epic!
180
- LLM Response: Party
181
-
182
- User: Lets turn up the music and have some fun!
183
- LLM Response: Party
184
- """
185
  prompt = f"<s>{fixed_prompt}"
186
  for user_prompt, bot_response in history:
187
  prompt += f"\n User:{user_prompt}\n LLM Response:{bot_response}"
@@ -229,14 +91,41 @@ def generate(
229
 
230
  def process_audio(audio_file):
231
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  # Transcribe the audio using Whisper
233
- result = model.transcribe(audio_file)
 
234
  text = result["text"]
235
 
 
 
 
 
 
 
 
236
  # Generate a response using the existing generate function
 
237
  response = generate(text, [])
238
 
 
 
239
  # Convert the response text to speech
 
240
  tts = gTTS(response)
241
  response_audio_io = io.BytesIO()
242
  tts.write_to_fp(response_audio_io)
@@ -247,9 +136,11 @@ def process_audio(audio_file):
247
  with open(response_audio_path, "wb") as audio_file:
248
  audio_file.write(response_audio_io.getvalue())
249
 
 
250
  return text, response, response_audio_path
251
  except Exception as e:
252
- return f"An error occurred: {e}", "", None
 
253
 
254
  # Create the Gradio interface with customized UI
255
  with gr.Blocks(css="""
@@ -295,7 +186,7 @@ with gr.Blocks(css="""
295
 
296
  with gr.Row():
297
  with gr.Column():
298
- audio_input = gr.Audio(sources="microphone", type="filepath", label="Upload Audio or Use Microphone")
299
  submit_button = gr.Button("Submit")
300
 
301
  with gr.Column():
@@ -305,4 +196,5 @@ with gr.Blocks(css="""
305
 
306
  submit_button.click(fn=process_audio, inputs=audio_input, outputs=[transcription, response_text, response_audio])
307
 
308
- demo.launch(share=True)
 
 
3
  import whisper
4
  from gtts import gTTS
5
  import io
6
+ import logging
7
  from huggingface_hub import InferenceClient
8
 
9
+ # Set up logging
10
+ logging.basicConfig(level=logging.DEBUG)
11
+ logger = logging.getLogger(__name__)
12
+
13
  # Initialize the Hugging Face Inference Client
14
  client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
15
 
 
21
  You are a smart mood analyser, who determines user mood. Based on the user input, classify the mood of the user into one of the four moods {Happy, Sad, Instrumental, Party}. If you are finding it difficult to classify into one of these four moods, keep the conversation going on until we classify the user's mood. Return a single-word reply from one of the options if you have classified. Suppose you classify a sentence as happy, then just respond with "happy".
22
 
23
  Note: Do not write anything else other than the classified mood if classified.
 
24
  Note: If any question or any user text cannot be classified, follow up with a question to know the user's mood until you classify the mood.
 
25
  Note: Mood should be classified only from any of these 4 classes {Happy, Sad, Instrumental, Party}, if not any of these 4 then continue with a follow-up question until you classify the mood.
 
26
  Note: if user asks something like i need a coffee then do not classify the mood directly and ask more follow-up questions as asked in examples.
27
 
28
+ Examples:
29
+ User: I'm feeling so energetic today!
30
+ LLM Response: Happy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ User: I'm feeling down today.
33
+ LLM Response: Sad
34
 
35
+ User: I need some background music while I am stuck in traffic.
36
+ LLM Response: Instrumental
37
 
38
+ User: Let's have a blast tonight!
39
+ LLM Response: Party
40
 
41
+ User: Hi
42
+ LLM Response: Hi, how are you doing?
43
 
44
+ User: I need a coffee
45
+ LLM Response: It sounds like you're in need of a little pick-me-up. How are you feeling right now? Are you looking for something upbeat, something to relax to, or maybe some instrumental music while you enjoy your coffee?
46
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  prompt = f"<s>{fixed_prompt}"
48
  for user_prompt, bot_response in history:
49
  prompt += f"\n User:{user_prompt}\n LLM Response:{bot_response}"
 
91
 
92
  def process_audio(audio_file):
93
  try:
94
+ logger.debug(f"Processing audio file: {audio_file}")
95
+
96
+ # Check if audio_file is None or empty
97
+ if audio_file is None or not os.path.exists(audio_file):
98
+ logger.warning("No audio input detected")
99
+ return "No audio input detected. Please try again.", "", None
100
+
101
+ # Load audio file
102
+ audio = whisper.load_audio(audio_file)
103
+
104
+ # Check if audio is empty
105
+ if len(audio) == 0:
106
+ logger.warning("Empty audio file detected")
107
+ return "The audio file appears to be empty. Please try again with a valid audio input.", "", None
108
+
109
  # Transcribe the audio using Whisper
110
+ logger.debug("Transcribing audio")
111
+ result = model.transcribe(audio)
112
  text = result["text"]
113
 
114
+ # Check if transcription is empty
115
+ if not text.strip():
116
+ logger.warning("No speech detected in the audio")
117
+ return "No speech detected in the audio. Please try speaking more clearly or check your microphone.", "", None
118
+
119
+ logger.debug(f"Transcribed text: {text}")
120
+
121
  # Generate a response using the existing generate function
122
+ logger.debug("Generating response")
123
  response = generate(text, [])
124
 
125
+ logger.debug(f"Generated response: {response}")
126
+
127
  # Convert the response text to speech
128
+ logger.debug("Converting response to speech")
129
  tts = gTTS(response)
130
  response_audio_io = io.BytesIO()
131
  tts.write_to_fp(response_audio_io)
 
136
  with open(response_audio_path, "wb") as audio_file:
137
  audio_file.write(response_audio_io.getvalue())
138
 
139
+ logger.debug("Audio processing completed successfully")
140
  return text, response, response_audio_path
141
  except Exception as e:
142
+ logger.exception("An error occurred while processing audio")
143
+ return f"An error occurred: {str(e)}", "", None
144
 
145
  # Create the Gradio interface with customized UI
146
  with gr.Blocks(css="""
 
186
 
187
  with gr.Row():
188
  with gr.Column():
189
+ audio_input = gr.Audio(source="microphone", type="filepath", label="Upload Audio or Use Microphone")
190
  submit_button = gr.Button("Submit")
191
 
192
  with gr.Column():
 
196
 
197
  submit_button.click(fn=process_audio, inputs=audio_input, outputs=[transcription, response_text, response_audio])
198
 
199
+ if __name__ == "__main__":
200
+ demo.launch(share=True)