DurgaDeepak commited on
Commit
4d89c61
·
verified ·
1 Parent(s): 5e2b87c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -57
app.py CHANGED
@@ -1,62 +1,48 @@
1
  # app.py
2
  import gradio as gr
3
  import spaces
4
- from transformers import pipeline
5
- import os
6
- #from PyPDF2 import PdfReader
7
- from dotenv import load_dotenv
8
-
9
- load_dotenv()
10
 
11
  @spaces.GPU
12
- class ChatBot:
13
- def __init__(self):
14
- self.llm = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1", token=os.getenv("HF_TOKEN"))
15
- self.context = ""
16
-
17
- def read_meal_plans(self, folder="meal_plans"):
18
- text = ""
19
- for file in os.listdir(folder):
20
- if file.endswith(".pdf"):
21
- reader = PdfReader(os.path.join(folder, file))
22
- for page in reader.pages:
23
- text += page.extract_text() + "\n"
24
- return text
25
-
26
- def reply(self, message, history, preferences):
27
- diet, goal, allergens = preferences
28
- if not self.context:
29
- mealplan_text = self.read_meal_plans()
30
- self.context = f"Meal Plans: {mealplan_text}\nUser Preferences: Diet={diet}, Goal={goal}, Allergens={allergens}"
31
-
32
- prompt = f"{self.context}\nUser: {message}\nAI:"
33
- response = self.llm(prompt, max_new_tokens=100, do_sample=True, temperature=0.7)[0]['generated_text'].split("AI:")[-1].strip()
34
- return response
35
-
36
- bot = ChatBot()
37
-
38
- def chat(message, history, diet, goal, allergens):
39
- return bot.reply(message, history, (diet, goal, allergens))
40
-
41
- diet_choices = ["Vegetarian", "Vegan", "Keto", "Paleo", "No Preference"]
42
- goal_choices = ["Weight Loss", "Muscle Gain", "Maintenance"]
43
- allergen_choices = ["Nuts", "Dairy", "Gluten", "Soy", "Eggs"]
44
-
45
- with gr.Blocks() as demo:
46
- gr.Markdown("# 🥗 AI Meal Plan Assistant")
47
- with gr.Row():
48
- diet = gr.Dropdown(diet_choices, label="Diet Type")
49
- goal = gr.Dropdown(goal_choices, label="Goal")
50
- allergens = gr.CheckboxGroup(allergen_choices, label="Allergies")
51
- chatbot = gr.Chatbot()
52
- msg = gr.Textbox(placeholder="Ask me for meal ideas...", label="Message")
53
- send = gr.Button("Send")
54
-
55
- def user_input(message, chat_history):
56
- response = chat(message, chat_history, diet.value, goal.value, allergens.value)
57
- chat_history.append((message, response))
58
- return chat_history, ""
59
-
60
- send.click(user_input, [msg, chatbot], [chatbot, msg])
61
-
62
- demo.launch()
 
1
  # app.py
2
  import gradio as gr
3
  import spaces
4
+ from agent import generate_response
 
 
 
 
 
5
 
6
  @spaces.GPU
7
+ def main():
8
+ with gr.Blocks(title="Meal Plan Chatbot") as demo:
9
+ gr.Markdown("# 🍎 MealBot: Your AI Nutritionist")
10
+
11
+ with gr.Row():
12
+ diet_type = gr.CheckboxGroup(
13
+ label="Diet Type",
14
+ choices=["Vegan", "Keto", "Vegetarian", "Paleo", "High-Protein"],
15
+ value=["Vegetarian"]
16
+ )
17
+ goal = gr.CheckboxGroup(
18
+ label="Your Goal",
19
+ choices=["Weight Loss", "Muscle Gain", "Maintenance"],
20
+ value=["Weight Loss"]
21
+ )
22
+ duration = gr.CheckboxGroup(
23
+ label="Duration",
24
+ choices=["1 week", "4 weeks", "8 weeks"],
25
+ value=["4 weeks"]
26
+ )
27
+
28
+ chatbot = gr.Chatbot()
29
+ msg = gr.Textbox(placeholder="Ask me anything about meal plans...")
30
+ clear = gr.Button("Clear")
31
+
32
+ def user_respond(message, chat_history):
33
+ prefs = {
34
+ "diet": diet_type.value,
35
+ "goal": goal.value,
36
+ "duration": duration.value
37
+ }
38
+ response = generate_response(message, prefs)
39
+ chat_history.append((message, response))
40
+ return "", chat_history
41
+
42
+ msg.submit(user_respond, [msg, chatbot], [msg, chatbot])
43
+ clear.click(lambda: None, None, chatbot, queue=False)
44
+
45
+ demo.launch()
46
+
47
+ if __name__ == "__main__":
48
+ main()