vatistasdimitris commited on
Commit
d15702a
·
verified ·
1 Parent(s): d8cf98e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -11,18 +11,21 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
11
  def respond(audio, history, system_message, max_tokens, temperature, top_p):
12
  # Convert audio to text
13
  recognizer = sr.Recognizer()
14
- with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_audio_file:
15
- temp_audio_file.write(audio)
16
- temp_audio_file_path = temp_audio_file.name
17
-
18
  try:
19
- with sr.AudioFile(temp_audio_file_path) as source:
 
 
 
 
 
20
  audio_data = recognizer.record(source)
21
  message = recognizer.recognize_google(audio_data)
22
  except Exception as e:
23
  return "Error in recognizing audio", None
24
-
25
- os.remove(temp_audio_file_path)
 
26
 
27
  # Prepare messages for the model
28
  messages = [{"role": "system", "content": system_message}]
@@ -61,7 +64,7 @@ def respond(audio, history, system_message, max_tokens, temperature, top_p):
61
  demo = gr.Interface(
62
  fn=respond,
63
  inputs=[
64
- gr.Audio(source="upload", type="bytes", label="Upload your audio"),
65
  gr.State(value=[], label="History"),
66
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
67
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
@@ -71,7 +74,8 @@ demo = gr.Interface(
71
  outputs=[
72
  gr.Textbox(label="Response"),
73
  gr.Audio(label="Response Audio", type="file")
74
- ]
 
75
  )
76
 
77
  if __name__ == "__main__":
 
11
  def respond(audio, history, system_message, max_tokens, temperature, top_p):
12
  # Convert audio to text
13
  recognizer = sr.Recognizer()
14
+ temp_audio_path = None
 
 
 
15
  try:
16
+ # Save the uploaded file to a temporary path
17
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_file:
18
+ temp_file.write(audio)
19
+ temp_audio_path = temp_file.name
20
+
21
+ with sr.AudioFile(temp_audio_path) as source:
22
  audio_data = recognizer.record(source)
23
  message = recognizer.recognize_google(audio_data)
24
  except Exception as e:
25
  return "Error in recognizing audio", None
26
+ finally:
27
+ if temp_audio_path:
28
+ os.remove(temp_audio_path)
29
 
30
  # Prepare messages for the model
31
  messages = [{"role": "system", "content": system_message}]
 
64
  demo = gr.Interface(
65
  fn=respond,
66
  inputs=[
67
+ gr.Audio(type="bytes", label="Record your audio"),
68
  gr.State(value=[], label="History"),
69
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
70
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
74
  outputs=[
75
  gr.Textbox(label="Response"),
76
  gr.Audio(label="Response Audio", type="file")
77
+ ],
78
+ live=True
79
  )
80
 
81
  if __name__ == "__main__":