Shriti09 commited on
Commit
ef334e8
·
verified ·
1 Parent(s): 11aeae6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ from PIL import Image
6
+ import torch.nn.functional as F
7
+ from torchvision import transforms
8
+ import gradio as gr
9
+ from diffusers import StableDiffusionPipeline
10
+
11
+ # Define Loss Functions (same as your original code)
12
+
13
+ # Setup Stable Diffusion Pipeline
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+ pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4").to(device)
16
+
17
+ # Image transform to tensor
18
+ transform = transforms.ToTensor()
19
+
20
+ # Create a function to generate images
21
+ def generate_images(prompt, seed=42):
22
+ # Set generator with a fixed seed
23
+ generator = torch.Generator(device).manual_seed(seed)
24
+
25
+ # Generate image
26
+ output_image = pipe(prompt, generator=generator).images[0]
27
+
28
+ # Convert to tensor
29
+ image_tensor = transform(output_image).to(device)
30
+
31
+ # Compute losses (similar to original code)
32
+ losses = {
33
+ "edge": edge_loss,
34
+ "texture": texture_loss,
35
+ "entropy": entropy_loss,
36
+ "symmetry": symmetry_loss,
37
+ "contrast": contrast_loss
38
+ }
39
+
40
+ # Save and return the image
41
+ result_image_path = f"generated_images/seed_{seed}/original.png"
42
+ output_image.save(result_image_path)
43
+
44
+ # Return the image for display
45
+ return output_image
46
+
47
+ # Define Gradio Interface
48
+ interface = gr.Interface(
49
+ fn=generate_images, # Function to call when user interacts with the UI
50
+ inputs=[
51
+ gr.Textbox(label="Prompt", value="A futuristic city skyline at sunset"),
52
+ gr.Slider(minimum=0, maximum=1000, step=1, label="Seed", value=42)
53
+ ],
54
+ outputs=gr.Image(type="pil"),
55
+ live=True,
56
+ title="Futuristic City Image Generator",
57
+ description="Generate images of futuristic cities with various customizable settings."
58
+ )
59
+
60
+ # Launch the interface
61
+ interface.launch()