Bhaskar2611's picture
Update app.py
e35136f verified
raw
history blame
12.7 kB
# import gradio as gr
# from huggingface_hub import InferenceClient
# """
# For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
# """
# client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
# def respond(message, history: list[tuple[str, str]]):
# system_message = "You are a friendly Chatbot. If the user query is product-related, provide structured product recommendations based on intent and relevance."
# max_tokens = 2048
# temperature = 0.7
# top_p = 0.95
# messages = [{"role": "system", "content": system_message}]
# for val in history:
# if val[0]:
# messages.append({"role": "user", "content": val[0]})
# if val[1]:
# messages.append({"role": "assistant", "content": val[1]})
# # Append product recommendation prompt if the user query is relevant
# product_prompt = ("Given a user's search query, recommend the most relevant products from the catalog. "
# "Consider synonyms, user intent, and semantic meaning rather than just keyword matching. "
# "If the query is vague, infer potential needs based on common shopping behavior. "
# "Provide a ranked list of product recommendations with a short explanation for each suggestion. "
# "Ensure the recommendations are diverse and cover multiple relevant categories if applicable. "
# f"Now, based on the user query: '{message}', generate a well-structured product recommendation list.")
# messages.append({"role": "user", "content": product_prompt})
# response = ""
# for message in client.chat_completion(
# messages,
# max_tokens=max_tokens,
# stream=True,
# temperature=temperature,
# top_p=top_p,
# ):
# token = message.choices[0].delta.content
# response += token
# yield response
# """
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
# """
# demo = gr.ChatInterface(respond)
# if __name__ == "__main__":
# demo.launch()
# import gradio as gr
# from huggingface_hub import InferenceClient
# import tempfile
# # Initialize clients
# chat_client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
# stt_client = InferenceClient("openai/whisper-large-v3")
# def transcribe_audio(audio_file):
# """Convert audio to text using Whisper model"""
# with open(audio_file, "rb") as f:
# return stt_client.automatic_speech_recognition(f.read())
# def respond(history, query):
# system_message = "You are a friendly Chatbot. Provide structured product recommendations based on user queries."
# messages = [{"role": "system", "content": system_message}]
# # Build conversation history
# for entry in history:
# messages.append({"role": "user", "content": entry[0]})
# if entry[1]: # Only add assistant response if present
# messages.append({"role": "assistant", "content": entry[1]})
# # Add product recommendation prompt
# product_prompt = ("Analyze this query and provide recommendations: ")
# messages.append({"role": "user", "content": f"{product_prompt}\n{query}"})
# # Generate streamed response
# response = ""
# for chunk in chat_client.chat_completion(
# messages,
# max_tokens=2048,
# stream=True,
# temperature=0.7,
# top_p=0.95,
# ):
# token = chunk.choices[0].delta.content
# response += token
# history[-1] = (query, response) # Update last history entry
# yield history
# # Custom CSS for better styling
# css = """
# .gradio-container { background: #f0f4f8 !important; }
# .audio-input { background: white !important; border-radius: 10px; }
# """
# with gr.Blocks(css=css) as demo:
# gr.Markdown("# Smart Product Assistant πŸŽ€πŸ›οΈ")
# with gr.Row():
# chatbot = gr.Chatbot(height=600)
# with gr.Column():
# with gr.Tab("Text Input"):
# text_input = gr.Textbox(label="Type your query")
# with gr.Tab("Voice Input"):
# audio_input = gr.Audio(
# sources="microphone",
# type="filepath",
# label="Record your query",
# elem_classes="audio-input"
# )
# submit_btn = gr.Button("Submit", variant="primary")
# def process_inputs(text, audio, history):
# """Handle both text and audio inputs"""
# query = text.strip()
# if audio and not query:
# query = transcribe_audio(audio)
# if query:
# # Add new entry to history with empty response
# return history + [[query, None]]
# return history
# submit_btn.click(
# process_inputs,
# [text_input, audio_input, chatbot],
# chatbot,
# queue=False
# ).then(
# respond,
# [chatbot, text_input],
# chatbot
# )
# if __name__ == "__main__":
# demo.launch()
# import gradio as gr
# from huggingface_hub import InferenceClient
# import tempfile
# # Initialize clients
# chat_client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
# stt_client = InferenceClient("openai/whisper-large-v3")
# def transcribe_audio(audio_file):
# """Convert audio to text using Whisper model"""
# with open(audio_file, "rb") as f:
# result = stt_client.automatic_speech_recognition(f.read())
# return result.text # Extract only the text from the response
# def respond(history, query):
# system_message = """You are a friendly Product Assistant. Follow these rules:
# 1. If the query is product-related, provide structured recommendations
# 2. Consider both voice and text inputs equally
# 3. Format responses with bullet points and emojis
# 4. Always acknowledge voice queries specifically"""
# product_prompt = f"""Analyze this {'voice' if history[-1][0] == query else 'text'} query:
# "{query}"
# Recommend products considering:
# - User intent
# - Semantic meaning
# - Potential use cases
# - Price ranges
# Provide ranked list with brief explanations"""
# messages = [
# {"role": "system", "content": system_message},
# {"role": "user", "content": product_prompt}
# ]
# # Build conversation history
# for entry in history[:-1]: # Exclude current query
# messages.extend([
# {"role": "user", "content": entry[0]},
# {"role": "assistant", "content": entry[1]}
# ])
# # Generate streamed response
# response = ""
# for chunk in chat_client.chat_completion(
# messages,
# max_tokens=2048,
# stream=True,
# temperature=0.7,
# top_p=0.95,
# ):
# token = chunk.choices[0].delta.content
# response += token
# history[-1] = (history[-1][0], response) # Update last entry
# yield history
# # Custom styling
# css = """
# .gradio-container { background: #f5f7fa !important; }
# .audio-input { background: white !important; border-radius: 10px; }
# """
# with gr.Blocks(css=css) as demo:
# gr.Markdown("# 🎀 Voice-Activated Product Advisor πŸ›οΈ")
# with gr.Row():
# chatbot = gr.Chatbot(height=600, bubble_full_width=False)
# with gr.Column():
# with gr.Tab("πŸŽ™οΈ Voice Input"):
# audio_input = gr.Audio(
# sources="microphone",
# type="filepath",
# label="Speak your product request",
# elem_classes="audio-input"
# )
# with gr.Tab("πŸ“ Text Input"):
# text_input = gr.Textbox(label="Type your request")
# submit_btn = gr.Button("πŸš€ Get Recommendations", variant="primary")
# def process_inputs(text, audio, history):
# """Handle both input types"""
# query = text.strip()
# if audio:
# query = transcribe_audio(audio)
# # Add voice-specific marker
# query = f"🎀 Voice Query: {query}"
# if query:
# return history + [(query, None)] # Proper tuple format
# return history
# submit_btn.click(
# process_inputs,
# [text_input, audio_input, chatbot],
# chatbot,
# queue=False
# ).then(
# respond,
# [chatbot, text_input],
# chatbot
# )
# # Clear inputs after submission
# submit_btn.click(
# lambda: [None, None], # Clear audio and text inputs
# outputs=[text_input, audio_input]
# )
# if __name__ == "__main__":
# demo.launch()
import gradio as gr
from huggingface_hub import InferenceClient
import tempfile
# Initialize clients
chat_client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
stt_client = InferenceClient("openai/whisper-large-v3")
def transcribe_audio(audio_file):
"""Convert audio to text using Whisper model"""
try:
with open(audio_file, "rb") as f:
result = stt_client.automatic_speech_recognition(f.read())
return result.text
except Exception as e:
print(f"Transcription error: {e}")
return "Could not process audio. Please try again."
def respond(history, query):
system_message = """You are a Voice-Aware Product Assistant. Rules:
1. Always acknowledge voice queries with 🎧 icon
2. Provide structured recommendations with emojis
3. Consider both voice and text inputs equally"""
product_prompt = f"""Analyze this {'🎧 VOICE' if '🎧' in query else 'πŸ“ TEXT'} query:
"{query.replace('🎧 VOICE: ', '')}"
Recommend products considering:
- Voice tone analysis (if audio)
- Semantic meaning
- User intent
- Price context"""
messages = [
{"role": "system", "content": system_message},
{"role": "user", "content": product_prompt}
]
# Generate streamed response
response = ""
for chunk in chat_client.chat_completion(
messages,
max_tokens=2048,
stream=True,
temperature=0.7,
top_p=0.95,
):
token = chunk.choices[0].delta.content
response += token
history[-1] = (history[-1][0], response)
yield history
css = """
.gradio-container { background: #f8f9fa !important; }
.audio-input { background: white !important; border-radius: 10px; }
.mic-status { color: #4a90e2; font-weight: bold; }
"""
with gr.Blocks(css=css, title="Voice Product Assistant") as demo:
gr.Markdown("# 🎧 Voice-Activated Product Advisor πŸ›’")
with gr.Row():
chatbot = gr.Chatbot(height=600, bubble_full_width=False)
with gr.Column():
# Audio input with status indicator
with gr.Group():
audio_input = gr.Audio(
sources="microphone",
type="filepath",
label="Click mic & speak",
elem_classes="audio-input",
interactive=True
)
mic_status = gr.Markdown("πŸ”΄ Mic offline", elem_classes="mic-status")
# Text input
text_input = gr.Textbox(label="Or type your request")
# Unified submit button
submit_btn = gr.Button("πŸš€ Get Recommendations", variant="primary")
# Handle audio permissions
def request_mic_access():
return gr.update(text="🟒 Mic ready") if audio_input.is_enabled else gr.update(text="πŸ”΄ Mic blocked")
# Process inputs
def process_inputs(text, audio, history):
query = text.strip()
if audio:
transcript = transcribe_audio(audio)
query = f"🎧 VOICE: {transcript}"
if query:
return history + [(query, None)], ""
return history, ""
# Connect all components
audio_input.change(
request_mic_access,
outputs=mic_status,
queue=False
)
submit_btn.click(
process_inputs,
[text_input, audio_input, chatbot],
[chatbot, text_input],
queue=False
).then(
respond,
[chatbot, text_input],
chatbot
)
if __name__ == "__main__":
demo.launch(server_port=7860, share=False)