redjkh commited on
Commit
7778915
·
verified ·
1 Parent(s): d2dc922

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -42
app.py CHANGED
@@ -1,50 +1,65 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- import os
4
-
5
- # Make sure you set your HF token as an environment variable in your Space:
6
- # Settings → Variables → Add HF_TOKEN
7
- client = InferenceClient(
8
- "mistralai/Mistral-7B-Instruct-v0.3",
9
- token=os.environ.get("HF_TOKEN")
10
- )
11
-
12
- def respond(message, history, system_message, max_tokens, temperature, top_p):
13
- # Ensure history is in the role/content format
14
- messages = [{"role": "system", "content": system_message}]
15
- for msg in history:
16
- messages.append({"role": "user", "content": msg[0]})
17
- messages.append({"role": "assistant", "content": msg[1]})
18
-
19
- messages.append({"role": "user", "content": message})
20
-
21
- # Call the model
22
- completion = client.chat_completion(
23
- messages=messages,
24
- max_tokens=max_tokens,
25
- temperature=temperature,
26
- top_p=top_p
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  )
28
 
29
- reply = completion.choices[0].message["content"]
30
- history.append((message, reply))
31
- return history, history # return chatbot state & UI
 
 
 
 
 
 
 
 
32
 
33
  with gr.Blocks() as demo:
34
- gr.Markdown("## 🍳 Recipe Assistant Chatbot")
35
- chatbot = gr.Chatbot(type="messages", height=400)
36
- system_message = gr.Textbox(value="You are a helpful recipe assistant.", label="System Message")
37
- msg = gr.Textbox(label="Your message")
38
- max_tokens = gr.Slider(50, 500, value=200, step=10, label="Max Tokens")
39
- temperature = gr.Slider(0, 1, value=0.7, step=0.1, label="Temperature")
40
- top_p = gr.Slider(0, 1, value=0.9, step=0.1, label="Top P")
41
-
42
- state = gr.State([])
43
-
44
- msg.submit(
45
- respond,
46
- [msg, state, system_message, max_tokens, temperature, top_p],
47
- [chatbot, state]
48
- )
49
 
50
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+
4
+ # ✅ This model supports chat completions through the free Inference API
5
+ MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
6
+
7
+ client = InferenceClient(model=MODEL_NAME)
8
+
9
+ # Example fake recipe database
10
+ recipes = [
11
+ {
12
+ "name": "Veggie Pasta",
13
+ "ingredients": ["pasta", "tomato", "garlic", "olive oil"],
14
+ "allergies": ["gluten"],
15
+ "budget": "low"
16
+ },
17
+ {
18
+ "name": "Chicken Stir Fry",
19
+ "ingredients": ["chicken", "soy sauce", "broccoli", "garlic"],
20
+ "allergies": ["soy"],
21
+ "budget": "medium"
22
+ }
23
+ ]
24
+
25
+ def find_recipes(budget, have_items, allergies):
26
+ results = []
27
+ for recipe in recipes:
28
+ if recipe["budget"] != budget:
29
+ continue
30
+ if any(a in recipe["allergies"] for a in allergies):
31
+ continue
32
+ if not all(item in have_items for item in recipe["ingredients"]):
33
+ continue
34
+ results.append(recipe["name"])
35
+ return results or ["No matching recipes found."]
36
+
37
+ def chatbot(message, history, budget, have_items, allergies):
38
+ recipes_found = find_recipes(budget, have_items.split(","), allergies.split(","))
39
+ system_prompt = (
40
+ f"You are a friendly cooking assistant. The user has budget '{budget}', "
41
+ f"these ingredients: {have_items}, and allergies: {allergies}. "
42
+ f"Suggest recipes from this list: {recipes_found}."
43
  )
44
 
45
+ msgs = [{"role": "system", "content": system_prompt}]
46
+ for user_msg, bot_msg in history:
47
+ msgs.append({"role": "user", "content": user_msg})
48
+ msgs.append({"role": "assistant", "content": bot_msg})
49
+ msgs.append({"role": "user", "content": message})
50
+
51
+ response_text = ""
52
+ for resp in client.chat_completion(messages=msgs, stream=True):
53
+ token = resp.choices[0].delta.content or ""
54
+ response_text += token
55
+ return response_text
56
 
57
  with gr.Blocks() as demo:
58
+ gr.Markdown("## 🍳 Recipe Suggestion Chatbot")
59
+
60
+ budget = gr.Dropdown(["low", "medium", "high"], label="Budget", value="low")
61
+ have_items = gr.Textbox(label="Ingredients you have (comma separated)", placeholder="pasta,tomato,garlic")
62
+ allergies = gr.Textbox(label="Allergies (comma separated)", placeholder="gluten,soy")
63
+ chatbot_ui = gr.ChatInterface(fn=lambda msg, hist: chatbot(msg, hist, budget.value, have_items.value, allergies.value))
 
 
 
 
 
 
 
 
 
64
 
65
  demo.launch()