Aastha074 commited on
Commit
d523783
·
verified ·
1 Parent(s): 3c75ee0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from PIL import Image
3
+ from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
4
+ from diffusers import StableDiffusionImg2ImgPipeline
5
+ from diffusers import EulerAncestralDiscreteScheduler
6
+
7
+ #provide your Hugging Face Authentication token
8
+ #you can obtain your token from https://huggingface.co/settings/tokens
9
+ auth_token = input("Enter your Hugging Face token: ")
10
+
11
+ ### image generation ###
12
+
13
+ #using stable diffusion version 2.1 for image generation
14
+ modelid = "stabilityai/stable-diffusion-2-1"
15
+ device = "cuda" if torch.cuda.is_available() else "cpu"
16
+
17
+ pipe = StableDiffusionPipeline.from_pretrained(
18
+ modelid,
19
+ revision="fp16",
20
+ torch_dtype= torch.float16,
21
+ use_auth_token=auth_token,
22
+ low_cpu_mem_usage=True
23
+ )
24
+ #using EulerAncestralDiscreteScheduler for sharper and detailed images
25
+
26
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
27
+
28
+
29
+ ### image modification ###
30
+ #using StableDiffusionImg2ImgPipeline for image to image generation
31
+ pipe.to(device)
32
+
33
+ pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16, low_cpu_mem_usage=True).to(device)
34
+ pipe_img2img.to(device)
35
+
36
+ import gradio as gr
37
+ # Function to generate image from text
38
+ def generate_image(prompt, guidance_scale):
39
+ global stored_image
40
+ image = pipe(prompt, guidance_scale=guidance_scale).images[0]
41
+ stored_image = image
42
+ return image
43
+
44
+ # Function to modify image (Img2Img)
45
+ def modify_image(prompt, strength=0.5):
46
+ global stored_image
47
+ if stored_image is None:
48
+ return "No generated image available. Generate an image first!"
49
+
50
+ stored_image = stored_image.convert("RGB")
51
+ modified_image = pipe_img2img(prompt=prompt, image=stored_image, strength=strength, guidance_scale=8.5).images[0]
52
+ return modified_image
53
+
54
+ # Gradio UI
55
+ with gr.Blocks() as demo:
56
+ gr.Markdown("# Stable Diffusion - Generate & Modify Images")
57
+ with gr.Group():
58
+ with gr.Row():
59
+ prompt_input = gr.Textbox(label="Enter Prompt", placeholder="A futuristic city at sunset")
60
+ with gr.Row():
61
+ guidance_input = gr.Slider(1.0, 15.0, value=8.5, label="Guidance Scale (More guidance meansthe model follows the prompt very strictly but is less creative)") # User can adjust guidance
62
+
63
+ with gr.Row():
64
+ generate_btn = gr.Button("Generate")
65
+ with gr.Row():
66
+ output_image = gr.Image(label="Generated Image")
67
+
68
+ generate_btn.click(generate_image, inputs=[prompt_input, guidance_input], outputs=output_image)
69
+
70
+
71
+ # Modify Image (After Generation)
72
+ with gr.Tab("Modify Image"):
73
+ modify_prompt = gr.Textbox(label="Enter modification prompt")
74
+ strength_slider = gr.Slider(0.1, 1.0, value=0.5, label="Strength (Higher = More Change)")
75
+ modify_btn = gr.Button("Modify")
76
+ modified_output = gr.Image(label="Modified Image")
77
+
78
+ modify_btn.click(modify_image, inputs=[modify_prompt, strength_slider], outputs=modified_output)
79
+
80
+ # Launch Gradio App
81
+ demo.launch(share=True)