ItzRoBeerT commited on
Commit
4a0cc75
1 Parent(s): d725d78

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from diffusers import StableDiffusion3Pipeline
4
+ from diffusers import DiffusionPipeline
5
+ import torch
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ model_repo_id = "stabilityai/stable-diffusion-3.5-medium"
9
+ image_style = "pixel art"
10
+ torch_dtype = torch.float32
11
+
12
+ if torch.cuda.is_available():
13
+ torch_dtype = torch.bfloat16
14
+
15
+ def generate_description(image):
16
+ model = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
17
+ return model(image)[0]['generated_text']
18
+
19
+ def generate_image_by_description(description):
20
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
21
+ pipe = pipe.to(device)
22
+ prompt = (
23
+ f"Generate a high-quality, detailed image of a {image_style} of a pigeon. "
24
+ f"The description of the pigeon is: {description}. "
25
+ "Make it visually appealing with clear textures and distinct colors."
26
+ )
27
+ image = pipe(
28
+ prompt,
29
+ num_inference_steps=40,
30
+ guidance_scale=4.5,
31
+ ).images[0]
32
+ return image
33
+
34
+ with gr.Blocks() as demo:
35
+ selected_image = gr.Image(type="filepath", label="Upload an Image of the Pigeon")
36
+ generate_button = gr.Button("Generate Avatar", variant="primary")
37
+ generated_image = gr.Image(type="numpy", label="Generated Avatar")
38
+
39
+ # Function chaining: generate description, then generate image without displaying text
40
+ def process_and_generate(image):
41
+ description = generate_description(image)
42
+ return generate_image_by_description(description)
43
+
44
+ generate_button.click(process_and_generate, inputs=selected_image, outputs=generated_image)
45
+
46
+
47
+ demo.launch()