Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
-
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
| 50 |
-
gr.
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Configuration
|
| 6 |
+
DEFAULT_API_KEY = "your-space-key-123" # Change this!
|
| 7 |
+
SYSTEM_PROMPT = """You are a mood-based recipe assistant. Follow these steps:
|
| 8 |
+
1. Detect the user's mood from their message
|
| 9 |
+
2. Suggest 3 recipes matching their mood and preferences
|
| 10 |
+
3. Format with emojis and clear instructions"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def generate_response(message, history=None, api_key=None):
|
| 15 |
+
"""Secure response generation with API key check"""
|
| 16 |
+
if api_key != DEFAULT_API_KEY:
|
| 17 |
+
return "π Error: Invalid API Key"
|
| 18 |
+
|
| 19 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 20 |
+
|
| 21 |
+
if history:
|
| 22 |
+
for user_msg, bot_msg in history:
|
| 23 |
+
messages.extend([
|
| 24 |
+
{"role": "user", "content": user_msg},
|
| 25 |
+
{"role": "assistant", "content": bot_msg}
|
| 26 |
+
])
|
| 27 |
+
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = ""
|
| 32 |
+
for chunk in client.chat_completion(messages, stream=True):
|
| 33 |
+
response += chunk.choices[0].delta.content or ""
|
| 34 |
+
return response
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"β οΈ Error: {str(e)}"
|
| 37 |
+
|
| 38 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
demo = gr.ChatInterface(
|
| 40 |
+
fn=lambda msg, hist: generate_response(msg, hist, DEFAULT_API_KEY),
|
| 41 |
additional_inputs=[
|
| 42 |
+
gr.Textbox(DEFAULT_API_KEY, label="API Key", type="password"),
|
| 43 |
+
gr.Textbox(SYSTEM_PROMPT, label="System Prompt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
],
|
| 45 |
+
title="π³ Mood-to-Recipe Assistant",
|
| 46 |
+
description="Share your mood β Get personalized recipes!",
|
| 47 |
+
examples=[
|
| 48 |
+
["I'm stressed and want comfort food"],
|
| 49 |
+
["Feeling happy! Suggest something light and fresh"]
|
| 50 |
+
]
|
| 51 |
)
|
| 52 |
|
| 53 |
+
# For Hugging Face Spaces deployment
|
| 54 |
+
demo.launch(show_api=True)
|
|
|