naxemCDA commited on
Commit
6246831
·
1 Parent(s): 7c9b96e
Files changed (2) hide show
  1. app.py +19 -38
  2. requirements.txt +3 -7
app.py CHANGED
@@ -1,21 +1,22 @@
1
- # -*- coding: utf-8 -*-
2
- # app.py
3
-
4
  import gradio as gr
5
- from transformers import pipeline
6
- from diffusers import StableDiffusionPipeline
7
- import torch
8
  import requests
9
  import re
10
  import os
11
  import json
12
  from reportlab.lib.pagesizes import letter
13
  from reportlab.pdfgen import canvas
14
- from tempfile import NamedTemporaryFile
15
  from gtts import gTTS
16
- import spaces # For ZeroGPU preload
17
 
18
- # === USDA API Setup ===
 
 
 
 
 
 
 
19
  USDA_API_KEY = "gcwe1gXmMveg7buqddggl6wAZa7Sd7wrZV87P31z"
20
  USDA_SEARCH_URL = "https://api.nal.usda.gov/fdc/v1/foods/search"
21
 
@@ -37,27 +38,7 @@ def get_calories(ingredient):
37
  nutrients = get_nutrients(ingredient)
38
  return nutrients.get("Energy", 0.0)
39
 
40
- # === Globals for Models ===
41
- recipe_model = None
42
- image_model = None
43
- device = "cuda" if torch.cuda.is_available() else "cpu"
44
-
45
- # === Preload Models ===
46
- @spaces.GPU
47
- def preload():
48
- global recipe_model, image_model
49
- print(f"🚀 Preloading models on {device}...")
50
-
51
- recipe_model = pipeline("text-generation", model="samdak93/qrit-2", device=0 if device == "cuda" else -1)
52
-
53
- image_model = StableDiffusionPipeline.from_pretrained(
54
- "runwayml/stable-diffusion-v1-5",
55
- torch_dtype=torch.float16 if device == "cuda" else torch.float32
56
- ).to(device)
57
-
58
- print("✅ Models preloaded successfully.")
59
-
60
- # === Recipe Generation Utilities ===
61
  forbidden = ["pork", "bacon", "ham", "lard", "gelatin", "alcohol", "beer", "wine", "rum", "whiskey", "vodka", "gin"]
62
 
63
  def parse_ingredients(text):
@@ -70,7 +51,7 @@ def parse_ingredients(text):
70
  def generate_recipe(key_ingredient):
71
  for _ in range(5):
72
  prompt = f"Create a halal recipe under 2000 calories that includes {key_ingredient}.\nIngredients:"
73
- out = recipe_model(prompt, max_length=300, num_return_sequences=1)[0]['generated_text']
74
  if any(h in out.lower() for h in forbidden):
75
  continue
76
 
@@ -87,8 +68,8 @@ def get_nutrient_breakdown(ingredients):
87
  return breakdown
88
 
89
  def generate_image(prompt):
90
- with torch.autocast(device):
91
- return image_model(prompt).images[0]
92
 
93
  def export_pdf(recipe_text, nutrients):
94
  temp_pdf = NamedTemporaryFile(delete=False, suffix=".pdf")
@@ -125,7 +106,6 @@ def generate_audio(recipe_text):
125
  tts.save(temp_audio.name)
126
  return temp_audio.name
127
 
128
- # === Core App Function ===
129
  def app(key_ingredient):
130
  recipe_text, ingredients, calories = generate_recipe(key_ingredient)
131
  if not ingredients:
@@ -141,9 +121,9 @@ def app(key_ingredient):
141
  display_text = f"{recipe_text}\n\nEstimated Calories: {int(calories)} kcal"
142
  return display_text, image, nutrients, pdf_path, audio_path
143
 
144
- # === Gradio Interface ===
145
  with gr.Blocks() as demo:
146
- gr.Markdown("## 🕌 Halal Recipe Generator - Advanced Edition")
147
  with gr.Row():
148
  key_input = gr.Textbox(label="Key Ingredient")
149
  submit_btn = gr.Button("Generate Recipe")
@@ -157,5 +137,6 @@ with gr.Blocks() as demo:
157
  submit_btn.click(app, inputs=key_input,
158
  outputs=[output_text, output_img, output_table, output_pdf, output_audio])
159
 
160
- # Launch app
161
- demo.launch(share=True)
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
 
 
3
  import requests
4
  import re
5
  import os
6
  import json
7
  from reportlab.lib.pagesizes import letter
8
  from reportlab.pdfgen import canvas
 
9
  from gtts import gTTS
10
+ from tempfile import NamedTemporaryFile
11
 
12
+ # --- Load HF Token from environment ---
13
+ HF_TOKEN = os.getenv("HF_TOKEN")
14
+
15
+ # --- Inference Clients ---
16
+ text_client = InferenceClient("samdak93/qrit-2", token=HF_TOKEN)
17
+ image_client = InferenceClient("runwayml/stable-diffusion-v1-5", token=HF_TOKEN)
18
+
19
+ # --- USDA API Setup ---
20
  USDA_API_KEY = "gcwe1gXmMveg7buqddggl6wAZa7Sd7wrZV87P31z"
21
  USDA_SEARCH_URL = "https://api.nal.usda.gov/fdc/v1/foods/search"
22
 
 
38
  nutrients = get_nutrients(ingredient)
39
  return nutrients.get("Energy", 0.0)
40
 
41
+ # --- Utilities ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  forbidden = ["pork", "bacon", "ham", "lard", "gelatin", "alcohol", "beer", "wine", "rum", "whiskey", "vodka", "gin"]
43
 
44
  def parse_ingredients(text):
 
51
  def generate_recipe(key_ingredient):
52
  for _ in range(5):
53
  prompt = f"Create a halal recipe under 2000 calories that includes {key_ingredient}.\nIngredients:"
54
+ out = text_client.text_generation(prompt, max_new_tokens=300)
55
  if any(h in out.lower() for h in forbidden):
56
  continue
57
 
 
68
  return breakdown
69
 
70
  def generate_image(prompt):
71
+ image = image_client.text_to_image(prompt)
72
+ return image
73
 
74
  def export_pdf(recipe_text, nutrients):
75
  temp_pdf = NamedTemporaryFile(delete=False, suffix=".pdf")
 
106
  tts.save(temp_audio.name)
107
  return temp_audio.name
108
 
 
109
  def app(key_ingredient):
110
  recipe_text, ingredients, calories = generate_recipe(key_ingredient)
111
  if not ingredients:
 
121
  display_text = f"{recipe_text}\n\nEstimated Calories: {int(calories)} kcal"
122
  return display_text, image, nutrients, pdf_path, audio_path
123
 
124
+ # --- Gradio UI ---
125
  with gr.Blocks() as demo:
126
+ gr.Markdown("## 🍏 Halal Recipe Generator - Hugging Face Spaces Edition")
127
  with gr.Row():
128
  key_input = gr.Textbox(label="Key Ingredient")
129
  submit_btn = gr.Button("Generate Recipe")
 
137
  submit_btn.click(app, inputs=key_input,
138
  outputs=[output_text, output_img, output_table, output_pdf, output_audio])
139
 
140
+ if __name__ == '__main__':
141
+ demo.launch()
142
+
requirements.txt CHANGED
@@ -1,10 +1,6 @@
1
- gradio>=4.0
2
- transformers>=4.35.0
3
- diffusers>=0.24.0
4
- accelerate>=0.23.0
5
- torch>=2.1.0
6
  reportlab
7
  gTTS
8
- requests
9
- spaces
10
 
 
1
+ gradio
2
+ huggingface_hub
3
+ requests
 
 
4
  reportlab
5
  gTTS
 
 
6