yukee1992 commited on
Commit
d67939c
·
verified ·
1 Parent(s): 8c184bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -9,8 +9,8 @@ import time
9
  device = "cpu"
10
  print(f"Using device: {device}")
11
 
12
- # Load a smaller, CPU-friendly PUBLIC model
13
- model_id = "dreamlike-art/dreamlike-diffusion-1.0"
14
 
15
  print("Loading pipeline... This may take a few minutes.")
16
  try:
@@ -32,24 +32,39 @@ try:
32
 
33
  except Exception as e:
34
  print(f"Error loading model: {e}")
35
- raise e
 
 
 
 
 
 
 
 
 
 
36
 
37
  # Define the image generation function
38
  def generate_image(prompt):
39
  """
40
  This function takes a text prompt and returns a generated image.
41
  """
42
- # Add a consistent style to all prompts
43
- enhanced_prompt = f"children's book illustration, watercolor style, cute, whimsical, {prompt}"
 
 
 
 
44
  print(f"Generating image for prompt: {enhanced_prompt}")
45
 
46
- # Generate the image
47
  image = pipe(
48
  prompt=enhanced_prompt,
 
49
  width=512,
50
  height=512,
51
- guidance_scale=7.5,
52
- num_inference_steps=20,
53
  generator=torch.Generator(device=device)
54
  ).images[0]
55
 
@@ -66,14 +81,14 @@ demo = gr.Interface(
66
  inputs=gr.Textbox(
67
  label="Enter your scene description",
68
  lines=2,
69
- placeholder="A brave little mouse exploring a giant forest..."
70
  ),
71
  outputs=gr.Image(label="Generated Illustration", type="pil"),
72
- title="Children's Book Illustrator (CPU Edition) 🤖🎨",
73
- description="This free version runs on CPU. It's slower but gets the job done! Enter a scene description."
74
  )
75
 
76
- # Launch the app with simple settings
77
  demo.launch(
78
  debug=True,
79
  server_name="0.0.0.0",
 
9
  device = "cpu"
10
  print(f"Using device: {device}")
11
 
12
+ # Load a HIGHER QUALITY model
13
+ model_id = "stabilityai/stable-diffusion-2-1"
14
 
15
  print("Loading pipeline... This may take a few minutes.")
16
  try:
 
32
 
33
  except Exception as e:
34
  print(f"Error loading model: {e}")
35
+ # Fallback to original model if needed
36
+ model_id = "dreamlike-art/dreamlike-diffusion-1.0"
37
+ pipe = StableDiffusionPipeline.from_pretrained(
38
+ model_id,
39
+ torch_dtype=torch.float32,
40
+ use_safetensors=True,
41
+ safety_checker=None,
42
+ requires_safety_checker=False
43
+ )
44
+ pipe = pipe.to(device)
45
+ print(f"Fell back to {model_id}")
46
 
47
  # Define the image generation function
48
  def generate_image(prompt):
49
  """
50
  This function takes a text prompt and returns a generated image.
51
  """
52
+ # PROFESSIONAL-QUALITY PROMPT ENHANCEMENT
53
+ enhanced_prompt = f"masterpiece, best quality, 4K, ultra detailed, photorealistic, sharp focus, studio lighting, professional photography, {prompt}"
54
+
55
+ # Remove any negative aspects (optional but improves quality)
56
+ negative_prompt = "blurry, low quality, low resolution, watermark, signature, text, ugly, deformed"
57
+
58
  print(f"Generating image for prompt: {enhanced_prompt}")
59
 
60
+ # Generate the image with better settings
61
  image = pipe(
62
  prompt=enhanced_prompt,
63
+ negative_prompt=negative_prompt, # Added negative prompt
64
  width=512,
65
  height=512,
66
+ guidance_scale=9.0, # Slightly higher for better prompt adherence
67
+ num_inference_steps=25, # More steps for better quality
68
  generator=torch.Generator(device=device)
69
  ).images[0]
70
 
 
81
  inputs=gr.Textbox(
82
  label="Enter your scene description",
83
  lines=2,
84
+ placeholder="A dragon reading a book under a magical tree"
85
  ),
86
  outputs=gr.Image(label="Generated Illustration", type="pil"),
87
+ title="Premium Children's Book Illustrator 🤖🎨",
88
+ description="Generating high-quality, sharp, detailed images for your stories. Enter a scene description."
89
  )
90
 
91
+ # Launch the app
92
  demo.launch(
93
  debug=True,
94
  server_name="0.0.0.0",