Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
add space
Browse files- .gitattributes +3 -0
- README.md +3 -3
- app.py +139 -0
- example_images/campeones.jpg +3 -0
- example_images/document.jpg +3 -0
- example_images/dogs.jpg +3 -0
- example_images/examples_invoice.png +3 -0
- example_images/examples_weather_events.png +3 -0
- example_images/fiche.jpg +3 -0
- example_images/math.jpg +3 -0
- example_images/mosque.jpg +3 -0
- example_images/newyork.jpg +3 -0
- example_images/rococo.jpg +3 -0
- example_images/rococo_1.jpg +3 -0
- example_images/s2w_example.png +3 -0
- example_images/short.mp4 +3 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
38 |
+
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Vision Langage Openvino
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.35.0
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
title: Vision Langage Openvino
|
3 |
+
emoji: 📊
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.35.0
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText, TextIteratorStreamer
|
3 |
+
from threading import Thread
|
4 |
+
import re
|
5 |
+
import time
|
6 |
+
|
7 |
+
from optimum.intel import OVModelForVisualCausalLM
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
# model_id = "echarlaix/SmolVLM2-2.2B-Instruct-openvino"
|
12 |
+
model_id = "echarlaix/SmolVLM-256M-Instruct-openvino"
|
13 |
+
|
14 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
15 |
+
model = OVModelForVisualCausalLM.from_pretrained(model_id)
|
16 |
+
|
17 |
+
def model_inference(input_dict, history, max_tokens):
|
18 |
+
text = input_dict["text"]
|
19 |
+
images = []
|
20 |
+
user_content = []
|
21 |
+
media_queue = []
|
22 |
+
if history == []:
|
23 |
+
text = input_dict["text"].strip()
|
24 |
+
|
25 |
+
for file in input_dict.get("files", []):
|
26 |
+
if file.endswith((".png", ".jpg", ".jpeg", ".gif", ".bmp")):
|
27 |
+
media_queue.append({"type": "image", "path": file})
|
28 |
+
elif file.endswith((".mp4", ".mov", ".avi", ".mkv", ".flv")):
|
29 |
+
media_queue.append({"type": "video", "path": file})
|
30 |
+
|
31 |
+
if "<image>" in text or "<video>" in text:
|
32 |
+
parts = re.split(r'(<image>|<video>)', text)
|
33 |
+
for part in parts:
|
34 |
+
if part == "<image>" and media_queue:
|
35 |
+
user_content.append(media_queue.pop(0))
|
36 |
+
elif part == "<video>" and media_queue:
|
37 |
+
user_content.append(media_queue.pop(0))
|
38 |
+
elif part.strip():
|
39 |
+
user_content.append({"type": "text", "text": part.strip()})
|
40 |
+
else:
|
41 |
+
user_content.append({"type": "text", "text": text})
|
42 |
+
|
43 |
+
for media in media_queue:
|
44 |
+
user_content.append(media)
|
45 |
+
|
46 |
+
resulting_messages = [{"role": "user", "content": user_content}]
|
47 |
+
|
48 |
+
elif len(history) > 0:
|
49 |
+
resulting_messages = []
|
50 |
+
user_content = []
|
51 |
+
media_queue = []
|
52 |
+
for hist in history:
|
53 |
+
if hist["role"] == "user" and isinstance(hist["content"], tuple):
|
54 |
+
file_name = hist["content"][0]
|
55 |
+
if file_name.endswith((".png", ".jpg", ".jpeg")):
|
56 |
+
media_queue.append({"type": "image", "path": file_name})
|
57 |
+
elif file_name.endswith(".mp4"):
|
58 |
+
media_queue.append({"type": "video", "path": file_name})
|
59 |
+
|
60 |
+
|
61 |
+
for hist in history:
|
62 |
+
if hist["role"] == "user" and isinstance(hist["content"], str):
|
63 |
+
text = hist["content"]
|
64 |
+
parts = re.split(r'(<image>|<video>)', text)
|
65 |
+
|
66 |
+
for part in parts:
|
67 |
+
if part == "<image>" and media_queue:
|
68 |
+
user_content.append(media_queue.pop(0))
|
69 |
+
elif part == "<video>" and media_queue:
|
70 |
+
user_content.append(media_queue.pop(0))
|
71 |
+
elif part.strip():
|
72 |
+
user_content.append({"type": "text", "text": part.strip()})
|
73 |
+
|
74 |
+
elif hist["role"] == "assistant":
|
75 |
+
resulting_messages.append({
|
76 |
+
"role": "user",
|
77 |
+
"content": user_content
|
78 |
+
})
|
79 |
+
resulting_messages.append({
|
80 |
+
"role": "assistant",
|
81 |
+
"content": [{"type": "text", "text": hist["content"]}]
|
82 |
+
})
|
83 |
+
user_content = []
|
84 |
+
|
85 |
+
|
86 |
+
if text == "" and not images:
|
87 |
+
gr.Error("Please input a query and optionally image(s).")
|
88 |
+
|
89 |
+
if text == "" and images:
|
90 |
+
gr.Error("Please input a text query along the images(s).")
|
91 |
+
print("resulting_messages", resulting_messages)
|
92 |
+
inputs = processor.apply_chat_template(
|
93 |
+
resulting_messages,
|
94 |
+
add_generation_prompt=True,
|
95 |
+
tokenize=True,
|
96 |
+
return_dict=True,
|
97 |
+
return_tensors="pt",
|
98 |
+
)
|
99 |
+
|
100 |
+
# Generate
|
101 |
+
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
102 |
+
generation_args = dict(inputs, streamer=streamer, max_new_tokens=max_tokens)
|
103 |
+
# generated_text = ""
|
104 |
+
|
105 |
+
thread = Thread(target=model.generate, kwargs=generation_args)
|
106 |
+
thread.start()
|
107 |
+
|
108 |
+
yield "..."
|
109 |
+
buffer = ""
|
110 |
+
|
111 |
+
for new_text in streamer:
|
112 |
+
|
113 |
+
buffer += new_text
|
114 |
+
# generated_text_without_prompt = buffer#[len(ext_buffer):]
|
115 |
+
time.sleep(0.01)
|
116 |
+
yield buffer
|
117 |
+
|
118 |
+
|
119 |
+
examples=[
|
120 |
+
[{"text": "Where do the severe droughts happen according to this diagram?", "files": ["example_images/examples_weather_events.png"]}],
|
121 |
+
[{"text": "What art era this artpiece <image> and this artpiece <image> belong to?", "files": ["example_images/rococo.jpg", "example_images/rococo_1.jpg"]}],
|
122 |
+
[{"text": "Describe this image.", "files": ["example_images/mosque.jpg"]}],
|
123 |
+
[{"text": "When was this purchase made and how much did it cost?", "files": ["example_images/fiche.jpg"]}],
|
124 |
+
[{"text": "What is the date in this document?", "files": ["example_images/document.jpg"]}],
|
125 |
+
[{"text": "What is happening in the video?", "files": ["example_images/short.mp4"]}],
|
126 |
+
]
|
127 |
+
demo = gr.ChatInterface(fn=model_inference, title="SmolVLM2: The Smollest Video Model Ever 📺",
|
128 |
+
description="Play with [SmolVLM2-2.2B-Instruct](https://huggingface.co/HuggingFaceTB/SmolVLM2-2.2B-Instruct) in this demo. To get started, upload an image and text or try one of the examples. This demo doesn't use history for the chat, so every chat you start is a new conversation.",
|
129 |
+
examples=examples,
|
130 |
+
textbox=gr.MultimodalTextbox(label="Query Input", file_types=["image", ".mp4"], file_count="multiple"), stop_btn="Stop Generation", multimodal=True,
|
131 |
+
cache_examples=False,
|
132 |
+
additional_inputs=[gr.Slider(minimum=100, maximum=500, step=50, value=200, label="Max Tokens")],
|
133 |
+
type="messages"
|
134 |
+
)
|
135 |
+
|
136 |
+
|
137 |
+
|
138 |
+
demo.launch(debug=True)
|
139 |
+
|
example_images/campeones.jpg
ADDED
![]() |
Git LFS Details
|
example_images/document.jpg
ADDED
![]() |
Git LFS Details
|
example_images/dogs.jpg
ADDED
![]() |
Git LFS Details
|
example_images/examples_invoice.png
ADDED
![]() |
Git LFS Details
|
example_images/examples_weather_events.png
ADDED
![]() |
Git LFS Details
|
example_images/fiche.jpg
ADDED
![]() |
Git LFS Details
|
example_images/math.jpg
ADDED
![]() |
Git LFS Details
|
example_images/mosque.jpg
ADDED
![]() |
Git LFS Details
|
example_images/newyork.jpg
ADDED
![]() |
Git LFS Details
|
example_images/rococo.jpg
ADDED
![]() |
Git LFS Details
|
example_images/rococo_1.jpg
ADDED
![]() |
Git LFS Details
|
example_images/s2w_example.png
ADDED
![]() |
Git LFS Details
|
example_images/short.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3df3c33fe94db5e7406f86e7d94c4ec8804124167f772912ce58dff17dc79440
|
3 |
+
size 4028661
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub
|
2 |
+
gradio
|
3 |
+
optimum-intel[openvino]==1.24
|
4 |
+
nncf
|