Add application and requirements files
Browse files- app.py +30 -21
- requirements.txt +7 -5
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
-
from spaces import GPU
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import pipeline
|
| 6 |
from diffusers import StableDiffusionPipeline
|
|
@@ -13,21 +13,9 @@ from reportlab.lib.pagesizes import letter
|
|
| 13 |
from reportlab.pdfgen import canvas
|
| 14 |
from tempfile import NamedTemporaryFile
|
| 15 |
from gtts import gTTS
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
@GPU
|
| 19 |
-
def preload():
|
| 20 |
-
global recipe_model, image_model
|
| 21 |
-
|
| 22 |
-
recipe_model = pipeline("text-generation", model="samdak93/qrit-2")
|
| 23 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
-
image_model = StableDiffusionPipeline.from_pretrained(
|
| 25 |
-
"runwayml/stable-diffusion-v1-5",
|
| 26 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 27 |
-
).to(device)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# --- USDA API Setup ---
|
| 31 |
USDA_API_KEY = "gcwe1gXmMveg7buqddggl6wAZa7Sd7wrZV87P31z"
|
| 32 |
USDA_SEARCH_URL = "https://api.nal.usda.gov/fdc/v1/foods/search"
|
| 33 |
|
|
@@ -49,14 +37,34 @@ def get_calories(ingredient):
|
|
| 49 |
nutrients = get_nutrients(ingredient)
|
| 50 |
return nutrients.get("Energy", 0.0)
|
| 51 |
|
| 52 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
forbidden = ["pork", "bacon", "ham", "lard", "gelatin", "alcohol", "beer", "wine", "rum", "whiskey", "vodka", "gin"]
|
| 54 |
|
| 55 |
def parse_ingredients(text):
|
| 56 |
match = re.search(r"(?i)ingredients:(.*?)(\n|directions:|instructions:|$)", text, re.DOTALL)
|
| 57 |
if match:
|
| 58 |
ingredients_block = match.group(1).strip()
|
| 59 |
-
return
|
| 60 |
return []
|
| 61 |
|
| 62 |
def generate_recipe(key_ingredient):
|
|
@@ -79,7 +87,6 @@ def get_nutrient_breakdown(ingredients):
|
|
| 79 |
return breakdown
|
| 80 |
|
| 81 |
def generate_image(prompt):
|
| 82 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 83 |
with torch.autocast(device):
|
| 84 |
return image_model(prompt).images[0]
|
| 85 |
|
|
@@ -118,6 +125,7 @@ def generate_audio(recipe_text):
|
|
| 118 |
tts.save(temp_audio.name)
|
| 119 |
return temp_audio.name
|
| 120 |
|
|
|
|
| 121 |
def app(key_ingredient):
|
| 122 |
recipe_text, ingredients, calories = generate_recipe(key_ingredient)
|
| 123 |
if not ingredients:
|
|
@@ -133,9 +141,9 @@ def app(key_ingredient):
|
|
| 133 |
display_text = f"{recipe_text}\n\nEstimated Calories: {int(calories)} kcal"
|
| 134 |
return display_text, image, nutrients, pdf_path, audio_path
|
| 135 |
|
| 136 |
-
#
|
| 137 |
with gr.Blocks() as demo:
|
| 138 |
-
gr.Markdown("## 🕌 Halal Recipe Generator - Advanced Edition
|
| 139 |
with gr.Row():
|
| 140 |
key_input = gr.Textbox(label="Key Ingredient")
|
| 141 |
submit_btn = gr.Button("Generate Recipe")
|
|
@@ -149,4 +157,5 @@ with gr.Blocks() as demo:
|
|
| 149 |
submit_btn.click(app, inputs=key_input,
|
| 150 |
outputs=[output_text, output_img, output_table, output_pdf, output_audio])
|
| 151 |
|
| 152 |
-
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
# app.py
|
| 3 |
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import pipeline
|
| 6 |
from diffusers import StableDiffusionPipeline
|
|
|
|
| 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 |
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):
|
| 64 |
match = re.search(r"(?i)ingredients:(.*?)(\n|directions:|instructions:|$)", text, re.DOTALL)
|
| 65 |
if match:
|
| 66 |
ingredients_block = match.group(1).strip()
|
| 67 |
+
return [line.strip("- *") for line in ingredients_block.split("\n") if line.strip()]
|
| 68 |
return []
|
| 69 |
|
| 70 |
def generate_recipe(key_ingredient):
|
|
|
|
| 87 |
return breakdown
|
| 88 |
|
| 89 |
def generate_image(prompt):
|
|
|
|
| 90 |
with torch.autocast(device):
|
| 91 |
return image_model(prompt).images[0]
|
| 92 |
|
|
|
|
| 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 |
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 |
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)
|
requirements.txt
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
transformers
|
| 3 |
-
diffusers
|
| 4 |
-
accelerate
|
| 5 |
-
torch
|
| 6 |
reportlab
|
| 7 |
gTTS
|
| 8 |
requests
|
|
|
|
|
|
|
|
|
| 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 |
+
|