Upload 3 files
Browse files- README.md +5 -5
- app.py +100 -0
- requirements.txt +10 -0
README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
---
|
| 2 |
title: DeepSeek VL 1.3B Chat
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
license:
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
title: DeepSeek VL 1.3B Chat
|
| 3 |
+
emoji: 🐬 ⚡️ 🐬
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.1.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
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 |
+
from io import BytesIO
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import spaces # Import spaces for ZeroGPU support
|
| 9 |
+
|
| 10 |
+
# Load the model and processor
|
| 11 |
+
model_path = "deepseek-ai/deepseek-vl-1.3b-chat"
|
| 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 this image in great detail."):
|
| 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="Ask a question about the image (e.g., 'What is happening in this image?')",
|
| 84 |
+
lines=2
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
output_text = gr.Textbox(label="Image Description", interactive=False)
|
| 88 |
+
|
| 89 |
+
submit_btn = gr.Button("Generate Description")
|
| 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 |
+
|
| 97 |
+
demo.launch()
|
| 98 |
+
|
| 99 |
+
# Launch the Gradio app
|
| 100 |
+
gradio_app()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core requirements
|
| 2 |
+
bitsandbytes
|
| 3 |
+
transformers
|
| 4 |
+
huggingface_hub
|
| 5 |
+
accelerate
|
| 6 |
+
gradio
|
| 7 |
+
git+https://github.com/deepseek-ai/DeepSeek-VL
|
| 8 |
+
spaces
|
| 9 |
+
Pillow
|
| 10 |
+
torch
|