logan-codes commited on
Commit
d8411cf
·
1 Parent(s): 83fbbb0

added exception handling with logging

Browse files
Files changed (3) hide show
  1. Dockerfile +6 -6
  2. app.py +59 -28
  3. requirements.txt +0 -0
Dockerfile CHANGED
@@ -3,15 +3,17 @@ FROM python:3.11.9
3
  # Environment settings
4
  ENV PYTHONDONTWRITEBYTECODE=1
5
  ENV PYTHONUNBUFFERED=1
6
- ENV HOME=/home/user
7
- ENV PATH=/home/user/.local/bin:$PATH
 
8
 
9
  WORKDIR /app
10
 
11
  # System dependencies
12
  RUN apt-get update && apt-get install -y \
13
  build-essential \
14
- curl \libgl1 \
 
15
  libglib2.0-0 \
16
  libsm6 \
17
  libxext6 \
@@ -33,9 +35,7 @@ RUN pip install --no-cache-dir --user -r requirements.txt
33
  # Copy application code
34
  COPY --chown=user . .
35
 
36
- # HF Spaces default port is 7860 (will be used by Streamlit)
37
- # FastAPI will run on 8000 internally
38
  EXPOSE 7860
39
 
40
  # Run the startup script
41
- CMD ["python", "app.py"]
 
3
  # Environment settings
4
  ENV PYTHONDONTWRITEBYTECODE=1
5
  ENV PYTHONUNBUFFERED=1
6
+ ENV HF_HOME=/home/user/.cache/huggingface
7
+ ENV TRANSFORMERS_CACHE=/home/user/.cache/huggingface/transformers
8
+ ENV DIFFUSERS_CACHE=/home/user/.cache/huggingface/diffusers
9
 
10
  WORKDIR /app
11
 
12
  # System dependencies
13
  RUN apt-get update && apt-get install -y \
14
  build-essential \
15
+ curl \
16
+ libgl1 \
17
  libglib2.0-0 \
18
  libsm6 \
19
  libxext6 \
 
35
  # Copy application code
36
  COPY --chown=user . .
37
 
 
 
38
  EXPOSE 7860
39
 
40
  # Run the startup script
41
+ CMD ["startup.sh"]
app.py CHANGED
@@ -6,44 +6,69 @@ from PIL import Image
6
  import gradio as gr
7
  import numpy as np
8
  import logging
 
9
 
 
10
  logging.basicConfig(
11
  level=logging.INFO,
12
- format="%(asctime)s | %(levelname)s | %(name)s | %(message)s"
 
 
 
 
13
  )
14
 
15
  logger = logging.getLogger(__name__)
16
 
 
 
 
 
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
  dtype = torch.float16 if torch.cuda.is_available() else torch.float32
19
 
20
  logger.info(f"Device: {device}")
21
-
22
- logger.info("Loading Controlnet...")
23
- cn= ControlNetModel.from_pretrained(
24
- "lllyasviel/sd-controlnet-scribble",
25
- torch_dtype=dtype
26
- )
27
- logger.info("Controlnet Loaded.")
28
-
29
- logger.info("Loading Stable Diffusion Controlnet Pipeline...")
30
- pipe= StableDiffusionControlNetPipeline.from_pretrained(
31
- "runwayml/stable-diffusion-v1-5",
32
- controlnet=cn,
33
- torch_dtype=dtype,
34
- safety_checker=None
35
- )
36
- logger.info("Stable Diffusion Controlnet Pipeline Loaded.")
37
-
38
- pipe.scheduler= UniPCMultistepScheduler.from_config(
39
- pipe.scheduler.config
40
- )
41
- if torch.cuda.is_available():
42
- pipe.enable_attention_slicing()
43
- pipe.enable_model_cpu_offload()
44
- else:
45
- pipe= pipe.to(device)
46
- logger.info("Config setup completed")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  def generate(sketch_data, prompt, num_inference, guidance):
49
  logger.info("Image generation requested")
@@ -135,4 +160,10 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(), title="Sketch to Image") as demo
135
  outputs=output_image
136
  )
137
 
138
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
6
  import gradio as gr
7
  import numpy as np
8
  import logging
9
+ import sys
10
 
11
+ # Configure logging for both console and file
12
  logging.basicConfig(
13
  level=logging.INFO,
14
+ format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
15
+ handlers=[
16
+ logging.StreamHandler(sys.stdout),
17
+ logging.FileHandler('app.log')
18
+ ]
19
  )
20
 
21
  logger = logging.getLogger(__name__)
22
 
23
+ # Test logging immediately
24
+ logger.info("Application starting...")
25
+ logger.info(f"Python version: {sys.version}")
26
+
27
  device = "cuda" if torch.cuda.is_available() else "cpu"
28
  dtype = torch.float16 if torch.cuda.is_available() else torch.float32
29
 
30
  logger.info(f"Device: {device}")
31
+ logger.info(f"Torch version: {torch.__version__}")
32
+ logger.info(f"CUDA available: {torch.cuda.is_available()}")
33
+
34
+ # Add error handling for model loading
35
+ try:
36
+ logger.info("Loading Controlnet...")
37
+ cn = ControlNetModel.from_pretrained(
38
+ "lllyasviel/sd-controlnet-scribble",
39
+ torch_dtype=dtype
40
+ )
41
+ logger.info("Controlnet Loaded.")
42
+ except Exception as e:
43
+ logger.error(f"Failed to load ControlNet: {str(e)}")
44
+ raise
45
+
46
+ try:
47
+ logger.info("Loading Stable Diffusion Controlnet Pipeline...")
48
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
49
+ "runwayml/stable-diffusion-v1-5",
50
+ controlnet=cn,
51
+ torch_dtype=dtype,
52
+ safety_checker=None
53
+ )
54
+ logger.info("Stable Diffusion Controlnet Pipeline Loaded.")
55
+ except Exception as e:
56
+ logger.error(f"Failed to load pipeline: {str(e)}")
57
+ raise
58
+
59
+ try:
60
+ pipe.scheduler = UniPCMultistepScheduler.from_config(
61
+ pipe.scheduler.config
62
+ )
63
+ if torch.cuda.is_available():
64
+ pipe.enable_attention_slicing()
65
+ pipe.enable_model_cpu_offload()
66
+ else:
67
+ pipe = pipe.to(device)
68
+ logger.info("Config setup completed")
69
+ except Exception as e:
70
+ logger.error(f"Failed to setup pipeline configuration: {str(e)}")
71
+ raise
72
 
73
  def generate(sketch_data, prompt, num_inference, guidance):
74
  logger.info("Image generation requested")
 
160
  outputs=output_image
161
  )
162
 
163
+ logger.info("Starting Gradio demo...")
164
+ try:
165
+ demo.launch(server_name="0.0.0.0", server_port=7860)
166
+ logger.info("Gradio demo launched successfully")
167
+ except Exception as e:
168
+ logger.error(f"Failed to launch Gradio demo: {str(e)}")
169
+ raise
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ