redjkh commited on
Commit
07fb0cf
·
verified ·
1 Parent(s): f723976

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -50
app.py CHANGED
@@ -1,58 +1,65 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- import os
4
 
5
- # Load your token from Hugging Face Secrets in your Space settings
6
- HF_TOKEN = os.getenv("HF_TOKEN")
7
-
8
- # Choose a good open-source chat model
9
  MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
10
 
11
- client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN)
12
-
13
- SYSTEM_PROMPT = """
14
- You are a friendly recipe assistant 🍳.
15
- You suggest recipes based on:
16
- - Ingredients the user has
17
- - Their budget
18
- - Any allergies or dietary restrictions
19
- - Cooking difficulty level they prefer
20
- Always give clear step-by-step instructions.
21
- """
22
-
23
- # Chat function
24
- def chat(user_message, history):
25
- # Convert history into messages format
26
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
27
-
28
- for user, bot in history:
29
- messages.append({"role": "user", "content": user})
30
- messages.append({"role": "assistant", "content": bot})
31
-
32
- messages.append({"role": "user", "content": user_message})
33
-
34
- # Get response from model
35
- response = client.chat_completion(
36
- messages=messages,
37
- max_tokens=512,
38
- temperature=0.7,
39
- top_p=0.9
 
 
 
 
 
 
 
40
  )
41
-
42
- reply = response.choices[0].message["content"]
43
- history.append((user_message, reply))
44
- return history, ""
45
 
46
- # UI
 
 
 
 
 
 
 
 
 
 
 
47
  with gr.Blocks() as demo:
48
- gr.Markdown("# 🍽️ Smart Recipe Chatbot")
49
- chatbot = gr.Chatbot()
50
- msg = gr.Textbox(label="Ask me for a recipe...")
51
- clear = gr.Button("Clear Chat")
52
-
53
- msg.submit(chat, [msg, chatbot], [chatbot, msg])
54
- clear.click(lambda: ([], ""), None, [chatbot, msg])
55
-
56
- # Run
57
- if __name__ == "__main__":
58
- 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
+ yield 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()