adigabay2003 commited on
Commit
a516c6b
ยท
verified ยท
1 Parent(s): 3a39a25

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +132 -64
app.py CHANGED
@@ -9,10 +9,9 @@ from sklearn.pipeline import Pipeline
9
  from sklearn.linear_model import LogisticRegression
10
  from huggingface_hub import InferenceClient
11
 
12
- # --- 1. Hybrid Data Generation (High Volume - 10,000 Rows) ---
13
- # ืื ื—ื ื• ืžืฉืชืžืฉื™ื ื‘ืœื•ื’ื™ืงื” ื”ื”ื™ื‘ืจื™ื“ื™ืช ื›ื“ื™ ืœื™ื™ืฆืจ ื“ืื˜ื” ืขื ืง ื‘ื–ืžืŸ ืืžืช ื›ืฉื”ืืคืœื™ืงืฆื™ื” ืขื•ืœื”
14
  print("๐Ÿš€ Generating Synthetic Data...")
15
-
16
  proteins = ["Chicken", "Beef", "Tofu", "Salmon", "Shrimp", "Chickpeas", "Lentils", "Turkey", "Duck", "Egg"]
17
  carbs = ["Rice", "Pasta", "Quinoa", "Potatoes", "Bread", "Noodles", "Tortilla", "Bun", "Couscous"]
18
  veggies = ["Broccoli", "Spinach", "Carrots", "Tomatoes", "Mushrooms", "Onions", "Peppers", "Kale", "Avocado", "Corn"]
@@ -21,105 +20,174 @@ spices = ["Garlic", "Basil", "Chili", "Curry Powder", "Cumin", "Oregano", "Ginge
21
  adjectives = ["Spicy", "Creamy", "Roasted", "Grilled", "Fresh", "Zesty", "Hearty", "Crispy", "Savory", "Sweet"]
22
 
23
  def generate_recipe():
24
- p = random.choice(proteins)
25
- c = random.choice(carbs)
26
- v1 = random.choice(veggies)
27
- s = random.choice(sauces)
28
- sp = random.choice(spices)
29
- adj = random.choice(adjectives)
30
-
31
  title = f"{adj} {p} with {c}"
32
  ingredients = f"{p}, {c}, {v1}, {s}, {sp}"
33
-
34
- # Logic Rules for Labeling
35
  vibe = "Quick Lunch"
36
  if "Salmon" in p or "Shrimp" in p or "Duck" in p: vibe = "Fancy Dinner"
37
  elif "Chocolate" in ingredients or "Risotto" in title: vibe = "Romantic"
38
  elif "Tofu" in p or "Kale" in v1 or "Quinoa" in c: vibe = "Healthy Boost"
39
  elif "Tortilla" in c or "BBQ" in s or "Crispy" in title: vibe = "Party Snack"
40
  elif "Potatoes" in c or "Cream" in s or "Pasta" in c: vibe = "Comfort Food"
41
-
42
  return {"title": title, "ingredients": ingredients, "Vibe_AI": vibe}
43
 
44
- # Create 5,000 rows for the live app (fast training)
45
  data = [generate_recipe() for _ in range(5000)]
46
  df = pd.DataFrame(data)
47
-
48
- # --- 2. Model Training ---
49
- X = df['ingredients']
50
- y = df['Vibe_AI']
51
-
52
- pipeline = Pipeline([
53
- ('tfidf', TfidfVectorizer(stop_words='english')),
54
- ('clf', LogisticRegression(max_iter=1000))
55
- ])
56
  pipeline.fit(X, y)
57
- print("โœ… Model Trained on 5,000 synthetic records!")
58
 
59
- # --- 3. Functions ---
60
  def get_recipe_vibe(title, ingredients):
61
  full_text = f"{title} {ingredients}"
62
  return pipeline.predict([full_text])[0]
63
 
64
  def generate_food_image(title, vibe):
65
- # Fetch token from Spaces Secrets
66
  token = os.getenv("HF_TOKEN")
67
  if not token: return None
68
-
69
  client = InferenceClient(model="stabilityai/stable-diffusion-xl-base-1.0", token=token)
70
- prompt = f"Professional food photography of {title}, {vibe} style, 4k, highly detailed, appetizing, restaurant quality"
71
- try:
72
- return client.text_to_image(prompt)
73
- except:
74
- return None
75
 
76
  def smart_chef_app(title, ingredients):
77
  vibe = get_recipe_vibe(title, ingredients)
78
-
79
- messages = {
80
- "Romantic": "๐ŸŒน Love is in the air!",
81
- "Quick Lunch": "โšก Fast & Delicious!",
82
- "Comfort Food": "๐Ÿงธ Warm & Cozy.",
83
- "Party Snack": "๐ŸŽ‰ Party Time!",
84
- "Healthy Boost": "๐Ÿฅ— Feel Good Food.",
85
- "Fancy Dinner": "๐Ÿท Chef's Kiss!"
86
- }
87
-
88
  msg = messages.get(vibe, "Yum!")
89
  img = generate_food_image(title, vibe)
90
  return vibe, msg, img
91
 
92
- # --- 4. Premium UI Design ---
93
- premium_css = """
 
 
 
94
  .gradio-container {
95
- background: linear-gradient(rgba(0,0,0,0.8), rgba(0,0,0,0.8)), url('https://images.unsplash.com/photo-1556910103-1c02745a30bf?ixlib=rb-4.0.3&auto=format&fit=crop&w=1600&q=80');
96
- background-size: cover; background-position: center;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  }
98
- h1 {color: #ffcc00 !important; font-family: 'Helvetica Neue'; text-align: center; font-size: 3em !important; text-shadow: 2px 2px 4px black;}
99
- label {color: #ddd !important; font-weight: bold;}
100
- button.primary-btn {background: linear-gradient(45deg, #ff9900, #ffcc00) !important; color: black !important; font-weight: bold;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  """
102
 
103
- with gr.Blocks(css=premium_css, theme=gr.themes.Soft()) as demo:
104
- gr.Markdown("# ๐Ÿ‘จโ€๐Ÿณ SmartChef AI")
105
- gr.Markdown("### ๐Ÿš€ Powered by Synthetic Data (10k Rows) & Generative AI")
106
-
 
 
 
 
 
 
 
107
  with gr.Row():
108
- with gr.Column():
109
- t_in = gr.Textbox(label="Dish Name", placeholder="e.g. Truffle Risotto")
110
- i_in = gr.Textbox(label="Ingredients", placeholder="e.g. Rice, Mushrooms, Cream...", lines=3)
111
- btn = gr.Button("Cook & Visualize ๐Ÿณ", elem_classes="primary-btn")
112
-
113
- with gr.Column():
114
- v_out = gr.Label(label="Vibe Detected")
 
 
 
 
115
  m_out = gr.Markdown()
116
- im_out = gr.Image(label="AI Visualization")
117
 
 
118
  gr.Examples(
119
- examples=[["Avocado Toast", "Bread, Avocado, Chili"], ["Beef Wellington", "Beef, Pastry, Mushrooms"], ["Green Smoothie", "Kale, Apple, Ginger"]],
120
- inputs=[t_in, i_in]
 
 
 
 
 
121
  )
122
-
123
  btn.click(smart_chef_app, [t_in, i_in], [v_out, m_out, im_out])
124
 
125
  if __name__ == "__main__":
 
9
  from sklearn.linear_model import LogisticRegression
10
  from huggingface_hub import InferenceClient
11
 
12
+ # --- LIBRARIES & LOGIC (UNCHANGED) ---
13
+ # (ื›ืœ ื”ืœื•ื’ื™ืงื” ื ืฉืืจืช ื‘ื“ื™ื•ืง ืื•ืชื• ื“ื‘ืจ, ืจืง ืžื•ืกืชืจืช ื›ื“ื™ ืœื ืœื”ืคืจื™ืข ืœืขื™ื ื™ื™ื)
14
  print("๐Ÿš€ Generating Synthetic Data...")
 
15
  proteins = ["Chicken", "Beef", "Tofu", "Salmon", "Shrimp", "Chickpeas", "Lentils", "Turkey", "Duck", "Egg"]
16
  carbs = ["Rice", "Pasta", "Quinoa", "Potatoes", "Bread", "Noodles", "Tortilla", "Bun", "Couscous"]
17
  veggies = ["Broccoli", "Spinach", "Carrots", "Tomatoes", "Mushrooms", "Onions", "Peppers", "Kale", "Avocado", "Corn"]
 
20
  adjectives = ["Spicy", "Creamy", "Roasted", "Grilled", "Fresh", "Zesty", "Hearty", "Crispy", "Savory", "Sweet"]
21
 
22
  def generate_recipe():
23
+ p = random.choice(proteins); c = random.choice(carbs); v1 = random.choice(veggies); s = random.choice(sauces); sp = random.choice(spices); adj = random.choice(adjectives)
 
 
 
 
 
 
24
  title = f"{adj} {p} with {c}"
25
  ingredients = f"{p}, {c}, {v1}, {s}, {sp}"
 
 
26
  vibe = "Quick Lunch"
27
  if "Salmon" in p or "Shrimp" in p or "Duck" in p: vibe = "Fancy Dinner"
28
  elif "Chocolate" in ingredients or "Risotto" in title: vibe = "Romantic"
29
  elif "Tofu" in p or "Kale" in v1 or "Quinoa" in c: vibe = "Healthy Boost"
30
  elif "Tortilla" in c or "BBQ" in s or "Crispy" in title: vibe = "Party Snack"
31
  elif "Potatoes" in c or "Cream" in s or "Pasta" in c: vibe = "Comfort Food"
 
32
  return {"title": title, "ingredients": ingredients, "Vibe_AI": vibe}
33
 
 
34
  data = [generate_recipe() for _ in range(5000)]
35
  df = pd.DataFrame(data)
36
+ X = df['ingredients']; y = df['Vibe_AI']
37
+ pipeline = Pipeline([('tfidf', TfidfVectorizer(stop_words='english')),('clf', LogisticRegression(max_iter=1000))])
 
 
 
 
 
 
 
38
  pipeline.fit(X, y)
 
39
 
 
40
  def get_recipe_vibe(title, ingredients):
41
  full_text = f"{title} {ingredients}"
42
  return pipeline.predict([full_text])[0]
43
 
44
  def generate_food_image(title, vibe):
 
45
  token = os.getenv("HF_TOKEN")
46
  if not token: return None
 
47
  client = InferenceClient(model="stabilityai/stable-diffusion-xl-base-1.0", token=token)
48
+ prompt = f"Professional food photography of {title}, {vibe} style, 4k, highly detailed, appetizing, dramatic lighting, vibrant colors"
49
+ try: return client.text_to_image(prompt)
50
+ except: return None
 
 
51
 
52
  def smart_chef_app(title, ingredients):
53
  vibe = get_recipe_vibe(title, ingredients)
54
+ 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!"}
 
 
 
 
 
 
 
 
 
55
  msg = messages.get(vibe, "Yum!")
56
  img = generate_food_image(title, vibe)
57
  return vibe, msg, img
58
 
59
+ # --- THE NEW ULTRA-PREMIUM UI CSS ---
60
+ ultra_css = """
61
+ @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;500;800&display=swap');
62
+
63
+ /* ืื ื™ืžืฆื™ื™ืช ืจืงืข ื–ื–ื” */
64
  .gradio-container {
65
+ background: linear-gradient(-45deg, #0f0c29, #302b63, #24243e, #4a1c1c);
66
+ background-size: 400% 400%;
67
+ animation: gradientBG 15s ease infinite;
68
+ font-family: 'Montserrat', sans-serif !important;
69
+ }
70
+
71
+ @keyframes gradientBG {
72
+ 0% {background-position: 0% 50%;}
73
+ 50% {background-position: 100% 50%;}
74
+ 100% {background-position: 0% 50%;}
75
+ }
76
+
77
+ /* ื›ื•ืชืจืช ืจืืฉื™ืช ื–ื•ื”ืจืช */
78
+ h1 {
79
+ background: linear-gradient(to right, #ffcc00, #ff6600, #ff3300);
80
+ -webkit-background-clip: text;
81
+ -webkit-text-fill-color: transparent;
82
+ text-shadow: 0px 0px 15px rgba(255, 102, 0, 0.6);
83
+ font-weight: 800 !important;
84
+ text-align: center;
85
+ font-size: 3.5em !important;
86
+ letter-spacing: 2px;
87
+ margin-bottom: 10px !important;
88
+ }
89
+
90
+ h3 { color: #e0e0e0 !important; text-align: center; font-weight: 300; }
91
+
92
+ /* ื›ืจื˜ื™ืกื™ื•ืช ื–ื›ื•ื›ื™ืช ืžืจื—ืคื•ืช */
93
+ .group-container {
94
+ background: rgba(255, 255, 255, 0.03);
95
+ backdrop-filter: blur(20px);
96
+ -webkit-backdrop-filter: blur(20px);
97
+ border-radius: 25px;
98
+ border: 1px solid rgba(255, 255, 255, 0.1);
99
+ box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.5);
100
+ padding: 30px !important;
101
+ transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
102
+ }
103
+
104
+ .group-container:hover {
105
+ transform: translateY(-10px) scale(1.02);
106
+ border: 1px solid rgba(255, 204, 0, 0.4);
107
+ box-shadow: 0 20px 50px rgba(255, 102, 0, 0.3);
108
+ }
109
+
110
+ /* ื›ืคืชื•ืจ ืคืขื•ืœื” ืคื•ืขื */
111
+ button.primary-btn {
112
+ background: linear-gradient(135deg, #ffcc00, #ff6600) !important;
113
+ border: none !important;
114
+ border-radius: 50px !important;
115
+ color: #000 !important;
116
+ font-weight: 800 !important;
117
+ font-size: 1.3em !important;
118
+ padding: 15px 30px !important;
119
+ box-shadow: 0 10px 20px -10px rgba(255, 102, 0, 0.7);
120
+ transition: all 0.3s ease;
121
+ animation: pulse 2s infinite;
122
+ }
123
+
124
+ button.primary-btn:hover {
125
+ transform: scale(1.08);
126
+ box-shadow: 0 20px 30px -10px rgba(255, 102, 0, 1);
127
+ }
128
+
129
+ @keyframes pulse {
130
+ 0% {box-shadow: 0 0 0 0 rgba(255, 102, 0, 0.7);}
131
+ 70% {box-shadow: 0 0 0 15px rgba(255, 102, 0, 0);}
132
+ 100% {box-shadow: 0 0 0 0 rgba(255, 102, 0, 0);}
133
  }
134
+
135
+ /* ืืœืžื ื˜ื™ื ืฉืœ ื˜ืงืกื˜ */
136
+ label { color: #ffcc00 !important; font-weight: 600 !important; letter-spacing: 1px; }
137
+ span { color: #e0e0e0 !important; }
138
+ textarea, input {
139
+ background-color: rgba(0,0,0,0.4) !important;
140
+ border: 1px solid rgba(255,255,255,0.1) !important;
141
+ color: white !important;
142
+ border-radius: 10px !important;
143
+ }
144
+ textarea:focus, input:focus {
145
+ border-color: #ffcc00 !important;
146
+ box-shadow: 0 0 10px rgba(255, 204, 0, 0.5) !important;
147
+ }
148
+
149
+ /* ืื ื™ืžืฆื™ื™ืช ื›ื ื™ืกื” */
150
+ .gradio-container > * { animation: fadeUp 0.8s ease-out forwards; opacity: 0; }
151
+ @keyframes fadeUp { from { opacity: 0; transform: translateY(30px); } to { opacity: 1; transform: translateY(0); } }
152
  """
153
 
154
+ # ืฉื™ืžื•ืฉ ื‘ืขืจื›ืช ื ื•ืฉื ื›ื”ื” ืžืื•ื“ ื›ื‘ืกื™ืก
155
+ theme = gr.themes.Soft(primary_hue="orange", neutral_hue="slate").set(
156
+ body_background_fill="#000000",
157
+ block_background_fill="#121212",
158
+ block_border_width="0px"
159
+ )
160
+
161
+ with gr.Blocks(css=ultra_css, theme=theme) as demo:
162
+ gr.Markdown("# โœจ SMARTCHEF AI โœจ")
163
+ gr.Markdown("### Experience the future of culinary magic.")
164
+
165
  with gr.Row():
166
+ # ื”ื•ืกืคืชื™ ืืช elem_classes ื›ื“ื™ ืœื”ื—ื™ืœ ืืช ื”-CSS ื”ื—ื“ืฉ ืขืœ ื”ืขืžื•ื“ื•ืช
167
+ with gr.Column(elem_classes="group-container"):
168
+ gr.Markdown("#### ๐Ÿ“ CREATE YOUR DISH")
169
+ t_in = gr.Textbox(label="Dish Name", placeholder="e.g., Mystical Truffle Risotto")
170
+ i_in = gr.Textbox(label="Ingredients", placeholder="e.g., Arborio rice, black truffle dust, parmesan magic...", lines=4)
171
+ btn = gr.Button("โœจ UNLEASH MAGIC โœจ", elem_classes="primary-btn")
172
+
173
+ with gr.Column(elem_classes="group-container"):
174
+ gr.Markdown("#### ๐Ÿ”ฎ THE REVELATION")
175
+ # ืฉื™ื ื™ืชื™ ืืช ื”ืœื™ื™ื‘ืœื™ื ืฉื™ืจืื• ื›ืžื• ื›ืจื˜ื™ืกื™ื•ืช ืžื™ื“ืข
176
+ v_out = gr.Textbox(label="Vibe Detected", interactive=False, show_label=True)
177
  m_out = gr.Markdown()
178
+ im_out = gr.Image(label="AI Visualization", type="pil", interactive=False, show_download_button=False)
179
 
180
+ gr.Markdown("### โšก Instant Inspirations (Click to try):")
181
  gr.Examples(
182
+ examples=[
183
+ ["Midnight Burger", "Black bun, wagyu beef, spicy aioli, caramelized onions"],
184
+ ["Neon Sushi Roll", "Tuna, salmon, avocado, glowing tobiko, eel sauce"],
185
+ ["Enchanted Forest Salad", "Kale, edible flowers, berries, nuts, fairy dust dressing"]
186
+ ],
187
+ inputs=[t_in, i_in],
188
+ elem_id="examples-table"
189
  )
190
+
191
  btn.click(smart_chef_app, [t_in, i_in], [v_out, m_out, im_out])
192
 
193
  if __name__ == "__main__":