File size: 4,223 Bytes
073e8e8
f0b2a3a
f19d122
b9c7be3
edac0b4
073e8e8
1b35e03
1646317
d171510
 
 
 
f19d122
371509a
d171510
 
 
371509a
d171510
3cbe2db
371509a
 
 
 
d171510
1afdccf
1646317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0b2a3a
1646317
a1abcd1
d171510
1646317
 
 
 
 
 
 
 
 
 
 
 
 
d171510
1646317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371509a
4d5e6b4
 
1646317
 
d8422d7
4d5e6b4
1afdccf
f0b2a3a
531e192
 
 
f0b2a3a
 
 
 
 
 
edac0b4
cc828cd
 
 
 
edac0b4
 
239971d
1b35e03
3859856
1b35e03
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
99
100
101
102
103
104
105
106
107
108
109
import streamlit as st
import pandas as pd
import transformers
from transformers import pipeline, AutoTokenizer
import torch


# Load Hugging Face model (replace with your desired access token)
torch.manual_seed(0)
model = "tiiuae/falcon-7b-instruct"

tokenizer = AutoTokenizer.from_pretrained(model)
pipeline = pipeline(
    "text-generation", #task
    model=model,
    tokenizer=tokenizer,
    torch_dtype=torch.bfloat16,
    trust_remote_code=True,
    device_map="auto",
    max_length=500,
    do_sample=True,
    top_k=10,
    num_return_sequences=1,
    eos_token_id=tokenizer.eos_token_id
)
# Knowledge base data 
knowledge_base = {
    "health_conditions": [
        "diabetes",
        "heart disease",
        "high blood pressure",
        "kidney disease",
        "liver disease",
    ],
    "dietary_restrictions": ["vegetarian", "vegan", "gluten-free", "dairy-free"],
    "food_preferences": ["spicy", "low-carb", "high-protein", "Mediterranean"],
    "fruits": ["apple", "banana", "orange", "grapefruit", "strawberry"],
    "vegetables": ["broccoli", "spinach", "kale", "carrot", "tomato"],
    "whole_grains": ["brown rice", "quinoa", "oats", "whole-wheat bread", "barley"],
    "lean_proteins": ["chicken breast", "fish", "beans", "lentils", "tofu"],
    "healthy_fats": ["avocado", "nuts", "seeds", "olive oil"],
}

def get_prompt(name, age, gender, weight, height, body_type, health_conditions, dietary_restrictions, food_preferences):
    """
    Generates a prompt for model based on user input.

    Args:
        name (str): User's name.
        age (int): User's age.
        gender (str): User's gender.
        weight (float): User's weight (kg).
        height (float): User's height (meters).
        body_type (str): User's body type.
        health_conditions (list): User's health conditions (if any).
        dietary_restrictions (list): User's dietary restrictions (if any).
        food_preferences (list): User's food preferences (if any).

    Returns:
        str: The generated prompt.
    """

    prompt = f"""Based on the information provided about {name} (age: {age}, gender: {gender}, weight: {weight} kg, height: {height} m, body type: {body_type}), who has the following health conditions: {', '.join(health_conditions) if health_conditions else 'none'} and dietary restrictions: {', '.join(dietary_restrictions) if dietary_restrictions else 'none'}, what would be a personalized diet plan that considers their food preferences for {', '.join(food_preferences) if food_preferences else 'healthy eating'}?

**Knowledge:**

* Health conditions: {', '.join(knowledge_base['health_conditions'])}
* Dietary restrictions: {', '.join(knowledge_base['dietary_restrictions'])}
* Food preferences: {', '.join(knowledge_base['food_preferences'])}
* Fruits: {', '.join(knowledge_base['fruits'])}
* Vegetables: {', '.join(knowledge_base['vegetables'])}
* Whole grains: {', '.join(knowledge_base['whole_grains'])}
* Lean proteins: {', '.join(knowledge_base['lean_proteins'])}
* Healthy fats: {', '.join(knowledge_base['healthy_fats'])}
"""

    return prompt

def predict_diet(prompt):
    input_ids = tokenizer(prompt, return_tensors="pt")
    output = pipeline(prompt)
    predicted_text = output[0]['generated_text']
    return predicted_text



st.title("Personalized Diet Recommendation")

health_conditions = "No health conditions"
dietary_restrictions = "No dietary conditions"
food_preferences = "No food preferences conditions"
# Input fields
name = st.text_input("Name")
age = st.number_input("Age", min_value=0)
gender = st.selectbox("Gender", ["Male", "Female", "Non-binary"])
weight = st.number_input("Weight (kg)", min_value=0.0)
height = st.number_input("Height (meters)", min_value=0.0)
body_type = st.selectbox("Body Type", ["Ectomorph", "Mesomorph", "Endomorph"])
health_conditions = st.text_input("health_conditions")
dietary_restrictions = st.text_input("dietary_restrictions")
food_preferences = st.text_input("food_preferences")


if st.button("Generate"):
    op_prompt = get_prompt(name, age, gender, weight, height, body_type, health_conditions, dietary_restrictions, food_preferences)
    predected_text= predict_diet(op_prompt)
    st.write(predected_text)