Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,8 @@
|
|
1 |
-
from transformers import
|
|
|
|
|
|
|
|
|
2 |
from PIL import Image
|
3 |
import requests
|
4 |
import torch
|
@@ -7,62 +11,89 @@ import gradio as gr
|
|
7 |
from gradio import FileData
|
8 |
import time
|
9 |
import spaces
|
|
|
|
|
10 |
ckpt = "toandev/Viet-Receipt-Llama-3.2-11B-Vision-Instruct"
|
11 |
-
model = MllamaForConditionalGeneration.from_pretrained(
|
12 |
-
torch_dtype=torch.bfloat16
|
|
|
13 |
processor = AutoProcessor.from_pretrained(ckpt)
|
14 |
|
15 |
|
16 |
@spaces.GPU
|
17 |
def bot_streaming(message, history, max_new_tokens=250):
|
18 |
-
|
19 |
txt = message["text"]
|
20 |
ext_buffer = f"{txt}"
|
21 |
-
|
22 |
-
messages= []
|
23 |
images = []
|
24 |
-
|
25 |
|
26 |
-
for i, msg in enumerate(history):
|
27 |
if isinstance(msg[0], tuple):
|
28 |
-
messages.append(
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
images.append(Image.open(msg[0][0]).convert("RGB"))
|
31 |
-
elif isinstance(history[i-1], tuple) and isinstance(msg[0], str):
|
32 |
# messages are already handled
|
33 |
pass
|
34 |
-
elif isinstance(history[i-1][0], str) and isinstance(
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# add current message
|
39 |
if len(message["files"]) == 1:
|
40 |
-
|
41 |
-
if isinstance(message["files"][0], str):
|
42 |
image = Image.open(message["files"][0]).convert("RGB")
|
43 |
-
else:
|
44 |
image = Image.open(message["files"][0]["path"]).convert("RGB")
|
45 |
images.append(image)
|
46 |
-
messages.append(
|
|
|
|
|
|
|
|
|
|
|
47 |
else:
|
48 |
messages.append({"role": "user", "content": [{"type": "text", "text": txt}]})
|
49 |
|
50 |
-
|
51 |
texts = processor.apply_chat_template(messages, add_generation_prompt=True)
|
52 |
|
53 |
if images == []:
|
54 |
inputs = processor(text=texts, return_tensors="pt").to("cuda")
|
55 |
else:
|
56 |
inputs = processor(text=texts, images=images, return_tensors="pt").to("cuda")
|
57 |
-
streamer = TextIteratorStreamer(
|
|
|
|
|
58 |
|
59 |
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=max_new_tokens)
|
60 |
generated_text = ""
|
61 |
-
|
62 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
63 |
thread.start()
|
64 |
buffer = ""
|
65 |
-
|
66 |
for new_text in streamer:
|
67 |
buffer += new_text
|
68 |
generated_text_without_prompt = buffer
|
@@ -70,31 +101,24 @@ def bot_streaming(message, history, max_new_tokens=250):
|
|
70 |
yield buffer
|
71 |
|
72 |
|
73 |
-
demo = gr.ChatInterface(
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
84 |
],
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
],
|
94 |
-
cache_examples=False,
|
95 |
-
description="Try Multimodal Llama by Meta with transformers in this demo. Upload an image, and start chatting about it, or simply try one of the examples below. To learn more about Llama Vision, visit [our blog post](https://huggingface.co/blog/llama32). ",
|
96 |
-
stop_btn="Stop Generation",
|
97 |
-
fill_height=True,
|
98 |
-
multimodal=True)
|
99 |
-
|
100 |
-
demo.launch(debug=True)
|
|
|
1 |
+
from transformers import (
|
2 |
+
MllamaForConditionalGeneration,
|
3 |
+
AutoProcessor,
|
4 |
+
TextIteratorStreamer,
|
5 |
+
)
|
6 |
from PIL import Image
|
7 |
import requests
|
8 |
import torch
|
|
|
11 |
from gradio import FileData
|
12 |
import time
|
13 |
import spaces
|
14 |
+
|
15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
ckpt = "toandev/Viet-Receipt-Llama-3.2-11B-Vision-Instruct"
|
17 |
+
model = MllamaForConditionalGeneration.from_pretrained(
|
18 |
+
ckpt, torch_dtype=torch.bfloat16
|
19 |
+
).to(device)
|
20 |
processor = AutoProcessor.from_pretrained(ckpt)
|
21 |
|
22 |
|
23 |
@spaces.GPU
|
24 |
def bot_streaming(message, history, max_new_tokens=250):
|
25 |
+
|
26 |
txt = message["text"]
|
27 |
ext_buffer = f"{txt}"
|
28 |
+
|
29 |
+
messages = []
|
30 |
images = []
|
|
|
31 |
|
32 |
+
for i, msg in enumerate(history):
|
33 |
if isinstance(msg[0], tuple):
|
34 |
+
messages.append(
|
35 |
+
{
|
36 |
+
"role": "user",
|
37 |
+
"content": [
|
38 |
+
{"type": "text", "text": history[i + 1][0]},
|
39 |
+
{"type": "image"},
|
40 |
+
],
|
41 |
+
}
|
42 |
+
)
|
43 |
+
messages.append(
|
44 |
+
{
|
45 |
+
"role": "assistant",
|
46 |
+
"content": [{"type": "text", "text": history[i + 1][1]}],
|
47 |
+
}
|
48 |
+
)
|
49 |
images.append(Image.open(msg[0][0]).convert("RGB"))
|
50 |
+
elif isinstance(history[i - 1], tuple) and isinstance(msg[0], str):
|
51 |
# messages are already handled
|
52 |
pass
|
53 |
+
elif isinstance(history[i - 1][0], str) and isinstance(
|
54 |
+
msg[0], str
|
55 |
+
): # text only turn
|
56 |
+
messages.append(
|
57 |
+
{"role": "user", "content": [{"type": "text", "text": msg[0]}]}
|
58 |
+
)
|
59 |
+
messages.append(
|
60 |
+
{"role": "assistant", "content": [{"type": "text", "text": msg[1]}]}
|
61 |
+
)
|
62 |
|
63 |
# add current message
|
64 |
if len(message["files"]) == 1:
|
65 |
+
|
66 |
+
if isinstance(message["files"][0], str): # examples
|
67 |
image = Image.open(message["files"][0]).convert("RGB")
|
68 |
+
else: # regular input
|
69 |
image = Image.open(message["files"][0]["path"]).convert("RGB")
|
70 |
images.append(image)
|
71 |
+
messages.append(
|
72 |
+
{
|
73 |
+
"role": "user",
|
74 |
+
"content": [{"type": "text", "text": txt}, {"type": "image"}],
|
75 |
+
}
|
76 |
+
)
|
77 |
else:
|
78 |
messages.append({"role": "user", "content": [{"type": "text", "text": txt}]})
|
79 |
|
|
|
80 |
texts = processor.apply_chat_template(messages, add_generation_prompt=True)
|
81 |
|
82 |
if images == []:
|
83 |
inputs = processor(text=texts, return_tensors="pt").to("cuda")
|
84 |
else:
|
85 |
inputs = processor(text=texts, images=images, return_tensors="pt").to("cuda")
|
86 |
+
streamer = TextIteratorStreamer(
|
87 |
+
processor, skip_special_tokens=True, skip_prompt=True
|
88 |
+
)
|
89 |
|
90 |
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=max_new_tokens)
|
91 |
generated_text = ""
|
92 |
+
|
93 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
94 |
thread.start()
|
95 |
buffer = ""
|
96 |
+
|
97 |
for new_text in streamer:
|
98 |
buffer += new_text
|
99 |
generated_text_without_prompt = buffer
|
|
|
101 |
yield buffer
|
102 |
|
103 |
|
104 |
+
demo = gr.ChatInterface(
|
105 |
+
fn=bot_streaming,
|
106 |
+
title="Multimodal Llama",
|
107 |
+
textbox=gr.MultimodalTextbox(),
|
108 |
+
additional_inputs=[
|
109 |
+
gr.Slider(
|
110 |
+
minimum=10,
|
111 |
+
maximum=500,
|
112 |
+
value=250,
|
113 |
+
step=10,
|
114 |
+
label="Maximum number of new tokens to generate",
|
115 |
+
)
|
116 |
],
|
117 |
+
cache_examples=False,
|
118 |
+
description="Try Multimodal Llama by Meta with transformers in this demo. Upload an image, and start chatting about it, or simply try one of the examples below. To learn more about Llama Vision, visit [our blog post](https://huggingface.co/blog/llama32). ",
|
119 |
+
stop_btn="Stop Generation",
|
120 |
+
fill_height=True,
|
121 |
+
multimodal=True,
|
122 |
+
)
|
123 |
+
|
124 |
+
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|