Spaces:
Runtime error
Runtime error
Aguilar Elizondo commited on
Commit ·
af0d24e
1
Parent(s): ffbb1f9
Fix: Use functions instead of classes for services
Browse files
app.py
CHANGED
|
@@ -13,36 +13,27 @@ import sys
|
|
| 13 |
# Add backend to path
|
| 14 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 15 |
|
| 16 |
-
from backend.services.diffusion_pipeline import
|
| 17 |
-
from backend.services.upscaler import
|
| 18 |
-
from backend.services.postprocess import
|
| 19 |
from backend.config import settings
|
| 20 |
|
| 21 |
# Configure logging
|
| 22 |
logging.basicConfig(level=logging.INFO)
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
| 25 |
-
# Initialize
|
| 26 |
-
|
| 27 |
-
upscaler = None
|
| 28 |
-
post_processor = None
|
| 29 |
|
| 30 |
def initialize_models():
|
| 31 |
"""Initialize all models on first use"""
|
| 32 |
-
global
|
| 33 |
|
| 34 |
-
if
|
| 35 |
logger.info("Loading Stable Diffusion pipeline...")
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
if upscaler is None:
|
| 39 |
-
logger.info("Loading upscaler...")
|
| 40 |
-
upscaler = ESRGANUpscaler()
|
| 41 |
-
|
| 42 |
-
if post_processor is None:
|
| 43 |
-
post_processor = PostProcessor()
|
| 44 |
|
| 45 |
-
def
|
| 46 |
input_image: Image.Image,
|
| 47 |
strength: float = 0.3,
|
| 48 |
guidance_scale: float = 5.5,
|
|
@@ -78,7 +69,7 @@ def enhance_image(
|
|
| 78 |
|
| 79 |
progress(0.2, desc="Enhancing with AI (this may take a few minutes)...")
|
| 80 |
|
| 81 |
-
enhanced =
|
| 82 |
image=input_image,
|
| 83 |
prompt=prompt,
|
| 84 |
negative_prompt=settings.NEGATIVE_PROMPT,
|
|
@@ -92,13 +83,29 @@ def enhance_image(
|
|
| 92 |
# Step 2: Optional Upscaling
|
| 93 |
if use_upscaler:
|
| 94 |
progress(0.75, desc="Upscaling image...")
|
| 95 |
-
enhanced =
|
| 96 |
progress(0.85, desc="Upscaling complete!")
|
| 97 |
|
| 98 |
# Step 3: Optional Post-processing
|
| 99 |
if use_postprocess:
|
| 100 |
progress(0.9, desc="Applying final touches...")
|
| 101 |
-
enhanced =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
progress(0.95, desc="Post-processing complete!")
|
| 103 |
|
| 104 |
progress(1.0, desc="Done!")
|
|
@@ -196,7 +203,7 @@ with gr.Blocks(title="Architecture AI Enhancer", theme=gr.themes.Soft()) as demo
|
|
| 196 |
|
| 197 |
# Connect the enhance button
|
| 198 |
enhance_btn.click(
|
| 199 |
-
fn=
|
| 200 |
inputs=[
|
| 201 |
input_image,
|
| 202 |
strength,
|
|
@@ -216,7 +223,7 @@ with gr.Blocks(title="Architecture AI Enhancer", theme=gr.themes.Soft()) as demo
|
|
| 216 |
],
|
| 217 |
inputs=[input_image, strength, guidance_scale, custom_prompt, use_upscaler, use_postprocess],
|
| 218 |
outputs=output_image,
|
| 219 |
-
fn=
|
| 220 |
cache_examples=False
|
| 221 |
)
|
| 222 |
|
|
|
|
| 13 |
# Add backend to path
|
| 14 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 15 |
|
| 16 |
+
from backend.services.diffusion_pipeline import get_pipeline_manager, enhance_image as enhance_with_diffusion
|
| 17 |
+
from backend.services.upscaler import upscale_image
|
| 18 |
+
from backend.services.postprocess import postprocess_image
|
| 19 |
from backend.config import settings
|
| 20 |
|
| 21 |
# Configure logging
|
| 22 |
logging.basicConfig(level=logging.INFO)
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
| 25 |
+
# Initialize pipeline manager (lazy loading)
|
| 26 |
+
pipeline_manager = None
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def initialize_models():
|
| 29 |
"""Initialize all models on first use"""
|
| 30 |
+
global pipeline_manager
|
| 31 |
|
| 32 |
+
if pipeline_manager is None:
|
| 33 |
logger.info("Loading Stable Diffusion pipeline...")
|
| 34 |
+
pipeline_manager = get_pipeline_manager()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
def enhance_image_gradio(
|
| 37 |
input_image: Image.Image,
|
| 38 |
strength: float = 0.3,
|
| 39 |
guidance_scale: float = 5.5,
|
|
|
|
| 69 |
|
| 70 |
progress(0.2, desc="Enhancing with AI (this may take a few minutes)...")
|
| 71 |
|
| 72 |
+
enhanced = pipeline_manager.run_inference(
|
| 73 |
image=input_image,
|
| 74 |
prompt=prompt,
|
| 75 |
negative_prompt=settings.NEGATIVE_PROMPT,
|
|
|
|
| 83 |
# Step 2: Optional Upscaling
|
| 84 |
if use_upscaler:
|
| 85 |
progress(0.75, desc="Upscaling image...")
|
| 86 |
+
enhanced = upscale_image(enhanced, scale=2)
|
| 87 |
progress(0.85, desc="Upscaling complete!")
|
| 88 |
|
| 89 |
# Step 3: Optional Post-processing
|
| 90 |
if use_postprocess:
|
| 91 |
progress(0.9, desc="Applying final touches...")
|
| 92 |
+
enhanced = postprocess_image(
|
| 93 |
+
enhanced,
|
| 94 |
+
clahe=True,
|
| 95 |
+
sharpen=1.0,
|
| 96 |
+
color_enhance=1.1
|
| 97 |
+
)
|
| 98 |
+
progress(0.85, desc="Upscaling complete!")
|
| 99 |
+
|
| 100 |
+
# Step 3: Optional Post-processing
|
| 101 |
+
if use_postprocess:
|
| 102 |
+
progress(0.9, desc="Applying final touches...")
|
| 103 |
+
enhanced = postprocess_image(
|
| 104 |
+
enhanced,
|
| 105 |
+
clahe=True,
|
| 106 |
+
sharpen=1.0,
|
| 107 |
+
color_enhance=1.1
|
| 108 |
+
)
|
| 109 |
progress(0.95, desc="Post-processing complete!")
|
| 110 |
|
| 111 |
progress(1.0, desc="Done!")
|
|
|
|
| 203 |
|
| 204 |
# Connect the enhance button
|
| 205 |
enhance_btn.click(
|
| 206 |
+
fn=enhance_image_gradio,
|
| 207 |
inputs=[
|
| 208 |
input_image,
|
| 209 |
strength,
|
|
|
|
| 223 |
],
|
| 224 |
inputs=[input_image, strength, guidance_scale, custom_prompt, use_upscaler, use_postprocess],
|
| 225 |
outputs=output_image,
|
| 226 |
+
fn=enhance_image_gradio,
|
| 227 |
cache_examples=False
|
| 228 |
)
|
| 229 |
|