RG_PPLX_LdMd
Browse files- app.py +24 -17
- requirements.txt +6 -2
app.py
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import spaces # For @spaces.GPU decorator
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
import requests
|
| 6 |
import re
|
| 7 |
import os
|
| 8 |
import json
|
| 9 |
from reportlab.lib.pagesizes import letter
|
| 10 |
from reportlab.pdfgen import canvas
|
| 11 |
-
from gtts import gTTS
|
| 12 |
from tempfile import NamedTemporaryFile
|
| 13 |
-
|
| 14 |
-
# --- Load HF Token from environment ---
|
| 15 |
-
HF_TOKEN = os.getenv("HF_FINEGRAIN_TOKEN")
|
| 16 |
-
|
| 17 |
-
# --- Inference Clients ---
|
| 18 |
-
text_client = InferenceClient("samdak93/qrit-2", token=HF_TOKEN)
|
| 19 |
-
image_client = InferenceClient("runwayml/stable-diffusion-v1-5", token=HF_TOKEN)
|
| 20 |
|
| 21 |
# --- USDA API Setup ---
|
| 22 |
USDA_API_KEY = "gcwe1gXmMveg7buqddggl6wAZa7Sd7wrZV87P31z"
|
|
@@ -40,6 +36,16 @@ def get_calories(ingredient):
|
|
| 40 |
nutrients = get_nutrients(ingredient)
|
| 41 |
return nutrients.get("Energy", 0.0)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# --- Utilities ---
|
| 44 |
forbidden = ["pork", "bacon", "ham", "lard", "gelatin", "alcohol", "beer", "wine", "rum", "whiskey", "vodka", "gin"]
|
| 45 |
|
|
@@ -53,7 +59,7 @@ def parse_ingredients(text):
|
|
| 53 |
def generate_recipe(key_ingredient):
|
| 54 |
for _ in range(5):
|
| 55 |
prompt = f"Create a halal recipe under 2000 calories that includes {key_ingredient}.\nIngredients:"
|
| 56 |
-
out =
|
| 57 |
if any(h in out.lower() for h in forbidden):
|
| 58 |
continue
|
| 59 |
|
|
@@ -69,10 +75,10 @@ def get_nutrient_breakdown(ingredients):
|
|
| 69 |
breakdown[ing] = get_nutrients(ing)
|
| 70 |
return breakdown
|
| 71 |
|
| 72 |
-
@spaces.GPU
|
| 73 |
def generate_image(prompt):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
def export_pdf(recipe_text, nutrients):
|
| 78 |
temp_pdf = NamedTemporaryFile(delete=False, suffix=".pdf")
|
|
@@ -103,6 +109,7 @@ def export_pdf(recipe_text, nutrients):
|
|
| 103 |
c.save()
|
| 104 |
return temp_pdf.name
|
| 105 |
|
|
|
|
| 106 |
def generate_audio(recipe_text):
|
| 107 |
tts = gTTS(text=recipe_text)
|
| 108 |
temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")
|
|
@@ -116,6 +123,7 @@ def app(key_ingredient):
|
|
| 116 |
|
| 117 |
title_match = re.search(r"(?i)^title: (.+)$", recipe_text, re.MULTILINE)
|
| 118 |
title = title_match.group(1).strip() if title_match else f"Dish with {key_ingredient}"
|
|
|
|
| 119 |
image = generate_image(f"A delicious {title}, beautifully plated")
|
| 120 |
nutrients = get_nutrient_breakdown(ingredients)
|
| 121 |
pdf_path = export_pdf(recipe_text, nutrients)
|
|
@@ -126,7 +134,7 @@ def app(key_ingredient):
|
|
| 126 |
|
| 127 |
# --- Gradio UI ---
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
-
gr.Markdown("##
|
| 130 |
with gr.Row():
|
| 131 |
key_input = gr.Textbox(label="Key Ingredient")
|
| 132 |
submit_btn = gr.Button("Generate Recipe")
|
|
@@ -140,6 +148,5 @@ with gr.Blocks() as demo:
|
|
| 140 |
submit_btn.click(app, inputs=key_input,
|
| 141 |
outputs=[output_text, output_img, output_table, output_pdf, output_audio])
|
| 142 |
|
| 143 |
-
|
| 144 |
-
demo.launch()
|
| 145 |
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
+
import spaces # Import spaces for ZeroGPU decorator
|
|
|
|
| 4 |
|
| 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 |
|
| 17 |
# --- USDA API Setup ---
|
| 18 |
USDA_API_KEY = "gcwe1gXmMveg7buqddggl6wAZa7Sd7wrZV87P31z"
|
|
|
|
| 36 |
nutrients = get_nutrients(ingredient)
|
| 37 |
return nutrients.get("Energy", 0.0)
|
| 38 |
|
| 39 |
+
# --- Models ---
|
| 40 |
+
recipe_model = pipeline("text-generation", model="samdak93/qrit-2")
|
| 41 |
+
|
| 42 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 43 |
+
|
| 44 |
+
image_model = StableDiffusionPipeline.from_pretrained(
|
| 45 |
+
"runwayml/stable-diffusion-v1-5",
|
| 46 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 47 |
+
).to(device)
|
| 48 |
+
|
| 49 |
# --- Utilities ---
|
| 50 |
forbidden = ["pork", "bacon", "ham", "lard", "gelatin", "alcohol", "beer", "wine", "rum", "whiskey", "vodka", "gin"]
|
| 51 |
|
|
|
|
| 59 |
def generate_recipe(key_ingredient):
|
| 60 |
for _ in range(5):
|
| 61 |
prompt = f"Create a halal recipe under 2000 calories that includes {key_ingredient}.\nIngredients:"
|
| 62 |
+
out = recipe_model(prompt, max_length=300, num_return_sequences=1)[0]['generated_text']
|
| 63 |
if any(h in out.lower() for h in forbidden):
|
| 64 |
continue
|
| 65 |
|
|
|
|
| 75 |
breakdown[ing] = get_nutrients(ing)
|
| 76 |
return breakdown
|
| 77 |
|
| 78 |
+
@spaces.GPU(duration=120) # Decorate for ZeroGPU with 120s duration
|
| 79 |
def generate_image(prompt):
|
| 80 |
+
with torch.autocast(device):
|
| 81 |
+
return image_model(prompt).images[0]
|
| 82 |
|
| 83 |
def export_pdf(recipe_text, nutrients):
|
| 84 |
temp_pdf = NamedTemporaryFile(delete=False, suffix=".pdf")
|
|
|
|
| 109 |
c.save()
|
| 110 |
return temp_pdf.name
|
| 111 |
|
| 112 |
+
@spaces.GPU # Decorate audio generation if needed; can be CPU but GPU decorator is safe
|
| 113 |
def generate_audio(recipe_text):
|
| 114 |
tts = gTTS(text=recipe_text)
|
| 115 |
temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")
|
|
|
|
| 123 |
|
| 124 |
title_match = re.search(r"(?i)^title: (.+)$", recipe_text, re.MULTILINE)
|
| 125 |
title = title_match.group(1).strip() if title_match else f"Dish with {key_ingredient}"
|
| 126 |
+
|
| 127 |
image = generate_image(f"A delicious {title}, beautifully plated")
|
| 128 |
nutrients = get_nutrient_breakdown(ingredients)
|
| 129 |
pdf_path = export_pdf(recipe_text, nutrients)
|
|
|
|
| 134 |
|
| 135 |
# --- Gradio UI ---
|
| 136 |
with gr.Blocks() as demo:
|
| 137 |
+
gr.Markdown("## 🕌 Halal Recipe Generator - Advanced Edition")
|
| 138 |
with gr.Row():
|
| 139 |
key_input = gr.Textbox(label="Key Ingredient")
|
| 140 |
submit_btn = gr.Button("Generate Recipe")
|
|
|
|
| 148 |
submit_btn.click(app, inputs=key_input,
|
| 149 |
outputs=[output_text, output_img, output_table, output_pdf, output_audio])
|
| 150 |
|
| 151 |
+
demo.launch(share=True, pwa=True, debug=True)
|
|
|
|
| 152 |
|
requirements.txt
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
|
|
| 1 |
gradio
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
reportlab
|
| 5 |
gTTS
|
|
|
|
| 6 |
|
|
|
|
| 1 |
+
|
| 2 |
gradio
|
| 3 |
+
transformers
|
| 4 |
+
diffusers
|
| 5 |
+
accelerate
|
| 6 |
+
torch
|
| 7 |
reportlab
|
| 8 |
gTTS
|
| 9 |
+
requests
|
| 10 |
|