yukee1992 commited on
Commit
8d00cc9
·
verified ·
1 Parent(s): 86f1f64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -67
app.py CHANGED
@@ -1,23 +1,34 @@
1
  # Import necessary libraries
2
  import torch
3
  from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
4
- import gradio as gr
5
- from PIL import Image
 
6
  import io
7
- import tempfile
8
- import os
9
  import time
10
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Force CPU usage
12
  device = "cpu"
13
  print(f"Using device: {device}")
14
 
15
- # Load a HIGHER QUALITY model
16
  model_id = "stabilityai/stable-diffusion-2-1"
17
-
18
  print("Loading pipeline... This may take a few minutes.")
 
19
  try:
20
- # Use torch.float32 for CPU compatibility
21
  pipe = StableDiffusionPipeline.from_pretrained(
22
  model_id,
23
  torch_dtype=torch.float32,
@@ -25,17 +36,12 @@ try:
25
  safety_checker=None,
26
  requires_safety_checker=False
27
  )
28
-
29
- # Use a faster scheduler for quicker generation
30
  pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
31
-
32
- # Move the pipeline to the CPU
33
  pipe = pipe.to(device)
34
  print("Model loaded successfully on CPU!")
35
-
36
  except Exception as e:
37
  print(f"Error loading model: {e}")
38
- # Fallback to original model if needed
39
  model_id = "dreamlike-art/dreamlike-diffusion-1.0"
40
  pipe = StableDiffusionPipeline.from_pretrained(
41
  model_id,
@@ -47,59 +53,59 @@ except Exception as e:
47
  pipe = pipe.to(device)
48
  print(f"Fell back to {model_id}")
49
 
50
- # Define the image generation function
51
- def generate_image(prompt):
52
- """
53
- This function takes a text prompt and returns a generated image.
54
- """
55
- # PROFESSIONAL-QUALITY PROMPT ENHANCEMENT
56
- enhanced_prompt = f"masterpiece, best quality, 4K, ultra detailed, photorealistic, sharp focus, studio lighting, professional photography, {prompt}"
57
-
58
- # Remove any negative aspects (optional but improves quality)
59
- negative_prompt = "blurry, low quality, low resolution, watermark, signature, text, ugly, deformed"
60
-
61
- print(f"Generating image for prompt: {enhanced_prompt}")
62
-
63
- # Generate the image with better settings
64
- image = pipe(
65
- prompt=enhanced_prompt,
66
- negative_prompt=negative_prompt,
67
- width=512,
68
- height=512,
69
- guidance_scale=9.0,
70
- num_inference_steps=25,
71
- generator=torch.Generator(device=device)
72
- ).images[0]
73
-
74
- # Convert to RGB to ensure proper color format
75
- if image.mode != 'RGB':
76
- image = image.convert('RGB')
77
-
78
- print("Image generated successfully!")
79
-
80
- # Create a temporary file and save the image
81
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_file:
82
- image.save(tmp_file, format='PNG')
83
- tmp_file_path = tmp_file.name
84
-
85
- return tmp_file_path
86
 
87
- # Create the Gradio Interface
88
- demo = gr.Interface(
89
- fn=generate_image,
90
- inputs=gr.Textbox(
91
- label="Enter your scene description",
92
- lines=2,
93
- placeholder="A dragon reading a book under a magical tree"
94
- ),
95
- outputs=gr.File(label="Download Generated Illustration"),
96
- title="Premium Children's Book Illustrator 🤖🎨",
97
- description="Generating high-quality, sharp, detailed images for your stories. Enter a scene description."
98
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
- # Launch the app
101
- demo.launch(
102
- debug=True,
103
- server_name="0.0.0.0",
104
- share=False
105
- )
 
1
  # Import necessary libraries
2
  import torch
3
  from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
4
+ from fastapi import FastAPI, HTTPException
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ from pydantic import BaseModel
7
  import io
8
+ import base64
9
+ from PIL import Image
10
  import time
11
 
12
+ # Initialize FastAPI
13
+ app = FastAPI(title="Children's Book Illustrator API")
14
+
15
+ # Add CORS middleware to allow requests from n8n
16
+ app.add_middleware(
17
+ CORSMiddleware,
18
+ allow_origins=["*"],
19
+ allow_methods=["*"],
20
+ allow_headers=["*"],
21
+ )
22
+
23
  # Force CPU usage
24
  device = "cpu"
25
  print(f"Using device: {device}")
26
 
27
+ # Load model
28
  model_id = "stabilityai/stable-diffusion-2-1"
 
29
  print("Loading pipeline... This may take a few minutes.")
30
+
31
  try:
 
32
  pipe = StableDiffusionPipeline.from_pretrained(
33
  model_id,
34
  torch_dtype=torch.float32,
 
36
  safety_checker=None,
37
  requires_safety_checker=False
38
  )
 
 
39
  pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
 
 
40
  pipe = pipe.to(device)
41
  print("Model loaded successfully on CPU!")
 
42
  except Exception as e:
43
  print(f"Error loading model: {e}")
44
+ # Fallback
45
  model_id = "dreamlike-art/dreamlike-diffusion-1.0"
46
  pipe = StableDiffusionPipeline.from_pretrained(
47
  model_id,
 
53
  pipe = pipe.to(device)
54
  print(f"Fell back to {model_id}")
55
 
56
+ # Request model
57
+ class GenerateRequest(BaseModel):
58
+ prompt: str
59
+ width: int = 512
60
+ height: int = 512
61
+ steps: int = 25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ # Health check endpoint
64
+ @app.get("/")
65
+ async def health_check():
66
+ return {"status": "healthy", "model": model_id}
67
+
68
+ # Main API endpoint
69
+ @app.post("/generate")
70
+ async def generate_image(request: GenerateRequest):
71
+ try:
72
+ # Enhanced prompt
73
+ enhanced_prompt = f"masterpiece, best quality, 4K, ultra detailed, photorealistic, sharp focus, studio lighting, professional photography, {request.prompt}"
74
+ negative_prompt = "blurry, low quality, low resolution, watermark, signature, text, ugly, deformed"
75
+
76
+ print(f"Generating image for prompt: {enhanced_prompt}")
77
+
78
+ # Generate image
79
+ image = pipe(
80
+ prompt=enhanced_prompt,
81
+ negative_prompt=negative_prompt,
82
+ width=request.width,
83
+ height=request.height,
84
+ guidance_scale=9.0,
85
+ num_inference_steps=request.steps,
86
+ generator=torch.Generator(device=device)
87
+ ).images[0]
88
+
89
+ if image.mode != 'RGB':
90
+ image = image.convert('RGB')
91
+
92
+ print("Image generated successfully!")
93
+
94
+ # Convert to base64 for API response
95
+ buffered = io.BytesIO()
96
+ image.save(buffered, format="PNG")
97
+ img_base64 = base64.b64encode(buffered.getvalue()).decode()
98
+
99
+ return {
100
+ "status": "success",
101
+ "image": f"data:image/png;base64,{img_base64}",
102
+ "prompt": request.prompt
103
+ }
104
+
105
+ except Exception as e:
106
+ raise HTTPException(status_code=500, detail=f"Generation failed: {str(e)}")
107
 
108
+ # Run the app
109
+ if __name__ == "__main__":
110
+ import uvicorn
111
+ uvicorn.run(app, host="0.0.0.0", port=7860)