Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,8 @@ load_dotenv()
|
|
16 |
|
17 |
# Retrieve Hugging Face token from environment variable
|
18 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
19 |
|
20 |
## 2.1 Image Analysis with DETR
|
21 |
def load_detr_model():
|
@@ -24,7 +26,7 @@ def load_detr_model():
|
|
24 |
detr_processor = DetrImageProcessor.from_pretrained('facebook/detr-resnet-50')
|
25 |
return detr_model, detr_processor, None
|
26 |
except Exception as e:
|
27 |
-
return None, None, f"Error loading DETR model: {e}"
|
28 |
|
29 |
detr_model, detr_processor, detr_error = load_detr_model()
|
30 |
|
@@ -37,7 +39,7 @@ def detect_objects(image):
|
|
37 |
results = detr_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
38 |
return results, None
|
39 |
except Exception as e:
|
40 |
-
return None, f"Error in detect_objects: {e}"
|
41 |
else:
|
42 |
return None, "DETR models not loaded. Skipping object detection."
|
43 |
|
@@ -76,7 +78,7 @@ def style_transfer(content_image, style_image):
|
|
76 |
generated_image = generated.squeeze().clamp(0, 255).cpu().detach().numpy().transpose(1, 2, 0)
|
77 |
return Image.fromarray(np.uint8(generated_image)), None
|
78 |
except Exception as e:
|
79 |
-
return content_image, f"Error in style_transfer: {e}"
|
80 |
|
81 |
## 2.3 Layout Generation with LayoutLM
|
82 |
def load_layoutlm_model():
|
@@ -85,7 +87,7 @@ def load_layoutlm_model():
|
|
85 |
layoutlm_model = LayoutLMForTokenClassification.from_pretrained('microsoft/layoutlm-base-uncased')
|
86 |
return layoutlm_tokenizer, layoutlm_model, None
|
87 |
except Exception as e:
|
88 |
-
return None, None, f"Error loading LayoutLM model: {e}"
|
89 |
|
90 |
layoutlm_tokenizer, layoutlm_model, layoutlm_error = load_layoutlm_model()
|
91 |
|
@@ -97,20 +99,18 @@ def generate_layout(text):
|
|
97 |
layout = outputs.logits.argmax(dim=-1)
|
98 |
return layout, None
|
99 |
except Exception as e:
|
100 |
-
return None, f"Error in generate_layout: {e}"
|
101 |
else:
|
102 |
return None, "LayoutLM models not loaded. Skipping layout generation."
|
103 |
|
104 |
## 2.4 Image Generation with Stable Diffusion
|
105 |
def load_stable_diffusion_model():
|
106 |
try:
|
107 |
-
if HF_TOKEN is None:
|
108 |
-
raise ValueError("Hugging Face token not found in environment variables.")
|
109 |
login(token=HF_TOKEN)
|
110 |
sd_pipeline = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4").to("cuda")
|
111 |
return sd_pipeline, None
|
112 |
except Exception as e:
|
113 |
-
return None, f"Error loading Stable Diffusion model: {e}"
|
114 |
|
115 |
sd_pipeline, sd_error = load_stable_diffusion_model()
|
116 |
|
@@ -120,7 +120,7 @@ def generate_image(prompt):
|
|
120 |
image = sd_pipeline(prompt).images[0]
|
121 |
return image, None
|
122 |
except Exception as e:
|
123 |
-
return None, f"Error in generate_image: {e}"
|
124 |
else:
|
125 |
return None, "Stable Diffusion model not loaded. Skipping image generation."
|
126 |
|
@@ -130,7 +130,7 @@ def load_upscale_pipeline():
|
|
130 |
upscale_pipeline = StableDiffusionUpscalePipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler").to("cuda")
|
131 |
return upscale_pipeline, None
|
132 |
except Exception as e:
|
133 |
-
return None, f"Error loading Upscale Pipeline: {e}"
|
134 |
|
135 |
upscale_pipeline, upscale_error = load_upscale_pipeline()
|
136 |
|
@@ -142,7 +142,7 @@ def super_resolve(image):
|
|
142 |
upscaled_image = upscale_pipeline(image=image).images[0]
|
143 |
return upscaled_image, None
|
144 |
except Exception as e:
|
145 |
-
return None, f"Error in super_resolve: {e}"
|
146 |
else:
|
147 |
return image, "Upscale Pipeline not loaded. Skipping super-resolution."
|
148 |
|
@@ -176,7 +176,7 @@ def process_image(image, style_image, text_prompt):
|
|
176 |
|
177 |
return final_image, None
|
178 |
except Exception as e:
|
179 |
-
return None, f"Error in process_image: {e}"
|
180 |
|
181 |
iface = gr.Interface(
|
182 |
fn=process_image,
|
@@ -194,5 +194,5 @@ iface = gr.Interface(
|
|
194 |
try:
|
195 |
iface.launch()
|
196 |
except Exception as e:
|
197 |
-
print(f"Error occurred while launching the interface: {e}")
|
198 |
traceback.print_exc()
|
|
|
16 |
|
17 |
# Retrieve Hugging Face token from environment variable
|
18 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
19 |
+
if HF_TOKEN is None:
|
20 |
+
raise ValueError("Hugging Face token not found in environment variables.")
|
21 |
|
22 |
## 2.1 Image Analysis with DETR
|
23 |
def load_detr_model():
|
|
|
26 |
detr_processor = DetrImageProcessor.from_pretrained('facebook/detr-resnet-50')
|
27 |
return detr_model, detr_processor, None
|
28 |
except Exception as e:
|
29 |
+
return None, None, f"Error loading DETR model: {str(e)}"
|
30 |
|
31 |
detr_model, detr_processor, detr_error = load_detr_model()
|
32 |
|
|
|
39 |
results = detr_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
40 |
return results, None
|
41 |
except Exception as e:
|
42 |
+
return None, f"Error in detect_objects: {str(e)}"
|
43 |
else:
|
44 |
return None, "DETR models not loaded. Skipping object detection."
|
45 |
|
|
|
78 |
generated_image = generated.squeeze().clamp(0, 255).cpu().detach().numpy().transpose(1, 2, 0)
|
79 |
return Image.fromarray(np.uint8(generated_image)), None
|
80 |
except Exception as e:
|
81 |
+
return content_image, f"Error in style_transfer: {str(e)}"
|
82 |
|
83 |
## 2.3 Layout Generation with LayoutLM
|
84 |
def load_layoutlm_model():
|
|
|
87 |
layoutlm_model = LayoutLMForTokenClassification.from_pretrained('microsoft/layoutlm-base-uncased')
|
88 |
return layoutlm_tokenizer, layoutlm_model, None
|
89 |
except Exception as e:
|
90 |
+
return None, None, f"Error loading LayoutLM model: {str(e)}"
|
91 |
|
92 |
layoutlm_tokenizer, layoutlm_model, layoutlm_error = load_layoutlm_model()
|
93 |
|
|
|
99 |
layout = outputs.logits.argmax(dim=-1)
|
100 |
return layout, None
|
101 |
except Exception as e:
|
102 |
+
return None, f"Error in generate_layout: {str(e)}"
|
103 |
else:
|
104 |
return None, "LayoutLM models not loaded. Skipping layout generation."
|
105 |
|
106 |
## 2.4 Image Generation with Stable Diffusion
|
107 |
def load_stable_diffusion_model():
|
108 |
try:
|
|
|
|
|
109 |
login(token=HF_TOKEN)
|
110 |
sd_pipeline = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4").to("cuda")
|
111 |
return sd_pipeline, None
|
112 |
except Exception as e:
|
113 |
+
return None, f"Error loading Stable Diffusion model: {str(e)}"
|
114 |
|
115 |
sd_pipeline, sd_error = load_stable_diffusion_model()
|
116 |
|
|
|
120 |
image = sd_pipeline(prompt).images[0]
|
121 |
return image, None
|
122 |
except Exception as e:
|
123 |
+
return None, f"Error in generate_image: {str(e)}"
|
124 |
else:
|
125 |
return None, "Stable Diffusion model not loaded. Skipping image generation."
|
126 |
|
|
|
130 |
upscale_pipeline = StableDiffusionUpscalePipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler").to("cuda")
|
131 |
return upscale_pipeline, None
|
132 |
except Exception as e:
|
133 |
+
return None, f"Error loading Upscale Pipeline: {str(e)}"
|
134 |
|
135 |
upscale_pipeline, upscale_error = load_upscale_pipeline()
|
136 |
|
|
|
142 |
upscaled_image = upscale_pipeline(image=image).images[0]
|
143 |
return upscaled_image, None
|
144 |
except Exception as e:
|
145 |
+
return None, f"Error in super_resolve: {str(e)}"
|
146 |
else:
|
147 |
return image, "Upscale Pipeline not loaded. Skipping super-resolution."
|
148 |
|
|
|
176 |
|
177 |
return final_image, None
|
178 |
except Exception as e:
|
179 |
+
return None, f"Error in process_image: {str(e)}"
|
180 |
|
181 |
iface = gr.Interface(
|
182 |
fn=process_image,
|
|
|
194 |
try:
|
195 |
iface.launch()
|
196 |
except Exception as e:
|
197 |
+
print(f"Error occurred while launching the interface: {str(e)}")
|
198 |
traceback.print_exc()
|