File size: 4,532 Bytes
4777736
cece503
c583b68
cece503
 
 
4777736
74ee35b
4777736
 
 
 
 
 
 
 
 
 
c6774b7
6aaff45
4777736
 
a75258d
 
 
 
3bf73d5
cece503
 
 
0cf15ff
12e1eb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cece503
 
 
 
 
 
 
b5a2a43
d9e9d8b
cece503
 
 
 
 
 
4777736
7d09e3e
c23d4b5
1691afd
f4e6d64
 
 
 
 
 
 
 
 
 
 
 
36d2d95
 
c956554
ed7617a
c956554
 
 
b967551
80fd64a
75174ce
80fd64a
6aaff45
7cd0c55
 
6aaff45
36d2d95
55a30b0
36d2d95
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

import gradio as gr
from huggingface_hub import InferenceClient
from sentence_transformers import SentenceTransformer
import torch

# Load knowledge
with open("recipesplease.txt", "r", encoding="utf-8") as file:
    knowledge = file.read()
cleaned_chunks = [chunk.strip() for chunk in knowledge.strip().split("\n") if chunk.strip()]
model = SentenceTransformer('all-MiniLM-L6-v2')
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
def get_top_chunks(query):
    query_embedding = model.encode(query, convert_to_tensor=True)
    query_embedding_normalized = query_embedding / query_embedding.norm()
    similarities = torch.matmul(chunk_embeddings, query_embedding_normalized)
    top_indices = torch.topk(similarities, k=5).indices.tolist()
    return [cleaned_chunks[i] for i in top_indices]
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
def respond(message, history, cuisine, dietary_restrictions, allergies, preferred_ingredient):
    response = ""
    top_chunks = get_top_chunks(message)
    top_chunks += get_top_chunks(cuisine)
    top_chunks += get_top_chunks(str(dietary_restrictions))  # convert list to str
    top_chunks += get_top_chunks(allergies)
    top_chunks += get_top_chunks(preferred_ingredient)
    print(top_chunks)
    messages = [
        {
            "role": "system",
            "content": f"""
You are BiteBot, a friendly recipe chatbot. Use only the recipes from the following content: {top_chunks}.
The user prefers {cuisine} cuisine, has the following dietary restrictions: {dietary_restrictions}, and is allergic to: {allergies}.
Suggest one suitable recipe that matches these preferences. Say something like:
"Based on your preferences, would you like to try Elizabeth's Sweet Potato Casserole?"

If they say yes:
- First, share the ingredients.
- Then ask if they'd like the instructions.
- If they agree, provide the instructions.

If they say no:
- Ask if they'd like another recommendation.

Always use recipes only from {top_chunks}.
"""

        }
    ]
    if history:
        messages.extend(history)
    messages.append({"role": "user", "content": message})
    stream = client.chat_completion(
        messages,
        max_tokens=700,
        temperature=1.5,top_p=0.7,
        stream=True,
    )
    for message in stream:
        token = message.choices[0].delta.content
        if token is not None:
            response += token
            yield response

logo="banner.png"

theme = gr.themes.Monochrome(
    primary_hue="orange",
    secondary_hue="zinc",
    neutral_hue=gr.themes.Color(c100="rgba(255, 227.4411088400613, 206.9078947368421, 1)", c200="rgba(255, 229.53334184977007, 218.0921052631579, 1)", c300="rgba(255, 234.91658150229947, 213.6184210526316, 1)", c400="rgba(189.603125, 154.41663986650488, 133.88641721491229, 1)", c50="#f3d1bbff", c500="rgba(170.2125, 139.18781968574348, 118.70082236842106, 1)", c600="rgba(193.32187499999998, 129.35648241888094, 111.07528782894737, 1)", c700="rgba(184.13125000000002, 141.9707339039346, 106.60230263157897, 1)", c800="rgba(156.06796875, 104.12209005333418, 69.81988075657894, 1)", c900="rgba(156.39999999999998, 117.22008175779253, 80.2578947368421, 1)", c950="rgba(158.43203125, 125.1788770279765, 97.28282620614036, 1)"),
    text_size="sm",
    spacing_size="md",
    radius_size="sm",
).set(
    body_background_fill='*primary_50',
    body_background_fill_dark='*primary_50'
)


with gr.Blocks(theme=theme) as chatbot:
    gr.Image(
	    value="Henrietta.png", 
	    show_label=False, 
	    show_share_button = False, 
	    show_download_button = False)
    gr.Markdown("### 👋 Welcome to BiteBot!\nTell me your preferred **cuisine**, any **dietary restrictions**, and **allergies**, and I’ll help you figure out what to cook. You can ask questions like:\n- _“What should I make tonight?”_\n- _“I'm feeling like eating something spicy.”_\n- _“Give me a recipe extra cheesy”_")
    cuisine=gr.Textbox(label="cuisine")
    dietary_restrictions=gr.Dropdown(["Gluten-Free","Dairy-Free","Vegan","Vegetarian","Keto","Kosher","No Soy","No Seafood","No Pork","No Beef"], label="dietary restrictions", multiselect=True,info="you can select multiple!")
    allergies=gr.Textbox(label="allergies")
    preferred_ingredients=gr.Textbox(label="preferred ingredients")
    gr.ChatInterface(
                    fn=respond,
                    type="messages", additional_inputs=[cuisine,dietary_restrictions,allergies,preferred_ingredients]
                    )
   
chatbot.launch()