Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
# Ensure that CUDA initialization happens within the worker process
|
6 |
model_pipe = None
|
7 |
|
8 |
@spaces.GPU
|
9 |
def generate(model_name, image, text):
|
10 |
global model_pipe
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
from t2v_metrics import VQAScore, list_all_vqascore_models
|
15 |
|
|
|
|
|
|
|
16 |
if model_pipe is None:
|
17 |
-
print("Initializing model
|
18 |
model_pipe = VQAScore(model="clip-flant5-xl", device="cuda") # our recommended scoring model
|
19 |
-
|
20 |
|
21 |
-
print(list_all_vqascore_models())
|
22 |
-
print("Image:", image)
|
23 |
-
print("Text:", text)
|
24 |
|
25 |
print("Generating!")
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
iface = gr.Interface(
|
30 |
fn=generate, # function to call
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# import spaces
|
3 |
+
|
4 |
+
# # Initialize the model only once, outside of any function
|
5 |
+
# # Ensure that CUDA initialization happens within the worker process
|
6 |
+
# model_pipe = None
|
7 |
+
|
8 |
+
# @spaces.GPU
|
9 |
+
# def generate(model_name, image, text):
|
10 |
+
# global model_pipe
|
11 |
+
# import torch
|
12 |
+
# torch.jit.script = lambda f: f
|
13 |
+
|
14 |
+
# from t2v_metrics import VQAScore, list_all_vqascore_models
|
15 |
+
|
16 |
+
# if model_pipe is None:
|
17 |
+
# print("Initializing model...")
|
18 |
+
# model_pipe = VQAScore(model="clip-flant5-xl", device="cuda") # our recommended scoring model
|
19 |
+
# # model_pipe.to("cuda")
|
20 |
+
|
21 |
+
# print(list_all_vqascore_models())
|
22 |
+
# print("Image:", image)
|
23 |
+
# print("Text:", text)
|
24 |
+
|
25 |
+
# print("Generating!")
|
26 |
+
# result = model_pipe(images=[image], texts=[text])
|
27 |
+
# return result
|
28 |
+
|
29 |
import gradio as gr
|
30 |
import spaces
|
31 |
+
import torch
|
32 |
+
import os
|
33 |
|
34 |
+
# Global model variable, but do not initialize or move to CUDA here
|
|
|
35 |
model_pipe = None
|
36 |
|
37 |
@spaces.GPU
|
38 |
def generate(model_name, image, text):
|
39 |
global model_pipe
|
40 |
+
|
41 |
+
# Debugging lines to trace CUDA initialization
|
42 |
+
print(f"PID: {os.getpid()}")
|
43 |
+
print(f"Before import: CUDA available: {torch.cuda.is_available()}")
|
44 |
+
|
45 |
+
torch.jit.script = lambda f: f # Avoid script error in lambda
|
46 |
|
47 |
from t2v_metrics import VQAScore, list_all_vqascore_models
|
48 |
|
49 |
+
print(f"After import: CUDA available: {torch.cuda.is_available()}")
|
50 |
+
|
51 |
+
# Worker Process: Perform all GPU-related initializations here
|
52 |
if model_pipe is None:
|
53 |
+
print("Initializing model in PID:", os.getpid())
|
54 |
model_pipe = VQAScore(model="clip-flant5-xl", device="cuda") # our recommended scoring model
|
55 |
+
print(f"Model initialized: CUDA available: {torch.cuda.is_available()}")
|
56 |
|
57 |
+
print(list_all_vqascore_models()) # Debug: List available models
|
58 |
+
print("Image:", image) # Debug: Print image path
|
59 |
+
print("Text:", text) # Debug: Print text input
|
60 |
|
61 |
print("Generating!")
|
62 |
+
# Wrap the model call in a try-except block to capture and debug CUDA errors
|
63 |
+
try:
|
64 |
+
result = model_pipe(images=[image], texts=[text]) # Perform the model inference
|
65 |
+
except RuntimeError as e:
|
66 |
+
print(f"RuntimeError during model inference: {e}")
|
67 |
+
raise e
|
68 |
+
|
69 |
+
return result # Return the result
|
70 |
|
71 |
iface = gr.Interface(
|
72 |
fn=generate, # function to call
|