File size: 7,552 Bytes
015cb5f
 
e22a328
 
 
 
 
015cb5f
 
e22a328
5d79b10
e22a328
015cb5f
 
 
 
5d79b10
 
 
 
 
 
 
015cb5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e22a328
015cb5f
 
 
 
 
 
 
 
e22a328
 
015cb5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e22a328
 
015cb5f
 
 
 
 
 
 
 
e22a328
 
 
 
015cb5f
 
 
a516c6b
 
e22a328
a516c6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e22a328
7028837
a516c6b
 
 
 
 
 
 
 
e22a328
 
a516c6b
 
 
 
 
 
015cb5f
 
 
a516c6b
 
 
 
e22a328
a516c6b
 
015cb5f
 
 
 
 
 
 
 
 
a516c6b
 
 
 
015cb5f
 
 
 
e22a328
015cb5f
 
 
 
 
e22a328
a516c6b
e22a328
a516c6b
015cb5f
 
 
 
 
 
a516c6b
 
 
e22a328
a516c6b
e22a328
 
 
015cb5f
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import warnings
warnings.filterwarnings("ignore")

import gradio as gr
import pandas as pd
import numpy as np
import os
import json
from sentence_transformers import SentenceTransformer
from sklearn.linear_model import LogisticRegression
from huggingface_hub import InferenceClient, hf_hub_download

# ============================================
# 1. Load Dataset from HuggingFace
# ============================================
print("Loading dataset from HuggingFace...")

csv_path = hf_hub_download(
    repo_id="adigabay2003/smartchef-recipes",
    filename="smartchef_dataset.csv",
    repo_type="dataset"
)
df = pd.read_csv(csv_path)
df = df.dropna(subset=["text"])
df = df[df["text"].str.strip() != ""]
df = df.reset_index(drop=True)
print(f"Loaded {len(df)} recipes")

# ============================================
# 2. Load Optimal Model from JSON
# ============================================
with open("optimal_model.json", "r") as f:
    optimal = json.load(f)

MODEL_NAME = optimal["model_name"]
print(f"Using embedding model: {MODEL_NAME}")

# ============================================
# 3. Load Precomputed Embeddings
# ============================================
print("Loading precomputed embeddings...")
X = np.load("best_embeddings.npy")
y = df["vibe"].tolist()
print(f"Embeddings shape: {X.shape}")

# ============================================
# 4. Train Classifier on Embeddings
# ============================================
print("Training classifier...")
embedder = SentenceTransformer(MODEL_NAME)
clf = LogisticRegression(max_iter=1000)
clf.fit(X, y)
print("Classifier ready!")

# ============================================
# 5. Prediction Function
# ============================================
def get_recipe_vibe(title, ingredients):
    text = f"{title}. Ingredients: {ingredients}"
    embedding = embedder.encode([text])
    vibe = clf.predict(embedding)[0]
    return vibe

# ============================================
# 6. Image Generation
# ============================================
def generate_food_image(title, vibe):
    token = os.getenv("HF_TOKEN")
    if not token:
        return None
    client = InferenceClient(
        model="stabilityai/stable-diffusion-xl-base-1.0",
        token=token
    )
    prompt = (
        f"Professional food photography of {title}, "
        f"{vibe} style, 4k, highly detailed, "
        f"appetizing, dramatic lighting, vibrant colors"
    )
    try:
        return client.text_to_image(prompt)
    except:
        return None

# ============================================
# 7. Main App Function
# ============================================
def smart_chef_app(title, ingredients):
    vibe = get_recipe_vibe(title, ingredients)
    messages = {
        "Romantic":     "๐ŸŒน Love is in the air!",
        "Quick Lunch":  "โšก Fast & Delicious!",
        "Comfort Food": "๐Ÿงธ Warm & Cozy.",
        "Party Snack":  "๐ŸŽ‰ Party Time!",
        "Healthy Boost":"๐Ÿฅ— Feel Good Food.",
        "Fancy Dinner": "๐Ÿท Chef's Kiss!"
    }
    msg = messages.get(vibe, "Yum!")
    img = generate_food_image(title, vibe)
    return vibe, msg, img

# ============================================
# 8. CSS & Theme
# ============================================
ultra_css = """
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;500;800&display=swap');
.gradio-container {
    background: linear-gradient(-45deg, #0f0c29, #302b63, #24243e, #4a1c1c);
    background-size: 400% 400%;
    animation: gradientBG 15s ease infinite;
    font-family: 'Montserrat', sans-serif !important;
}
@keyframes gradientBG {
    0% {background-position: 0% 50%;}
    50% {background-position: 100% 50%;}
    100% {background-position: 0% 50%;}
}
h1 {
    background: linear-gradient(to right, #ffcc00, #ff6600, #ff3300);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0px 0px 15px rgba(255, 102, 0, 0.6);
    font-weight: 800 !important;
    text-align: center;
    font-size: 3.5em !important;
    margin-bottom: 10px !important;
}
h3 { color: #e0e0e0 !important; text-align: center; font-weight: 300; }
.group-container {
    background: rgba(255, 255, 255, 0.03);
    backdrop-filter: blur(20px);
    border-radius: 25px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.5);
    padding: 30px !important;
    transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
}
.group-container:hover {
    transform: translateY(-10px) scale(1.02);
    border: 1px solid rgba(255, 204, 0, 0.4);
    box-shadow: 0 20px 50px rgba(255, 102, 0, 0.3);
}
button.primary-btn {
    background: linear-gradient(135deg, #ffcc00, #ff6600) !important;
    border: none !important;
    color: #000 !important;
    font-weight: 800 !important;
    font-size: 1.3em !important;
    padding: 15px 30px !important;
    animation: pulse 2s infinite;
}
@keyframes pulse {
    0% {box-shadow: 0 0 0 0 rgba(255, 102, 0, 0.7);}
    70% {box-shadow: 0 0 0 15px rgba(255, 102, 0, 0);}
    100% {box-shadow: 0 0 0 0 rgba(255, 102, 0, 0);}
}
label { color: #ffcc00 !important; font-weight: 600 !important; }
span { color: #e0e0e0 !important; }
textarea, input {
    background-color: rgba(0,0,0,0.4) !important;
    border: 1px solid rgba(255,255,255,0.1) !important;
    color: white !important;
}
.gradio-container > * { animation: fadeUp 0.8s ease-out forwards; opacity: 0; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(30px); } to { opacity: 1; transform: translateY(0); } }
"""

theme = gr.themes.Soft(primary_hue="orange", neutral_hue="slate").set(
    body_background_fill="#000000",
    block_background_fill="#121212",
    block_border_width="0px"
)

# ============================================
# 9. Gradio UI
# ============================================
with gr.Blocks(css=ultra_css, theme=theme) as demo:
    gr.Markdown("# โœจ SMARTCHEF AI โœจ")
    gr.Markdown("### Experience the future of culinary magic.")

    with gr.Row():
        with gr.Column(elem_classes="group-container"):
            gr.Markdown("#### ๐Ÿ“ CREATE YOUR DISH")
            t_in = gr.Textbox(
                label="Dish Name",
                placeholder="e.g., Mystical Truffle Risotto"
            )
            i_in = gr.Textbox(
                label="Ingredients",
                placeholder="e.g., Arborio rice, black truffle dust, parmesan...",
                lines=4
            )
            btn = gr.Button("โœจ UNLEASH MAGIC โœจ", elem_classes="primary-btn")

        with gr.Column(elem_classes="group-container"):
            gr.Markdown("#### ๐Ÿ”ฎ THE REVELATION")
            v_out = gr.Textbox(
                label="Vibe Detected",
                interactive=False
            )
            m_out = gr.Markdown()
            im_out = gr.Image(
                label="AI Visualization",
                type="pil",
                interactive=False
            )

    gr.Markdown("### โšก Instant Inspirations (Click to try):")
    gr.Examples(
        examples=[
            ["Midnight Burger",
             "Black bun, wagyu beef, spicy aioli, caramelized onions"],
            ["Neon Sushi Roll",
             "Tuna, salmon, avocado, glowing tobiko, eel sauce"],
            ["Enchanted Forest Salad",
             "Kale, edible flowers, berries, nuts, fairy dust dressing"]
        ],
        inputs=[t_in, i_in],
        elem_id="examples-table"
    )

    btn.click(smart_chef_app, [t_in, i_in], [v_out, m_out, im_out])

if __name__ == "__main__":
    demo.launch()