Bhaskar2611 commited on
Commit
ecc3826
·
verified ·
1 Parent(s): 0491281

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -49
app.py CHANGED
@@ -54,93 +54,86 @@ import gradio as gr
54
  from huggingface_hub import InferenceClient
55
  import tempfile
56
 
57
- # Initialize clients for both chat and speech-to-text
58
  chat_client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
59
- stt_client = InferenceClient("openai/whisper-large-v3") # Using OpenAI's Whisper model for STT
60
 
61
  def transcribe_audio(audio_file):
62
  """Convert audio to text using Whisper model"""
63
  with open(audio_file, "rb") as f:
64
  return stt_client.automatic_speech_recognition(f.read())
65
 
66
- def respond(message, history: list[tuple[str, str]], audio=None):
67
- # If audio input is provided, transcribe it first
68
- if audio:
69
- message = transcribe_audio(audio)
70
-
71
- system_message = "You are a friendly Chatbot. If the user query is product-related, provide structured product recommendations based on intent and relevance."
72
- max_tokens = 2048
73
- temperature = 0.7
74
- top_p = 0.95
75
-
76
  messages = [{"role": "system", "content": system_message}]
77
 
78
- for val in history:
79
- if val[0]:
80
- messages.append({"role": "user", "content": val[0]})
81
- if val[1]:
82
- messages.append({"role": "assistant", "content": val[1]})
83
 
84
- product_prompt = ("Given a user's search query, recommend relevant products...")
85
- messages.append({"role": "user", "content": f"{product_prompt}\n\nUser query: {message}"})
 
86
 
 
87
  response = ""
88
  for chunk in chat_client.chat_completion(
89
  messages,
90
- max_tokens=max_tokens,
91
  stream=True,
92
- temperature=temperature,
93
- top_p=top_p,
94
  ):
95
  token = chunk.choices[0].delta.content
96
  response += token
97
- yield response
 
98
 
99
- # Custom CSS for better visual appearance
100
  css = """
101
- .gradio-container {
102
- background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
103
- min-height: 100vh;
104
- }
105
- .audio-input {
106
- background: white !important;
107
- border-radius: 10px !important;
108
- padding: 20px !important;
109
- }
110
  """
111
 
112
  with gr.Blocks(css=css) as demo:
113
- gr.Markdown("# Smart Product Assistant 🎤🛒")
 
114
  with gr.Row():
115
- with gr.Column(scale=2):
116
- chatbot = gr.Chatbot(height=600)
117
- with gr.Column(scale=1):
118
- gr.Markdown("## Input Methods")
119
  with gr.Tab("Text Input"):
120
- text_input = gr.Textbox(placeholder="Type your query here...", label="Text Input")
121
  with gr.Tab("Voice Input"):
122
  audio_input = gr.Audio(
123
  sources="microphone",
124
  type="filepath",
125
- label="Speak your query",
126
  elem_classes="audio-input"
127
  )
128
  submit_btn = gr.Button("Submit", variant="primary")
129
 
130
- def process_input(text, audio, history):
131
- if audio:
132
- return audio, history
133
- elif text:
134
- return text, history
135
- return "", history
 
 
 
 
136
 
137
  submit_btn.click(
138
- process_input,
139
  [text_input, audio_input, chatbot],
140
- [text_input, chatbot]
 
141
  ).then(
142
  respond,
143
- [text_input, chatbot, audio_input],
144
  chatbot
145
  )
146
 
 
54
  from huggingface_hub import InferenceClient
55
  import tempfile
56
 
57
+ # Initialize clients
58
  chat_client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
59
+ stt_client = InferenceClient("openai/whisper-large-v3")
60
 
61
  def transcribe_audio(audio_file):
62
  """Convert audio to text using Whisper model"""
63
  with open(audio_file, "rb") as f:
64
  return stt_client.automatic_speech_recognition(f.read())
65
 
66
+ def respond(history, query):
67
+ system_message = "You are a friendly Chatbot. Provide structured product recommendations based on user queries."
 
 
 
 
 
 
 
 
68
  messages = [{"role": "system", "content": system_message}]
69
 
70
+ # Build conversation history
71
+ for entry in history:
72
+ messages.append({"role": "user", "content": entry[0]})
73
+ if entry[1]: # Only add assistant response if present
74
+ messages.append({"role": "assistant", "content": entry[1]})
75
 
76
+ # Add product recommendation prompt
77
+ product_prompt = ("Analyze this query and provide recommendations: ")
78
+ messages.append({"role": "user", "content": f"{product_prompt}\n{query}"})
79
 
80
+ # Generate streamed response
81
  response = ""
82
  for chunk in chat_client.chat_completion(
83
  messages,
84
+ max_tokens=2048,
85
  stream=True,
86
+ temperature=0.7,
87
+ top_p=0.95,
88
  ):
89
  token = chunk.choices[0].delta.content
90
  response += token
91
+ history[-1] = (query, response) # Update last history entry
92
+ yield history
93
 
94
+ # Custom CSS for better styling
95
  css = """
96
+ .gradio-container { background: #f0f4f8 !important; }
97
+ .audio-input { background: white !important; border-radius: 10px; }
 
 
 
 
 
 
 
98
  """
99
 
100
  with gr.Blocks(css=css) as demo:
101
+ gr.Markdown("# Smart Product Assistant 🎤🛍️")
102
+
103
  with gr.Row():
104
+ chatbot = gr.Chatbot(height=600)
105
+
106
+ with gr.Column():
 
107
  with gr.Tab("Text Input"):
108
+ text_input = gr.Textbox(label="Type your query")
109
  with gr.Tab("Voice Input"):
110
  audio_input = gr.Audio(
111
  sources="microphone",
112
  type="filepath",
113
+ label="Record your query",
114
  elem_classes="audio-input"
115
  )
116
  submit_btn = gr.Button("Submit", variant="primary")
117
 
118
+ def process_inputs(text, audio, history):
119
+ """Handle both text and audio inputs"""
120
+ query = text.strip()
121
+ if audio and not query:
122
+ query = transcribe_audio(audio)
123
+
124
+ if query:
125
+ # Add new entry to history with empty response
126
+ return history + [[query, None]]
127
+ return history
128
 
129
  submit_btn.click(
130
+ process_inputs,
131
  [text_input, audio_input, chatbot],
132
+ chatbot,
133
+ queue=False
134
  ).then(
135
  respond,
136
+ [chatbot, text_input],
137
  chatbot
138
  )
139