Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,10 @@ from transformers import AutoConfig, AutoModelForCausalLM
|
|
4 |
from janus.models import MultiModalityCausalLM, VLChatProcessor
|
5 |
from janus.utils.io import load_pil_images
|
6 |
from PIL import Image
|
7 |
-
|
8 |
import numpy as np
|
9 |
import os
|
10 |
import time
|
11 |
-
import spaces
|
12 |
-
|
13 |
|
14 |
# Load model and processor
|
15 |
model_path = "deepseek-ai/Janus-Pro-7B"
|
@@ -30,7 +28,6 @@ cuda_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
|
30 |
|
31 |
@torch.inference_mode()
|
32 |
@spaces.GPU(duration=120)
|
33 |
-
# Multimodal Understanding function
|
34 |
def multimodal_understanding(image, question, seed, top_p, temperature):
|
35 |
# Clear CUDA cache before generating
|
36 |
torch.cuda.empty_cache()
|
@@ -54,7 +51,6 @@ def multimodal_understanding(image, question, seed, top_p, temperature):
|
|
54 |
conversations=conversation, images=pil_images, force_batchify=True
|
55 |
).to(cuda_device, dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float16)
|
56 |
|
57 |
-
|
58 |
inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
|
59 |
|
60 |
outputs = vl_gpt.language_model.generate(
|
@@ -73,7 +69,6 @@ def multimodal_understanding(image, question, seed, top_p, temperature):
|
|
73 |
answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
|
74 |
return answer
|
75 |
|
76 |
-
|
77 |
def generate(input_ids,
|
78 |
width,
|
79 |
height,
|
@@ -113,8 +108,6 @@ def generate(input_ids,
|
|
113 |
img_embeds = vl_gpt.prepare_gen_img_embeds(next_token)
|
114 |
inputs_embeds = img_embeds.unsqueeze(dim=1)
|
115 |
|
116 |
-
|
117 |
-
|
118 |
patches = vl_gpt.gen_vision_model.decode_code(generated_tokens.to(dtype=torch.int),
|
119 |
shape=[parallel_size, 8, width // patch_size, height // patch_size])
|
120 |
|
@@ -129,8 +122,6 @@ def unpack(dec, width, height, parallel_size=5):
|
|
129 |
|
130 |
return visual_img
|
131 |
|
132 |
-
|
133 |
-
|
134 |
@torch.inference_mode()
|
135 |
@spaces.GPU(duration=120) # Specify a duration to avoid timeout
|
136 |
def generate_image(prompt,
|
@@ -169,28 +160,82 @@ def generate_image(prompt,
|
|
169 |
parallel_size=parallel_size)
|
170 |
|
171 |
return [Image.fromarray(images[i]).resize((768, 768), Image.LANCZOS) for i in range(parallel_size)]
|
172 |
-
|
173 |
|
174 |
-
# Gradio interface
|
175 |
-
with gr.Blocks(
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
temperature = gr.Slider(minimum=0, maximum=1, value=0.1, step=0.05, label="temperature")
|
184 |
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
Analysis Methodology (Concise): List, very briefly, the key anatomical areas/features you will assess, in the order of assessment (e.g., Optic Disc, Vessels, Macula, Periphery, Overall Quality). Do not describe the analysis process in detail here – just name the areas.
|
196 |
Detailed Image Analysis & Percentage Breakdown: Analyze the image, addressing each area listed in Step 1. For each area:
|
@@ -200,43 +245,74 @@ For any areas where analysis is incomplete (<100%), briefly explain the limiting
|
|
200 |
Differential Diagnosis (Image-Based Only): Based solely on your Step 2 analysis, provide:
|
201 |
Most Likely Diagnosis (from image findings).
|
202 |
Other Possible Diagnoses (from image findings).
|
203 |
-
Rationale: For each diagnosis, briefly link specific image findings to the diagnostic criteria.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
|
214 |
-
|
215 |
-
|
216 |
-
with gr.Row():
|
217 |
-
cfg_weight_input = gr.Slider(minimum=1, maximum=10, value=5, step=0.5, label="CFG Weight")
|
218 |
-
t2i_temperature = gr.Slider(minimum=0, maximum=1, value=1.0, step=0.05, label="temperature")
|
219 |
-
|
220 |
-
prompt_input = gr.Textbox(label="Prompt. (Prompt in more detail can help produce better images!)")
|
221 |
-
seed_input = gr.Number(label="Seed (Optional)", precision=0, value=12345)
|
222 |
-
|
223 |
-
generation_button = gr.Button("Generate Images")
|
224 |
-
|
225 |
-
image_output = gr.Gallery(label="Generated Images", columns=2, rows=2, height=300)
|
226 |
-
|
227 |
-
examples_t2i = gr.Examples(
|
228 |
-
label="Text to image generation examples.",
|
229 |
-
examples=[
|
230 |
-
"Master shifu racoon wearing drip attire as a street gangster.",
|
231 |
-
"The face of a beautiful girl",
|
232 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
233 |
-
"A glass of red wine on a reflective surface.",
|
234 |
-
"A cute and adorable baby fox with big brown eyes, autumn leaves in the background enchanting,immortal,fluffy, shiny mane,Petals,fairyism,unreal engine 5 and Octane Render,highly detailed, photorealistic, cinematic, natural colors.",
|
235 |
-
"The image features an intricately designed eye set against a circular backdrop adorned with ornate swirl patterns that evoke both realism and surrealism. At the center of attention is a strikingly vivid blue iris surrounded by delicate veins radiating outward from the pupil to create depth and intensity. The eyelashes are long and dark, casting subtle shadows on the skin around them which appears smooth yet slightly textured as if aged or weathered over time.\n\nAbove the eye, there's a stone-like structure resembling part of classical architecture, adding layers of mystery and timeless elegance to the composition. This architectural element contrasts sharply but harmoniously with the organic curves surrounding it. Below the eye lies another decorative motif reminiscent of baroque artistry, further enhancing the overall sense of eternity encapsulated within each meticulously crafted detail. \n\nOverall, the atmosphere exudes a mysterious aura intertwined seamlessly with elements suggesting timelessness, achieved through the juxtaposition of realistic textures and surreal artistic flourishes. Each component\u2014from the intricate designs framing the eye to the ancient-looking stone piece above\u2014contributes uniquely towards creating a visually captivating tableau imbued with enigmatic allure.",
|
236 |
-
],
|
237 |
-
inputs=prompt_input,
|
238 |
-
)
|
239 |
-
|
240 |
understanding_button.click(
|
241 |
multimodal_understanding,
|
242 |
inputs=[image_input, question_input, und_seed_input, top_p, temperature],
|
@@ -249,5 +325,58 @@ Rationale: For each diagnosis, briefly link specific image findings to the diagn
|
|
249 |
outputs=image_output
|
250 |
)
|
251 |
|
252 |
-
|
253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from janus.models import MultiModalityCausalLM, VLChatProcessor
|
5 |
from janus.utils.io import load_pil_images
|
6 |
from PIL import Image
|
|
|
7 |
import numpy as np
|
8 |
import os
|
9 |
import time
|
10 |
+
import spaces
|
|
|
11 |
|
12 |
# Load model and processor
|
13 |
model_path = "deepseek-ai/Janus-Pro-7B"
|
|
|
28 |
|
29 |
@torch.inference_mode()
|
30 |
@spaces.GPU(duration=120)
|
|
|
31 |
def multimodal_understanding(image, question, seed, top_p, temperature):
|
32 |
# Clear CUDA cache before generating
|
33 |
torch.cuda.empty_cache()
|
|
|
51 |
conversations=conversation, images=pil_images, force_batchify=True
|
52 |
).to(cuda_device, dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float16)
|
53 |
|
|
|
54 |
inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
|
55 |
|
56 |
outputs = vl_gpt.language_model.generate(
|
|
|
69 |
answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
|
70 |
return answer
|
71 |
|
|
|
72 |
def generate(input_ids,
|
73 |
width,
|
74 |
height,
|
|
|
108 |
img_embeds = vl_gpt.prepare_gen_img_embeds(next_token)
|
109 |
inputs_embeds = img_embeds.unsqueeze(dim=1)
|
110 |
|
|
|
|
|
111 |
patches = vl_gpt.gen_vision_model.decode_code(generated_tokens.to(dtype=torch.int),
|
112 |
shape=[parallel_size, 8, width // patch_size, height // patch_size])
|
113 |
|
|
|
122 |
|
123 |
return visual_img
|
124 |
|
|
|
|
|
125 |
@torch.inference_mode()
|
126 |
@spaces.GPU(duration=120) # Specify a duration to avoid timeout
|
127 |
def generate_image(prompt,
|
|
|
160 |
parallel_size=parallel_size)
|
161 |
|
162 |
return [Image.fromarray(images[i]).resize((768, 768), Image.LANCZOS) for i in range(parallel_size)]
|
|
|
163 |
|
164 |
+
# Gradio interface with improved UI
|
165 |
+
with gr.Blocks(theme=gr.themes.Soft(
|
166 |
+
primary_hue="blue",
|
167 |
+
secondary_hue="indigo",
|
168 |
+
)) as demo:
|
169 |
+
gr.Markdown(
|
170 |
+
"""
|
171 |
+
# Deepseek Multimodal
|
172 |
+
### Advanced AI for Visual Understanding and Generation
|
|
|
173 |
|
174 |
+
This powerful multimodal AI system combines:
|
175 |
+
* **Visual Analysis**: Advanced image understanding and medical image interpretation
|
176 |
+
* **Creative Generation**: High-quality image generation from text descriptions
|
177 |
+
* **Interactive Chat**: Natural conversation about visual content
|
178 |
+
"""
|
179 |
+
)
|
180 |
+
|
181 |
+
with gr.Tabs():
|
182 |
+
# Visual Chat Tab
|
183 |
+
with gr.Tab("Visual Understanding", icon="image"):
|
184 |
+
with gr.Row(equal_height=True):
|
185 |
+
with gr.Column(scale=1):
|
186 |
+
image_input = gr.Image(
|
187 |
+
label="Upload Image",
|
188 |
+
type="numpy",
|
189 |
+
elem_classes="image-preview"
|
190 |
+
)
|
191 |
+
|
192 |
+
with gr.Column(scale=1):
|
193 |
+
question_input = gr.Textbox(
|
194 |
+
label="Question or Analysis Request",
|
195 |
+
placeholder="Ask a question about the image or request detailed analysis...",
|
196 |
+
lines=3
|
197 |
+
)
|
198 |
+
with gr.Row():
|
199 |
+
und_seed_input = gr.Number(
|
200 |
+
label="Seed",
|
201 |
+
precision=0,
|
202 |
+
value=42,
|
203 |
+
container=False
|
204 |
+
)
|
205 |
+
top_p = gr.Slider(
|
206 |
+
minimum=0,
|
207 |
+
maximum=1,
|
208 |
+
value=0.95,
|
209 |
+
step=0.05,
|
210 |
+
label="Top-p",
|
211 |
+
container=False
|
212 |
+
)
|
213 |
+
temperature = gr.Slider(
|
214 |
+
minimum=0,
|
215 |
+
maximum=1,
|
216 |
+
value=0.1,
|
217 |
+
step=0.05,
|
218 |
+
label="Temperature",
|
219 |
+
container=False
|
220 |
+
)
|
221 |
+
|
222 |
+
understanding_button = gr.Button(
|
223 |
+
"Analyze Image",
|
224 |
+
variant="primary",
|
225 |
+
size="lg"
|
226 |
+
)
|
227 |
+
|
228 |
+
understanding_output = gr.Textbox(
|
229 |
+
label="Analysis Results",
|
230 |
+
lines=10,
|
231 |
+
show_copy_button=True
|
232 |
+
)
|
233 |
+
|
234 |
+
gr.Examples(
|
235 |
+
label="Medical Analysis Examples",
|
236 |
+
examples=[
|
237 |
+
[
|
238 |
+
"""You are an AI assistant trained to analyze medical images. Analyze the attached fundus photograph in extreme detail, following a structured approach analogous to an ophthalmologist's examination. Provide a differential diagnosis solely based on this image analysis, without assuming any clinical history. Structure your response as follows:
|
239 |
|
240 |
Analysis Methodology (Concise): List, very briefly, the key anatomical areas/features you will assess, in the order of assessment (e.g., Optic Disc, Vessels, Macula, Periphery, Overall Quality). Do not describe the analysis process in detail here – just name the areas.
|
241 |
Detailed Image Analysis & Percentage Breakdown: Analyze the image, addressing each area listed in Step 1. For each area:
|
|
|
245 |
Differential Diagnosis (Image-Based Only): Based solely on your Step 2 analysis, provide:
|
246 |
Most Likely Diagnosis (from image findings).
|
247 |
Other Possible Diagnoses (from image findings).
|
248 |
+
Rationale: For each diagnosis, briefly link specific image findings to the diagnostic criteria.""",
|
249 |
+
"fundus.webp",
|
250 |
+
],
|
251 |
+
],
|
252 |
+
inputs=[question_input, image_input],
|
253 |
+
)
|
254 |
+
|
255 |
+
# Image Generation Tab
|
256 |
+
with gr.Tab("Image Generation", icon="wand"):
|
257 |
+
with gr.Column():
|
258 |
+
prompt_input = gr.Textbox(
|
259 |
+
label="Image Description",
|
260 |
+
placeholder="Describe the image you want to create in detail...",
|
261 |
+
lines=3
|
262 |
+
)
|
263 |
|
264 |
+
with gr.Row():
|
265 |
+
cfg_weight_input = gr.Slider(
|
266 |
+
minimum=1,
|
267 |
+
maximum=10,
|
268 |
+
value=5,
|
269 |
+
step=0.5,
|
270 |
+
label="Guidance Scale",
|
271 |
+
info="Higher values create images that more closely match your prompt"
|
272 |
+
)
|
273 |
+
t2i_temperature = gr.Slider(
|
274 |
+
minimum=0,
|
275 |
+
maximum=1,
|
276 |
+
value=1.0,
|
277 |
+
step=0.05,
|
278 |
+
label="Temperature",
|
279 |
+
info="Controls randomness in generation"
|
280 |
+
)
|
281 |
+
seed_input = gr.Number(
|
282 |
+
label="Seed (Optional)",
|
283 |
+
precision=0,
|
284 |
+
value=12345,
|
285 |
+
info="Set for reproducible results"
|
286 |
+
)
|
287 |
+
|
288 |
+
generation_button = gr.Button(
|
289 |
+
"Generate Images",
|
290 |
+
variant="primary",
|
291 |
+
size="lg"
|
292 |
+
)
|
293 |
+
|
294 |
+
image_output = gr.Gallery(
|
295 |
+
label="Generated Images",
|
296 |
+
columns=3,
|
297 |
+
rows=2,
|
298 |
+
height=500,
|
299 |
+
object_fit="contain"
|
300 |
+
)
|
301 |
+
|
302 |
+
gr.Examples(
|
303 |
+
label="Generation Examples",
|
304 |
+
examples=[
|
305 |
+
"Master shifu racoon wearing drip attire as a street gangster.",
|
306 |
+
"The face of a beautiful girl",
|
307 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
308 |
+
"A glass of red wine on a reflective surface.",
|
309 |
+
"A cute and adorable baby fox with big brown eyes, autumn leaves in the background enchanting,immortal,fluffy, shiny mane,Petals,fairyism,unreal engine 5 and Octane Render,highly detailed, photorealistic, cinematic, natural colors.",
|
310 |
+
"The image features an intricately designed eye set against a circular backdrop adorned with ornate swirl patterns that evoke both realism and surrealism. At the center of attention is a strikingly vivid blue iris surrounded by delicate veins radiating outward from the pupil to create depth and intensity. The eyelashes are long and dark, casting subtle shadows on the skin around them which appears smooth yet slightly textured as if aged or weathered over time."
|
311 |
+
],
|
312 |
+
inputs=prompt_input,
|
313 |
+
)
|
314 |
|
315 |
+
# Connect components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
understanding_button.click(
|
317 |
multimodal_understanding,
|
318 |
inputs=[image_input, question_input, und_seed_input, top_p, temperature],
|
|
|
325 |
outputs=image_output
|
326 |
)
|
327 |
|
328 |
+
# Add custom CSS
|
329 |
+
demo.load(css="""
|
330 |
+
.gradio-container {
|
331 |
+
font-family: 'Inter', -apple-system, sans-serif;
|
332 |
+
}
|
333 |
+
.image-preview {
|
334 |
+
min-height: 300px;
|
335 |
+
max-height: 500px;
|
336 |
+
width: 100%;
|
337 |
+
object-fit: contain;
|
338 |
+
border-radius: 8px;
|
339 |
+
border: 2px solid #eee;
|
340 |
+
}
|
341 |
+
.tabs.tab-nav {
|
342 |
+
border-bottom: 2px solid #eee;
|
343 |
+
margin-bottom: 2rem;
|
344 |
+
}
|
345 |
+
.tab-nav {
|
346 |
+
background: white;
|
347 |
+
padding: 1rem;
|
348 |
+
border-radius: 8px;
|
349 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
350 |
+
}
|
351 |
+
.examples-table {
|
352 |
+
font-size: 0.9rem;
|
353 |
+
}
|
354 |
+
.gr-button.gr-button-lg {
|
355 |
+
padding: 12px 24px;
|
356 |
+
font-size: 1.1rem;
|
357 |
+
}
|
358 |
+
.gr-input, .gr-select {
|
359 |
+
border-radius: 6px;
|
360 |
+
}
|
361 |
+
.gr-form {
|
362 |
+
background: white;
|
363 |
+
padding: 20px;
|
364 |
+
border-radius: 12px;
|
365 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
|
366 |
+
}
|
367 |
+
.gr-panel {
|
368 |
+
border: none;
|
369 |
+
background: transparent;
|
370 |
+
}
|
371 |
+
.footer {
|
372 |
+
text-align: center;
|
373 |
+
margin-top: 2rem;
|
374 |
+
padding: 1rem;
|
375 |
+
color: #666;
|
376 |
+
}
|
377 |
+
""")
|
378 |
+
|
379 |
+
# Launch the demo
|
380 |
+
if __name__ == "__main__":
|
381 |
+
demo.launch(share=True)
|
382 |
+
|