Spaces:
Running
on
Zero
Running
on
Zero
fill screen height
Browse files
app.py
CHANGED
@@ -1,92 +1,93 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import TextIteratorStreamer, AutoModelForCausalLM, AutoProcessor
|
3 |
-
from threading import Thread
|
4 |
-
import re
|
5 |
-
import time
|
6 |
-
from PIL import Image
|
7 |
-
import torch
|
8 |
-
import argparse
|
9 |
-
import spaces
|
10 |
-
|
11 |
-
parser = argparse.ArgumentParser()
|
12 |
-
parser.add_argument('--model', type=str, default='aya')
|
13 |
-
args = parser.parse_args()
|
14 |
-
|
15 |
-
model_name = args.model
|
16 |
-
|
17 |
-
processor = AutoProcessor.from_pretrained(f"WueNLP/centurio_{model_name}", trust_remote_code=True)
|
18 |
-
model = AutoModelForCausalLM.from_pretrained(f"WueNLP/centurio_{model_name}",
|
19 |
-
trust_remote_code=True,
|
20 |
-
torch_dtype=torch.bfloat16,
|
21 |
-
low_cpu_mem_usage=True
|
22 |
-
).to("cuda:0")
|
23 |
-
|
24 |
-
@spaces.GPU
|
25 |
-
def bot_streaming(message, history):
|
26 |
-
if message["files"]:
|
27 |
-
image = message["files"][-1]
|
28 |
-
else:
|
29 |
-
# if there's no image uploaded for this turn, look for images in the past turns
|
30 |
-
# kept inside tuples, take the last one
|
31 |
-
for hist in history:
|
32 |
-
if type(hist[0]) == tuple:
|
33 |
-
image = hist[0][0]
|
34 |
-
|
35 |
-
if "qwen" in model_name:
|
36 |
-
if image is None:
|
37 |
-
prompt = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{message['text']}<|im_end|>\n<|im_start|>assistant\n"
|
38 |
-
else:
|
39 |
-
image = Image.open(image).convert("RGB")
|
40 |
-
prompt = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<image_placeholder>\n{message['text']}<|im_end|>\n<|im_start|>assistant\n"
|
41 |
-
else:
|
42 |
-
if image is None:
|
43 |
-
prompt = f"<BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|>{message['text']}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"
|
44 |
-
else:
|
45 |
-
image = Image.open(image).convert("RGB")
|
46 |
-
prompt = f"<BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|><image_placeholder>\n{message['text']}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"
|
47 |
-
|
48 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt").to("cuda:0", torch.bfloat16)
|
49 |
-
|
50 |
-
streamer = TextIteratorStreamer(processor, **{"skip_special_tokens": False})
|
51 |
-
generation_kwargs = dict(inputs, streamer=streamer,
|
52 |
-
do_sample=True,
|
53 |
-
num_beams=1,
|
54 |
-
repetition_penalty=1.15,
|
55 |
-
temperature=0.7,
|
56 |
-
top_p=0.8,
|
57 |
-
top_k=20,
|
58 |
-
max_new_tokens=512, min_new_tokens=1)
|
59 |
-
|
60 |
-
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
61 |
-
thread.start()
|
62 |
-
|
63 |
-
buffer = ""
|
64 |
-
for new_text in streamer:
|
65 |
-
buffer += new_text
|
66 |
-
if "qwen" in model_name:
|
67 |
-
generated_text_without_prompt = buffer.split("<|im_start|>assistant\n")[-1].split("<|im_end|>")[0]
|
68 |
-
else:
|
69 |
-
generated_text_without_prompt = buffer.split("<|CHATBOT_TOKEN|>")[-1].split("<|END_OF_TURN_TOKEN|>")[0]
|
70 |
-
|
71 |
-
time.sleep(0.04)
|
72 |
-
yield generated_text_without_prompt
|
73 |
-
|
74 |
-
|
75 |
-
description = ("""# [Centurio: On Drivers of Multilingual Ability of Large Vision-Language Model](gregor-ge.github.io/Centurio/)
|
76 |
-
Try [Centurio](https://huggingface.co/collections/WueNLP/centurio-677cf0ab6ddea874927a154e), a massively multilingual large vision-language model, in this demo (specifically, [Centurio Aya](https://huggingface.co/WueNLP/centurio_aya)).
|
77 |
-
Upload an image and start chatting about it, or try one of the examples below.
|
78 |
-
|
79 |
-
Centurio is trained with 100 languages but quality of answers can differ greatly depending on your language.
|
80 |
-
Centurio is trained to read text in images but struggles with small text and with non-Latin scripts.
|
81 |
-
|
82 |
-
> If you don't upload an image, you will receive an error.
|
83 |
-
> This demo does not support multi-image prompts or multi-turn dialog. Every new prompt will refer to the last image (if no new image is included) without prior dialog as context.""")
|
84 |
-
|
85 |
-
demo = gr.ChatInterface(fn=bot_streaming, title="Centurio Demo",
|
86 |
-
examples=[{"text": "What is on the flower?", "files": ["./bee.jpg"]},
|
87 |
-
{"text": "How to make this pastry?", "files": ["./baklava.png"]}],
|
88 |
-
description=description,
|
89 |
-
stop_btn="Stop Generation",
|
90 |
-
multimodal=True
|
91 |
-
|
|
|
92 |
demo.launch(debug=True, share=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import TextIteratorStreamer, AutoModelForCausalLM, AutoProcessor
|
3 |
+
from threading import Thread
|
4 |
+
import re
|
5 |
+
import time
|
6 |
+
from PIL import Image
|
7 |
+
import torch
|
8 |
+
import argparse
|
9 |
+
import spaces
|
10 |
+
|
11 |
+
parser = argparse.ArgumentParser()
|
12 |
+
parser.add_argument('--model', type=str, default='aya')
|
13 |
+
args = parser.parse_args()
|
14 |
+
|
15 |
+
model_name = args.model
|
16 |
+
|
17 |
+
processor = AutoProcessor.from_pretrained(f"WueNLP/centurio_{model_name}", trust_remote_code=True)
|
18 |
+
model = AutoModelForCausalLM.from_pretrained(f"WueNLP/centurio_{model_name}",
|
19 |
+
trust_remote_code=True,
|
20 |
+
torch_dtype=torch.bfloat16,
|
21 |
+
low_cpu_mem_usage=True
|
22 |
+
).to("cuda:0")
|
23 |
+
|
24 |
+
@spaces.GPU
|
25 |
+
def bot_streaming(message, history):
|
26 |
+
if message["files"]:
|
27 |
+
image = message["files"][-1]
|
28 |
+
else:
|
29 |
+
# if there's no image uploaded for this turn, look for images in the past turns
|
30 |
+
# kept inside tuples, take the last one
|
31 |
+
for hist in history:
|
32 |
+
if type(hist[0]) == tuple:
|
33 |
+
image = hist[0][0]
|
34 |
+
|
35 |
+
if "qwen" in model_name:
|
36 |
+
if image is None:
|
37 |
+
prompt = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{message['text']}<|im_end|>\n<|im_start|>assistant\n"
|
38 |
+
else:
|
39 |
+
image = Image.open(image).convert("RGB")
|
40 |
+
prompt = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<image_placeholder>\n{message['text']}<|im_end|>\n<|im_start|>assistant\n"
|
41 |
+
else:
|
42 |
+
if image is None:
|
43 |
+
prompt = f"<BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|>{message['text']}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"
|
44 |
+
else:
|
45 |
+
image = Image.open(image).convert("RGB")
|
46 |
+
prompt = f"<BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|><image_placeholder>\n{message['text']}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"
|
47 |
+
|
48 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to("cuda:0", torch.bfloat16)
|
49 |
+
|
50 |
+
streamer = TextIteratorStreamer(processor, **{"skip_special_tokens": False})
|
51 |
+
generation_kwargs = dict(inputs, streamer=streamer,
|
52 |
+
do_sample=True,
|
53 |
+
num_beams=1,
|
54 |
+
repetition_penalty=1.15,
|
55 |
+
temperature=0.7,
|
56 |
+
top_p=0.8,
|
57 |
+
top_k=20,
|
58 |
+
max_new_tokens=512, min_new_tokens=1)
|
59 |
+
|
60 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
61 |
+
thread.start()
|
62 |
+
|
63 |
+
buffer = ""
|
64 |
+
for new_text in streamer:
|
65 |
+
buffer += new_text
|
66 |
+
if "qwen" in model_name:
|
67 |
+
generated_text_without_prompt = buffer.split("<|im_start|>assistant\n")[-1].split("<|im_end|>")[0]
|
68 |
+
else:
|
69 |
+
generated_text_without_prompt = buffer.split("<|CHATBOT_TOKEN|>")[-1].split("<|END_OF_TURN_TOKEN|>")[0]
|
70 |
+
|
71 |
+
time.sleep(0.04)
|
72 |
+
yield generated_text_without_prompt
|
73 |
+
|
74 |
+
|
75 |
+
description = ("""# [Centurio: On Drivers of Multilingual Ability of Large Vision-Language Model](gregor-ge.github.io/Centurio/)
|
76 |
+
Try [Centurio](https://huggingface.co/collections/WueNLP/centurio-677cf0ab6ddea874927a154e), a massively multilingual large vision-language model, in this demo (specifically, [Centurio Aya](https://huggingface.co/WueNLP/centurio_aya)).
|
77 |
+
Upload an image and start chatting about it, or try one of the examples below.
|
78 |
+
|
79 |
+
Centurio is trained with 100 languages but quality of answers can differ greatly depending on your language.
|
80 |
+
Centurio is trained to read text in images but struggles with small text and with non-Latin scripts.
|
81 |
+
|
82 |
+
> If you don't upload an image, you will receive an error.
|
83 |
+
> This demo does not support multi-image prompts or multi-turn dialog. Every new prompt will refer to the last image (if no new image is included) without prior dialog as context.""")
|
84 |
+
|
85 |
+
demo = gr.ChatInterface(fn=bot_streaming, title="Centurio Demo",
|
86 |
+
examples=[{"text": "What is on the flower?", "files": ["./bee.jpg"]},
|
87 |
+
{"text": "How to make this pastry?", "files": ["./baklava.png"]}],
|
88 |
+
description=description,
|
89 |
+
stop_btn="Stop Generation",
|
90 |
+
multimodal=True,
|
91 |
+
fill_height=True,
|
92 |
+
)
|
93 |
demo.launch(debug=True, share=True)
|