naxemCDA commited on
Commit
107d2dc
·
1 Parent(s): 2a842e2

RG_PPLX_FixZGPU_CudaInitinsideGPUFunc

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -1,14 +1,12 @@
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
@@ -36,15 +34,12 @@ def get_calories(ingredient):
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"]
@@ -75,10 +70,22 @@ def get_nutrient_breakdown(ingredients):
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,7 +116,6 @@ def export_pdf(recipe_text, nutrients):
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")
 
1
 
2
  import gradio as gr
3
+ 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
  from reportlab.lib.pagesizes import letter
11
  from reportlab.pdfgen import canvas
12
  from tempfile import NamedTemporaryFile
 
34
  nutrients = get_nutrients(ingredient)
35
  return nutrients.get("Energy", 0.0)
36
 
37
+ # --- Load CPU-only model globally (safe) ---
38
  recipe_model = pipeline("text-generation", model="samdak93/qrit-2")
39
 
40
+ # Globals to cache GPU models after loading
41
+ image_model = None
42
+ recipe_model_gpu = None # If you want to move recipe_model to GPU, handle similarly
 
 
 
43
 
44
  # --- Utilities ---
45
  forbidden = ["pork", "bacon", "ham", "lard", "gelatin", "alcohol", "beer", "wine", "rum", "whiskey", "vodka", "gin"]
 
70
  breakdown[ing] = get_nutrients(ing)
71
  return breakdown
72
 
73
+ @spaces.GPU(duration=120)
74
+ def load_image_model():
75
+ global image_model
76
+ if image_model is None:
77
+ image_model = StableDiffusionPipeline.from_pretrained(
78
+ "runwayml/stable-diffusion-v1-5",
79
+ torch_dtype=torch.float16
80
+ ).to("cuda")
81
+ return image_model
82
+
83
+ @spaces.GPU(duration=120)
84
  def generate_image(prompt):
85
+ pipe = load_image_model()
86
+ with torch.autocast("cuda"):
87
+ image = pipe(prompt).images[0]
88
+ return image
89
 
90
  def export_pdf(recipe_text, nutrients):
91
  temp_pdf = NamedTemporaryFile(delete=False, suffix=".pdf")
 
116
  c.save()
117
  return temp_pdf.name
118
 
 
119
  def generate_audio(recipe_text):
120
  tts = gTTS(text=recipe_text)
121
  temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")