Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ import torch
|
|
3 |
from transformers import AutoModelForCausalLM
|
4 |
from deepseek_vl.models import VLChatProcessor, MultiModalityCausalLM
|
5 |
from deepseek_vl.utils.io import load_pil_images
|
|
|
|
|
6 |
import spaces # Import spaces for ZeroGPU support
|
7 |
|
8 |
# Load the model and processor
|
@@ -10,57 +12,77 @@ model_path = "deepseek-ai/deepseek-vl-1.3b-chat"
|
|
10 |
vl_chat_processor = VLChatProcessor.from_pretrained(model_path)
|
11 |
tokenizer = vl_chat_processor.tokenizer
|
12 |
|
13 |
-
# Define the function for image description
|
14 |
@spaces.GPU # Ensures GPU allocation for this function
|
15 |
-
def describe_image(image):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# Gradio interface
|
58 |
def gradio_app():
|
59 |
with gr.Blocks() as demo:
|
60 |
-
gr.Markdown("# Image Description with DeepSeek VL 1.3b\n### Upload an image
|
61 |
|
62 |
with gr.Row():
|
63 |
image_input = gr.Image(type="pil", label="Upload an Image")
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
output_text = gr.Textbox(label="Image Description", interactive=False)
|
66 |
|
@@ -68,7 +90,7 @@ def gradio_app():
|
|
68 |
|
69 |
submit_btn.click(
|
70 |
fn=describe_image,
|
71 |
-
inputs=[image_input],
|
72 |
outputs=output_text
|
73 |
)
|
74 |
|
|
|
3 |
from transformers import AutoModelForCausalLM
|
4 |
from deepseek_vl.models import VLChatProcessor, MultiModalityCausalLM
|
5 |
from deepseek_vl.utils.io import load_pil_images
|
6 |
+
from io import BytesIO
|
7 |
+
from PIL import Image
|
8 |
import spaces # Import spaces for ZeroGPU support
|
9 |
|
10 |
# Load the model and processor
|
|
|
12 |
vl_chat_processor = VLChatProcessor.from_pretrained(model_path)
|
13 |
tokenizer = vl_chat_processor.tokenizer
|
14 |
|
15 |
+
# Define the function for image description with ZeroGPU support
|
16 |
@spaces.GPU # Ensures GPU allocation for this function
|
17 |
+
def describe_image(image, user_question="Describe each stage of this image."):
|
18 |
+
try:
|
19 |
+
# Convert the PIL Image to a BytesIO object for compatibility
|
20 |
+
image_byte_arr = BytesIO()
|
21 |
+
image.save(image_byte_arr, format="PNG") # Save image in PNG format
|
22 |
+
image_byte_arr.seek(0) # Move pointer to the start
|
23 |
+
|
24 |
+
# Define the conversation, using the user's question
|
25 |
+
conversation = [
|
26 |
+
{
|
27 |
+
"role": "User",
|
28 |
+
"content": f"<image_placeholder>{user_question}",
|
29 |
+
"images": [image_byte_arr] # Pass the image byte array instead of an object
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"role": "Assistant",
|
33 |
+
"content": ""
|
34 |
+
}
|
35 |
+
]
|
36 |
+
|
37 |
+
# Convert image byte array back to a PIL image for processing
|
38 |
+
pil_images = [Image.open(BytesIO(image_byte_arr.read()))] # Convert byte back to PIL Image
|
39 |
+
image_byte_arr.seek(0) # Reset the byte stream again for reuse
|
40 |
+
|
41 |
+
# Load images and prepare the inputs
|
42 |
+
prepare_inputs = vl_chat_processor(
|
43 |
+
conversations=conversation,
|
44 |
+
images=pil_images,
|
45 |
+
force_batchify=True
|
46 |
+
).to('cuda')
|
47 |
+
|
48 |
+
# Load and prepare the model
|
49 |
+
vl_gpt = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True).to(torch.bfloat16).cuda().eval()
|
50 |
+
|
51 |
+
# Generate embeddings from the image input
|
52 |
+
inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
|
53 |
+
|
54 |
+
# Generate the model's response
|
55 |
+
outputs = vl_gpt.language_model.generate(
|
56 |
+
inputs_embeds=inputs_embeds,
|
57 |
+
attention_mask=prepare_inputs.attention_mask,
|
58 |
+
pad_token_id=tokenizer.eos_token_id,
|
59 |
+
bos_token_id=tokenizer.bos_token_id,
|
60 |
+
eos_token_id=tokenizer.eos_token_id,
|
61 |
+
max_new_tokens=512,
|
62 |
+
do_sample=False,
|
63 |
+
use_cache=True
|
64 |
+
)
|
65 |
+
|
66 |
+
# Decode the generated tokens into text
|
67 |
+
answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
|
68 |
+
return answer
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
# Provide detailed error information
|
72 |
+
return f"Error: {str(e)}"
|
73 |
|
74 |
# Gradio interface
|
75 |
def gradio_app():
|
76 |
with gr.Blocks() as demo:
|
77 |
+
gr.Markdown("# Image Description with DeepSeek VL 1.3b\n### Upload an image and ask a question about it.")
|
78 |
|
79 |
with gr.Row():
|
80 |
image_input = gr.Image(type="pil", label="Upload an Image")
|
81 |
+
question_input = gr.Textbox(
|
82 |
+
label="Question (optional)",
|
83 |
+
placeholder="Enter your question about the image (default: 'Describe each stage of this image.')",
|
84 |
+
lines=2
|
85 |
+
)
|
86 |
|
87 |
output_text = gr.Textbox(label="Image Description", interactive=False)
|
88 |
|
|
|
90 |
|
91 |
submit_btn.click(
|
92 |
fn=describe_image,
|
93 |
+
inputs=[image_input, question_input], # Pass both image and question as inputs
|
94 |
outputs=output_text
|
95 |
)
|
96 |
|