Spaces:
Sleeping
Sleeping
Update to use transfomers
Browse files- app.py +35 -23
- requirements.txt +3 -1
app.py
CHANGED
@@ -3,43 +3,55 @@ import spaces
|
|
3 |
|
4 |
import gradio as gr
|
5 |
|
|
|
|
|
6 |
@spaces.GPU(duration=30)
|
7 |
def blackjack_ai(image):
|
8 |
-
from
|
|
|
9 |
|
10 |
-
model
|
11 |
-
|
12 |
-
load_in_4bit = True, # Set to False for 16bit LoRA
|
13 |
-
)
|
14 |
-
FastVisionModel.for_inference(model) # Enable for inference!
|
15 |
|
16 |
-
|
17 |
-
instruction = "Write the LaTeX representation for this image."
|
18 |
|
19 |
messages = [
|
20 |
{"role": "user", "content": [
|
21 |
-
{"type": "image"},
|
22 |
{"type": "text", "text": instruction}
|
23 |
]}
|
24 |
]
|
25 |
-
input_text = tokenizer.apply_chat_template(messages, add_generation_prompt = True)
|
26 |
-
inputs = tokenizer(
|
27 |
-
image,
|
28 |
-
input_text,
|
29 |
-
add_special_tokens = False,
|
30 |
-
return_tensors = "pt",
|
31 |
-
).to("cuda")
|
32 |
-
|
33 |
-
from transformers import TextStreamer
|
34 |
-
text_streamer = TextStreamer(tokenizer, skip_prompt = True)
|
35 |
-
_ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 128,
|
36 |
-
use_cache = True, temperature = 1.5, min_p = 0.1)
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
with gr.Blocks() as demo:
|
41 |
|
42 |
-
image = gr.Image()
|
43 |
submit = gr.Button("Submit")
|
44 |
output = gr.TextArea()
|
45 |
|
|
|
3 |
|
4 |
import gradio as gr
|
5 |
|
6 |
+
MODEL_ID = "davidr99/qwen2-7b-instruct-blackjack"
|
7 |
+
|
8 |
@spaces.GPU(duration=30)
|
9 |
def blackjack_ai(image):
|
10 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
11 |
+
from qwen_vl_utils import process_vision_info
|
12 |
|
13 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(MODEL_ID, torch_dtype="auto", device_map="auto")
|
14 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
|
|
|
|
|
|
15 |
|
16 |
+
instruction = "extract json from this image."
|
|
|
17 |
|
18 |
messages = [
|
19 |
{"role": "user", "content": [
|
20 |
+
{"type": "image", "image": image},
|
21 |
{"type": "text", "text": instruction}
|
22 |
]}
|
23 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
print(messages)
|
26 |
+
|
27 |
+
# Preparation for inference
|
28 |
+
text = processor.apply_chat_template(
|
29 |
+
messages, tokenize=False, add_generation_prompt=True
|
30 |
+
)
|
31 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
32 |
+
inputs = processor(
|
33 |
+
text=[text],
|
34 |
+
images=image_inputs,
|
35 |
+
videos=video_inputs,
|
36 |
+
padding=True,
|
37 |
+
return_tensors="pt",
|
38 |
+
)
|
39 |
+
inputs = inputs.to("cuda")
|
40 |
+
|
41 |
+
# Inference: Generation of the output
|
42 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
43 |
+
generated_ids_trimmed = [
|
44 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
45 |
+
]
|
46 |
+
output_text = processor.batch_decode(
|
47 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
48 |
+
)
|
49 |
+
|
50 |
+
return output_text
|
51 |
|
52 |
with gr.Blocks() as demo:
|
53 |
|
54 |
+
image = gr.Image(type="filepath")
|
55 |
submit = gr.Button("Submit")
|
56 |
output = gr.TextArea()
|
57 |
|
requirements.txt
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
transformers
|
2 |
gradio
|
3 |
-
unsloth
|
4 |
pillow
|
|
|
|
|
|
|
5 |
spaces
|
|
|
1 |
transformers
|
2 |
gradio
|
|
|
3 |
pillow
|
4 |
+
qwen-vl-utils
|
5 |
+
torchvision
|
6 |
+
torch
|
7 |
spaces
|