Spaces:
Running
on
Zero
Running
on
Zero
runninglsy
commited on
Commit
β’
42fea26
1
Parent(s):
2005ef8
add chatbot code
Browse files
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
title: Ovis1.6 Gemma2 9B
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
|
|
1 |
---
|
2 |
title: Ovis1.6 Gemma2 9B
|
3 |
+
emoji: π
|
4 |
+
colorFrom: gray
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.36.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
app.py
CHANGED
@@ -1,7 +1,100 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
demo.launch()
|
|
|
1 |
+
import spaces
|
2 |
+
import os
|
3 |
+
|
4 |
import gradio as gr
|
5 |
+
import torch
|
6 |
+
from transformers import AutoModelForCausalLM
|
7 |
+
|
8 |
+
model_name = 'AIDC-AI/Ovis1.6-Gemma2-9B'
|
9 |
+
|
10 |
+
# load model
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name,
|
12 |
+
torch_dtype=torch.bfloat16,
|
13 |
+
multimodal_max_length=8192,
|
14 |
+
trust_remote_code=True).to(device='cuda')
|
15 |
+
text_tokenizer = model.get_text_tokenizer()
|
16 |
+
visual_tokenizer = model.get_visual_tokenizer()
|
17 |
+
image_placeholder = '<image>'
|
18 |
+
|
19 |
+
|
20 |
+
@spaces.GPU
|
21 |
+
def ovis_chat(chatbot, image_input, text_input):
|
22 |
+
# preprocess inputs
|
23 |
+
conversations = []
|
24 |
+
for query, response in chatbot:
|
25 |
+
conversations.append({
|
26 |
+
"from": "human",
|
27 |
+
"value": query
|
28 |
+
})
|
29 |
+
conversations.append({
|
30 |
+
"from": "gpt",
|
31 |
+
"value": response
|
32 |
+
})
|
33 |
+
text_input = text_input.replace(image_placeholder, '')
|
34 |
+
conversations.append({
|
35 |
+
"from": "human",
|
36 |
+
"value": text_input
|
37 |
+
})
|
38 |
+
if image_input is not None:
|
39 |
+
conversations[0]["value"] = image_placeholder + '\n' + conversations[0]["value"]
|
40 |
+
prompt, input_ids, pixel_values = model.preprocess_inputs(conversations, [image_input])
|
41 |
+
attention_mask = torch.ne(input_ids, text_tokenizer.pad_token_id)
|
42 |
+
input_ids = input_ids.unsqueeze(0).to(device=model.device)
|
43 |
+
attention_mask = attention_mask.unsqueeze(0).to(device=model.device)
|
44 |
+
if image_input is None:
|
45 |
+
pixel_values = [None]
|
46 |
+
else:
|
47 |
+
pixel_values = [pixel_values.to(dtype=visual_tokenizer.dtype, device=visual_tokenizer.device)]
|
48 |
+
|
49 |
+
# generate output
|
50 |
+
with torch.inference_mode():
|
51 |
+
gen_kwargs = dict(
|
52 |
+
max_new_tokens=512,
|
53 |
+
do_sample=False,
|
54 |
+
top_p=None,
|
55 |
+
top_k=None,
|
56 |
+
temperature=None,
|
57 |
+
repetition_penalty=None,
|
58 |
+
eos_token_id=model.generation_config.eos_token_id,
|
59 |
+
pad_token_id=text_tokenizer.pad_token_id,
|
60 |
+
use_cache=True
|
61 |
+
)
|
62 |
+
output_ids = model.generate(input_ids, pixel_values=pixel_values, attention_mask=attention_mask, **gen_kwargs)[0]
|
63 |
+
output = text_tokenizer.decode(output_ids, skip_special_tokens=True)
|
64 |
+
chatbot.append((text_input, output))
|
65 |
+
|
66 |
+
return chatbot, ""
|
67 |
+
|
68 |
+
|
69 |
+
def clear_chat():
|
70 |
+
return [], None, ""
|
71 |
+
|
72 |
+
md = f'''# <center>{model_name.split('/')[-1]}</center>
|
73 |
+
###
|
74 |
+
Ovis has been open-sourced on [GitHub](https://github.com/AIDC-AI/Ovis) and [Huggingface](https://huggingface.co/{model_name}). If you find Ovis useful, a star or a like would be appreciated.
|
75 |
+
'''
|
76 |
+
|
77 |
+
text_input = gr.Textbox(label="prompt", placeholder="Enter your text here...", lines=1, container=False)
|
78 |
+
with gr.Blocks(title=model_name.split('/')[-1]) as demo:
|
79 |
+
gr.Markdown(md)
|
80 |
+
cur_dir = os.path.dirname(os.path.abspath(__file__))
|
81 |
+
with gr.Row():
|
82 |
+
with gr.Column(scale=3):
|
83 |
+
image_input = gr.Image(label="image", height=350, type="pil")
|
84 |
+
gr.Examples(
|
85 |
+
examples=[
|
86 |
+
[f"{cur_dir}/examples/rs-1.png", "What shape should come as the fourth shape?"]],
|
87 |
+
inputs=[image_input, text_input]
|
88 |
+
)
|
89 |
+
with gr.Column(scale=7):
|
90 |
+
chatbot = gr.Chatbot(label="Ovis", layout="panel", height=470, show_copy_button=True)
|
91 |
+
text_input.render()
|
92 |
+
with gr.Row():
|
93 |
+
send_btn = gr.Button("Send", variant="primary")
|
94 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
95 |
|
96 |
+
send_click_event = send_btn.click(ovis_chat, [chatbot, image_input, text_input], [chatbot, text_input])
|
97 |
+
submit_event = text_input.submit(ovis_chat, [chatbot, image_input, text_input], [chatbot, text_input])
|
98 |
+
clear_btn.click(clear_chat, outputs=[chatbot, image_input, text_input])
|
99 |
|
|
|
100 |
demo.launch()
|