syedmudassir16 commited on
Commit
98c26bb
·
verified ·
1 Parent(s): 2ae0339

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -10,6 +10,8 @@ client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
10
  # Initialize the ASR pipeline
11
  asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
12
 
 
 
13
  def speech_to_text(speech):
14
  """Converts speech to text using the ASR pipeline."""
15
  return asr(speech)["text"]
@@ -64,7 +66,6 @@ def format_prompt(message, history):
64
 
65
  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.
66
 
67
- [Examples omitted for brevity]
68
  """
69
  prompt = f"{fixed_prompt}\n"
70
  for user_prompt, bot_response in history:
@@ -84,7 +85,7 @@ def process_input(input_text, history):
84
  return history, history, "", None
85
  response = generate(input_text, history)
86
  history.append((input_text, response))
87
- return history, history, "", None # Return history twice: once for state, once for chatbot, and reset voice input
88
 
89
  async def generate_audio(history):
90
  if history and len(history) > 0:
@@ -93,19 +94,27 @@ async def generate_audio(history):
93
  return audio_path
94
  return None
95
 
 
 
 
 
 
96
  # Gradio interface setup
97
  with gr.Blocks() as demo:
98
  gr.Markdown("# Mood-Based Music Recommender with Continuous Voice Chat")
99
 
100
- chatbot = gr.Chatbot()
101
- msg = gr.Textbox(placeholder="Type your message here or use the microphone to speak...")
102
  audio_output = gr.Audio(label="AI Response", autoplay=True)
103
 
104
  state = gr.State([])
105
 
106
  with gr.Row():
107
  submit = gr.Button("Send")
108
- voice_input = gr.Audio(sources="microphone", type="filepath", label="Voice Input")
 
 
 
109
 
110
  # Handle text input
111
  msg.submit(process_input, inputs=[msg, state], outputs=[state, chatbot, msg, voice_input]).then(
 
10
  # Initialize the ASR pipeline
11
  asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
12
 
13
+ INITIAL_MESSAGE = "Hi! I'm your music buddy—tell me about your mood and the type of tunes you're in the mood for today!"
14
+
15
  def speech_to_text(speech):
16
  """Converts speech to text using the ASR pipeline."""
17
  return asr(speech)["text"]
 
66
 
67
  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.
68
 
 
69
  """
70
  prompt = f"{fixed_prompt}\n"
71
  for user_prompt, bot_response in history:
 
85
  return history, history, "", None
86
  response = generate(input_text, history)
87
  history.append((input_text, response))
88
+ return history, history, "", None
89
 
90
  async def generate_audio(history):
91
  if history and len(history) > 0:
 
94
  return audio_path
95
  return None
96
 
97
+ async def init_chat():
98
+ history = [("", INITIAL_MESSAGE)]
99
+ audio_path = await text_to_speech(INITIAL_MESSAGE)
100
+ return history, history, audio_path
101
+
102
  # Gradio interface setup
103
  with gr.Blocks() as demo:
104
  gr.Markdown("# Mood-Based Music Recommender with Continuous Voice Chat")
105
 
106
+ chatbot = gr.Chatbot(height=400)
107
+ msg = gr.Textbox(placeholder="Type your message here or use the microphone to speak...", label="Your message")
108
  audio_output = gr.Audio(label="AI Response", autoplay=True)
109
 
110
  state = gr.State([])
111
 
112
  with gr.Row():
113
  submit = gr.Button("Send")
114
+ voice_input = gr.Audio(source="microphone", type="filepath", label="Voice Input")
115
+
116
+ # Initialize chat with greeting
117
+ demo.load(init_chat, outputs=[state, chatbot, audio_output])
118
 
119
  # Handle text input
120
  msg.submit(process_input, inputs=[msg, state], outputs=[state, chatbot, msg, voice_input]).then(