aashnaj commited on
Commit
d07e507
·
verified ·
1 Parent(s): a9f74cb

restore old one

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ from sentence_transformers import SentenceTransformer
4
+ import torch
5
+
6
+ # Load knowledge
7
+ with open("recipesplease.txt", "r", encoding="utf-8") as file:
8
+ knowledge = file.read()
9
+ cleaned_chunks = [chunk.strip() for chunk in knowledge.strip().split("\n") if chunk.strip()]
10
+ model = SentenceTransformer('all-MiniLM-L6-v2')
11
+ chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
12
+ def get_top_chunks(query):
13
+ query_embedding = model.encode(query, convert_to_tensor=True)
14
+ query_embedding_normalized = query_embedding / query_embedding.norm()
15
+ similarities = torch.matmul(chunk_embeddings, query_embedding_normalized)
16
+ top_indices = torch.topk(similarities, k=5).indices.tolist()
17
+ return [cleaned_chunks[i] for i in top_indices]
18
+ client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
19
+ def respond(message, history, cuisine, dietary_restrictions, allergies):
20
+ response = ""
21
+ top_chunks = get_top_chunks(message)
22
+ context = "\n".join(top_chunks)
23
+ print (top_chunks)
24
+ messages = [
25
+ {
26
+ "role": "system",
27
+ "content": f"You are a friendly recipe chatbot named BiteBot that responds to the user with any recipe from this: {context}. Find a recipe that is {cuisine} cuisine. They have the dietary restrictions,{dietary_restrictions} and are allergic to {allergies}. For example, you can say Based on your preference for something sweet and given the recipes you provided, let me suggest a recipe that might be of interest to you. Do you want to try Elizabeth's Sweet Potato Casserole? Return the title to the user and ask if this is the recipe they want. If they say yes return the ingredients to the user and ask them if they want the instructions to this recipe (give them the instructions next if they say yes to this). If they say no ask if they want another recipe. Only pull recipes from the file provided."
28
+ }
29
+ ]
30
+ if history:
31
+ messages.extend(history)
32
+ messages.append({"role": "user", "content": message})
33
+ stream = client.chat_completion(
34
+ messages,
35
+ max_tokens=700,
36
+ temperature=1.5,top_p=0.7,
37
+ stream=True,
38
+ )
39
+ for message in stream:
40
+ token = message.choices[0].delta.content
41
+ if token is not None:
42
+ response += token
43
+ yield response
44
+
45
+ logo="banner.png"
46
+
47
+ theme = gr.themes.Monochrome(
48
+ primary_hue="orange",
49
+ secondary_hue="zinc",
50
+ 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)"),
51
+ text_size="sm",
52
+ spacing_size="md",
53
+ radius_size="sm",
54
+ ).set(
55
+ body_background_fill='*primary_50',
56
+ body_background_fill_dark='*primary_50'
57
+ )
58
+
59
+
60
+ with gr.Blocks(theme=theme) as chatbot:
61
+ gr.Image(
62
+ value="Henrietta.png",
63
+ show_label=False,
64
+ show_share_button = False,
65
+ show_download_button = False)
66
+ gr.Markdown("### 👋 Welcome to Henrietta the Chatbot!\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- _“Give me something vegan and Indian.”_\n- _“I’m allergic to nuts—what can I eat?”_")
67
+ cuisine=gr.Textbox(label="cuisine")
68
+ 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!")
69
+ allergies=gr.Textbox(label="allergies")
70
+ gr.ChatInterface(
71
+ fn=respond,
72
+ type="messages", additional_inputs=[cuisine,dietary_restrictions,allergies]
73
+ )
74
+
75
+ chatbot.launch()