Spaces:
Running
on
Zero
Running
on
Zero
from PIL import Image | |
import spaces | |
import gradio as gr | |
MODEL_ID = "davidr99/qwen2-7b-instruct-blackjack" | |
def blackjack_ai(image): | |
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor | |
from qwen_vl_utils import process_vision_info | |
model = Qwen2VLForConditionalGeneration.from_pretrained(MODEL_ID, torch_dtype="auto", device_map="auto") | |
processor = AutoProcessor.from_pretrained(MODEL_ID) | |
instruction = "extract json from this image." | |
messages = [ | |
{"role": "user", "content": [ | |
{"type": "image", "image": image}, | |
{"type": "text", "text": instruction} | |
]} | |
] | |
print(messages) | |
# Preparation for inference | |
text = processor.apply_chat_template( | |
messages, tokenize=False, add_generation_prompt=True | |
) | |
image_inputs, video_inputs = process_vision_info(messages) | |
inputs = processor( | |
text=[text], | |
images=image_inputs, | |
videos=video_inputs, | |
padding=True, | |
return_tensors="pt", | |
) | |
inputs = inputs.to("cuda") | |
# Inference: Generation of the output | |
generated_ids = model.generate(**inputs, max_new_tokens=128) | |
generated_ids_trimmed = [ | |
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) | |
] | |
output_text = processor.batch_decode( | |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False | |
) | |
return output_text | |
with gr.Blocks() as demo: | |
image = gr.Image(type="filepath") | |
submit = gr.Button("Submit") | |
output = gr.TextArea() | |
submit.click(blackjack_ai, inputs=[image], outputs=[output]) | |
demo.launch() | |