kartik2627 commited on
Commit
6740350
Β·
verified Β·
1 Parent(s): d0a1850

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import torch
3
  from diffusers import StableDiffusionPipeline
4
  from datetime import datetime
 
5
 
6
  # Load the model
7
  model_id = "stabilityai/stable-diffusion-2"
@@ -22,19 +23,21 @@ def enhance_prompt(prompt, style):
22
  style_suffix = STYLE_TEMPLATES.get(style, "")
23
  return f"{prompt}, {style_suffix}"
24
 
25
- def generate_image(prompt, style):
26
  if not prompt.strip():
27
  return None, image_history
28
 
29
  final_prompt = enhance_prompt(prompt, style)
 
 
30
  image = pipe(final_prompt).images[0]
31
-
32
  # Add to history
33
  timestamp = datetime.now().strftime("%H:%M:%S")
34
  caption = f"{style} - {timestamp}"
35
  image_history.append((image, caption))
36
 
37
- return image, image_history[-5:] # show latest 5 only
38
 
39
  def clear_history():
40
  global image_history
@@ -44,22 +47,22 @@ def clear_history():
44
  # Gradio UI
45
  with gr.Blocks() as demo:
46
  gr.Markdown("## 🎨 Realistic Text-to-Image Generator")
47
- gr.Markdown("Powered by **Stable Diffusion 2** | Now with style presets, prompt enhancer, and image history!")
48
 
49
  with gr.Row():
50
  prompt = gr.Textbox(label="Enter your prompt", placeholder="e.g., A golden retriever in the snow")
51
  style = gr.Dropdown(["Photo", "Anime", "Oil Painting"], label="Choose Style", value="Photo")
 
 
52
 
53
  with gr.Row():
54
  generate_btn = gr.Button("🎨 Generate Image")
55
  clear_btn = gr.Button("🧹 Clear History")
56
 
57
  output_image = gr.Image(label="Generated Image", type="pil")
58
-
59
  gallery = gr.Gallery(label="πŸ–ΌοΈ Image History (last 5)", columns=3, object_fit="contain")
60
 
61
- generate_btn.click(generate_image, inputs=[prompt, style], outputs=[output_image, gallery])
62
  clear_btn.click(clear_history, outputs=[output_image, gallery])
63
 
64
- demo.launch()
65
-
 
2
  import torch
3
  from diffusers import StableDiffusionPipeline
4
  from datetime import datetime
5
+ from PIL import Image
6
 
7
  # Load the model
8
  model_id = "stabilityai/stable-diffusion-2"
 
23
  style_suffix = STYLE_TEMPLATES.get(style, "")
24
  return f"{prompt}, {style_suffix}"
25
 
26
+ def generate_image(prompt, style, uploaded_image):
27
  if not prompt.strip():
28
  return None, image_history
29
 
30
  final_prompt = enhance_prompt(prompt, style)
31
+
32
+ # Currently not using uploaded_image β€” could be used with img2img later
33
  image = pipe(final_prompt).images[0]
34
+
35
  # Add to history
36
  timestamp = datetime.now().strftime("%H:%M:%S")
37
  caption = f"{style} - {timestamp}"
38
  image_history.append((image, caption))
39
 
40
+ return image, image_history[-5:]
41
 
42
  def clear_history():
43
  global image_history
 
47
  # Gradio UI
48
  with gr.Blocks() as demo:
49
  gr.Markdown("## 🎨 Realistic Text-to-Image Generator")
50
+ gr.Markdown("Powered by **Stable Diffusion 2** | Now with style presets, prompt enhancer, image history, and optional image upload!")
51
 
52
  with gr.Row():
53
  prompt = gr.Textbox(label="Enter your prompt", placeholder="e.g., A golden retriever in the snow")
54
  style = gr.Dropdown(["Photo", "Anime", "Oil Painting"], label="Choose Style", value="Photo")
55
+
56
+ uploaded_image = gr.Image(label="Upload Optional Image (future use)", type="pil", tool=None)
57
 
58
  with gr.Row():
59
  generate_btn = gr.Button("🎨 Generate Image")
60
  clear_btn = gr.Button("🧹 Clear History")
61
 
62
  output_image = gr.Image(label="Generated Image", type="pil")
 
63
  gallery = gr.Gallery(label="πŸ–ΌοΈ Image History (last 5)", columns=3, object_fit="contain")
64
 
65
+ generate_btn.click(generate_image, inputs=[prompt, style, uploaded_image], outputs=[output_image, gallery])
66
  clear_btn.click(clear_history, outputs=[output_image, gallery])
67
 
68
+ demo.launch()