prithivMLmods commited on
Commit
fc69e7f
·
verified ·
1 Parent(s): 4d4a293

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -181
app.py DELETED
@@ -1,181 +0,0 @@
1
- import spaces
2
- import gradio as gr
3
- import torch
4
- from PIL import Image
5
- from diffusers import DiffusionPipeline
6
- import random
7
- import uuid
8
- import numpy as np
9
- import time
10
- import os
11
-
12
- # Description for the app
13
- DESCRIPTION = """
14
- # Qwen Image Upscaler
15
-
16
- Upload a low-quality or small image, and this app will use the Qwen-Image model to generate a higher-resolution, more detailed version.
17
- """
18
-
19
- # --- Helper functions ---
20
-
21
- def save_image(img: Image.Image) -> str:
22
- """Saves an image to a unique filename and returns the path."""
23
- unique_name = str(uuid.uuid4()) + ".png"
24
- img.save(unique_name)
25
- return unique_name
26
-
27
- MAX_SEED = np.iinfo(np.int32).max
28
-
29
- # --- Load the Qwen/Qwen-Image pipeline ---
30
- # This single pipeline is used for both text-to-image and image-to-image (upscaling)
31
-
32
- print("Loading Qwen-Image model...")
33
- dtype = torch.bfloat16
34
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
35
-
36
- pipe_qwen = DiffusionPipeline.from_pretrained(
37
- "Qwen/Qwen-Image",
38
- torch_dtype=dtype
39
- ).to(device)
40
- print("Model loaded successfully.")
41
-
42
-
43
- # --- The main upscaler function ---
44
-
45
- @spaces.GPU(duration=120)
46
- def upscale_image(
47
- image: Image.Image,
48
- prompt: str,
49
- negative_prompt: str,
50
- seed: int,
51
- guidance_scale: float,
52
- randomize_seed: bool,
53
- num_inference_steps: int,
54
- progress=gr.Progress(track_tqdm=True)
55
- ):
56
- """
57
- Takes a low-resolution image and upscales it using the Qwen-Image model.
58
- """
59
- if image is None:
60
- raise gr.Error("No image uploaded. Please upload an image to upscale.")
61
-
62
- if randomize_seed:
63
- seed = random.randint(0, MAX_SEED)
64
-
65
- generator = torch.Generator(device).manual_seed(seed)
66
-
67
- start_time = time.time()
68
-
69
- # The pipeline automatically handles upscaling when an `image` argument is provided.
70
- upscaled_image = pipe_qwen(
71
- prompt=prompt,
72
- negative_prompt=negative_prompt,
73
- image=image, # Providing the input image triggers the upscaling/img2img mode
74
- guidance_scale=guidance_scale,
75
- num_inference_steps=num_inference_steps,
76
- generator=generator,
77
- output_type="pil",
78
- ).images[0]
79
-
80
- end_time = time.time()
81
- duration = end_time - start_time
82
-
83
- image_path = save_image(upscaled_image)
84
-
85
- print(f"Upscaling finished in {duration:.2f} seconds. Seed used: {seed}")
86
-
87
- return image_path, seed, f"{duration:.2f}"
88
-
89
-
90
- # --- Gradio User Interface ---
91
-
92
- css = '''
93
- .gradio-container {
94
- max-width: 840px !important;
95
- margin: 0 auto !important;
96
- }
97
- h1 {
98
- text-align: center;
99
- }
100
- footer {
101
- visibility: hidden;
102
- }
103
- '''
104
-
105
- with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
106
- gr.Markdown(DESCRIPTION)
107
-
108
- with gr.Row():
109
- with gr.Column(scale=1):
110
- # FIXED LINE: Removed the `tool='editor'` argument
111
- image_upload = gr.Image(
112
- label="Upload Low-Resolution Image",
113
- type="pil"
114
- )
115
- prompt = gr.Textbox(
116
- label="Prompt",
117
- value="ultra-detailed, high quality, 4k, 8k, masterpiece",
118
- placeholder="Describe the desired result (e.g., 'photorealistic, sharp focus')."
119
- )
120
- upscale_button = gr.Button("Upscale Image", variant="primary")
121
-
122
- with gr.Column(scale=1):
123
- upscaled_image_result = gr.Image(label="Upscaled Image")
124
-
125
- with gr.Accordion("Upscaler Options", open=False):
126
- negative_prompt = gr.Text(
127
- label="Negative Prompt",
128
- max_lines=1,
129
- placeholder="Enter concepts to avoid (e.g., 'blurry, pixelated').",
130
- value="blurry, low resolution, text, watermark, jpeg artifacts, compression",
131
- )
132
- seed = gr.Slider(
133
- label="Seed",
134
- minimum=0,
135
- maximum=MAX_SEED,
136
- step=1,
137
- value=0,
138
- )
139
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
140
- guidance_scale = gr.Slider(
141
- label="Guidance Scale",
142
- minimum=0.0,
143
- maximum=20.0,
144
- step=0.1,
145
- value=4.0,
146
- )
147
- num_inference_steps = gr.Slider(
148
- label="Number of Inference Steps",
149
- minimum=1,
150
- maximum=100,
151
- step=1,
152
- value=25, # Upscaling often requires fewer steps than generation from scratch
153
- )
154
-
155
- with gr.Accordion("Output Information", open=True):
156
- with gr.Row():
157
- seed_display = gr.Textbox(label="Seed used", interactive=False)
158
- generation_time = gr.Textbox(label="Generation time (seconds)", interactive=False)
159
-
160
- # Connect the button to the function
161
- upscale_button.click(
162
- fn=upscale_image,
163
- inputs=[
164
- image_upload,
165
- prompt,
166
- negative_prompt,
167
- seed,
168
- guidance_scale,
169
- randomize_seed,
170
- num_inference_steps
171
- ],
172
- outputs=[
173
- upscaled_image_result,
174
- seed_display,
175
- generation_time,
176
- ],
177
- api_name="upscale"
178
- )
179
-
180
- if __name__ == "__main__":
181
- demo.queue(max_size=20).launch(share=False, debug=True, show_error=True)