naxemCDA commited on
Commit
f1de91b
·
1 Parent(s): b62963d

Add application and requirements files

Browse files
Files changed (1) hide show
  1. app.py +21 -39
app.py CHANGED
@@ -1,20 +1,6 @@
1
- # -*- coding: utf-8 -*-
2
- """2000CalRecipeGen_w_KeyIngr_Halal.ipynb
3
-
4
- Automatically generated by Colab.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1XfF-upk8LT3Gdb4i-yHTRC3X7IFPVrOJ
8
- """
9
-
10
- # Install dependencies
11
- #pyttsx3 below does not work; replaced with gTTS
12
- #!pip install gradio transformers diffusers accelerate torch reportlab pyttsx3 requests --quiet
13
-
14
- #below is active line
15
- #!pip install gradio transformers diffusers accelerate torch reportlab gTTS #requests --quiet
16
-
17
 
 
18
  import gradio as gr
19
  from transformers import pipeline
20
  from diffusers import StableDiffusionPipeline
@@ -25,8 +11,21 @@ import os
25
  import json
26
  from reportlab.lib.pagesizes import letter
27
  from reportlab.pdfgen import canvas
28
- #import pyttsx3
29
  from tempfile import NamedTemporaryFile
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # --- USDA API Setup ---
32
  USDA_API_KEY = "gcwe1gXmMveg7buqddggl6wAZa7Sd7wrZV87P31z"
@@ -50,14 +49,6 @@ def get_calories(ingredient):
50
  nutrients = get_nutrients(ingredient)
51
  return nutrients.get("Energy", 0.0)
52
 
53
- # --- Models ---
54
- recipe_model = pipeline("text-generation", model="samdak93/qrit-2")
55
- device = "cuda" if torch.cuda.is_available() else "cpu"
56
- image_model = StableDiffusionPipeline.from_pretrained(
57
- "runwayml/stable-diffusion-v1-5",
58
- torch_dtype=torch.float16 if device == "cuda" else torch.float32
59
- ).to(device)
60
-
61
  # --- Utilities ---
62
  forbidden = ["pork", "bacon", "ham", "lard", "gelatin", "alcohol", "beer", "wine", "rum", "whiskey", "vodka", "gin"]
63
 
@@ -65,9 +56,9 @@ def parse_ingredients(text):
65
  match = re.search(r"(?i)ingredients:(.*?)(\n|directions:|instructions:|$)", text, re.DOTALL)
66
  if match:
67
  ingredients_block = match.group(1).strip()
68
- return [line.strip("- *") for line in ingredients_block.split("\n") if line.strip()]
69
  return []
70
- #change 1000 to 2000 calories for more flexibility (2 places only)
71
  def generate_recipe(key_ingredient):
72
  for _ in range(5):
73
  prompt = f"Create a halal recipe under 2000 calories that includes {key_ingredient}.\nIngredients:"
@@ -88,6 +79,7 @@ def get_nutrient_breakdown(ingredients):
88
  return breakdown
89
 
90
  def generate_image(prompt):
 
91
  with torch.autocast(device):
92
  return image_model(prompt).images[0]
93
 
@@ -120,21 +112,12 @@ def export_pdf(recipe_text, nutrients):
120
  c.save()
121
  return temp_pdf.name
122
 
123
- #def generate_audio(recipe_text):
124
- # engine = pyttsx3.init()
125
- # temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")
126
- # engine.save_to_file(recipe_text, temp_audio.name)
127
- # engine.runAndWait()
128
- # return temp_audio.name
129
- from gtts import gTTS
130
-
131
  def generate_audio(recipe_text):
132
  tts = gTTS(text=recipe_text)
133
  temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")
134
  tts.save(temp_audio.name)
135
  return temp_audio.name
136
 
137
-
138
  def app(key_ingredient):
139
  recipe_text, ingredients, calories = generate_recipe(key_ingredient)
140
  if not ingredients:
@@ -152,7 +135,7 @@ def app(key_ingredient):
152
 
153
  # --- Gradio UI ---
154
  with gr.Blocks() as demo:
155
- gr.Markdown("## 🕌 Halal Recipe Generator - Advanced Edition")
156
  with gr.Row():
157
  key_input = gr.Textbox(label="Key Ingredient")
158
  submit_btn = gr.Button("Generate Recipe")
@@ -166,5 +149,4 @@ with gr.Blocks() as demo:
166
  submit_btn.click(app, inputs=key_input,
167
  outputs=[output_text, output_img, output_table, output_pdf, output_audio])
168
 
169
- #demo.launch(share=True,pwa=True,debug=True)
170
- demo.launch(share=True)
 
1
+ # app.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ from spaces import GPU
4
  import gradio as gr
5
  from transformers import pipeline
6
  from diffusers import StableDiffusionPipeline
 
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
+ # --- GPU preload hook ---
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"
 
49
  nutrients = get_nutrients(ingredient)
50
  return nutrients.get("Energy", 0.0)
51
 
 
 
 
 
 
 
 
 
52
  # --- Utilities ---
53
  forbidden = ["pork", "bacon", "ham", "lard", "gelatin", "alcohol", "beer", "wine", "rum", "whiskey", "vodka", "gin"]
54
 
 
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 list(set([line.strip("- *") for line in ingredients_block.split("\n") if line.strip()]))
60
  return []
61
+
62
  def generate_recipe(key_ingredient):
63
  for _ in range(5):
64
  prompt = f"Create a halal recipe under 2000 calories that includes {key_ingredient}.\nIngredients:"
 
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
 
 
112
  c.save()
113
  return temp_pdf.name
114
 
 
 
 
 
 
 
 
 
115
  def generate_audio(recipe_text):
116
  tts = gTTS(text=recipe_text)
117
  temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")
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:
 
135
 
136
  # --- Gradio UI ---
137
  with gr.Blocks() as demo:
138
+ gr.Markdown("## 🕌 Halal Recipe Generator - Advanced Edition (GPU-Enabled)")
139
  with gr.Row():
140
  key_input = gr.Textbox(label="Key Ingredient")
141
  submit_btn = gr.Button("Generate Recipe")
 
149
  submit_btn.click(app, inputs=key_input,
150
  outputs=[output_text, output_img, output_table, output_pdf, output_audio])
151
 
152
+ demo.launch()