Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -54,93 +54,86 @@ import gradio as gr
|
|
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(
|
67 |
-
|
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 |
-
|
79 |
-
|
80 |
-
|
81 |
-
if
|
82 |
-
messages.append({"role": "assistant", "content":
|
83 |
|
84 |
-
|
85 |
-
|
|
|
86 |
|
|
|
87 |
response = ""
|
88 |
for chunk in chat_client.chat_completion(
|
89 |
messages,
|
90 |
-
max_tokens=
|
91 |
stream=True,
|
92 |
-
temperature=
|
93 |
-
top_p=
|
94 |
):
|
95 |
token = chunk.choices[0].delta.content
|
96 |
response += token
|
97 |
-
|
|
|
98 |
|
99 |
-
# Custom CSS for better
|
100 |
css = """
|
101 |
-
.gradio-container {
|
102 |
-
|
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 |
-
|
116 |
-
|
117 |
-
with gr.Column(
|
118 |
-
gr.Markdown("## Input Methods")
|
119 |
with gr.Tab("Text Input"):
|
120 |
-
text_input = gr.Textbox(
|
121 |
with gr.Tab("Voice Input"):
|
122 |
audio_input = gr.Audio(
|
123 |
sources="microphone",
|
124 |
type="filepath",
|
125 |
-
label="
|
126 |
elem_classes="audio-input"
|
127 |
)
|
128 |
submit_btn = gr.Button("Submit", variant="primary")
|
129 |
|
130 |
-
def
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
136 |
|
137 |
submit_btn.click(
|
138 |
-
|
139 |
[text_input, audio_input, chatbot],
|
140 |
-
|
|
|
141 |
).then(
|
142 |
respond,
|
143 |
-
[
|
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 |
|