Spaces:
Runtime error
Runtime error
Commit
•
86cffc0
1
Parent(s):
970607e
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,119 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
""
|
43 |
-
|
44 |
-
""
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
|
62 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import spaces
|
4 |
|
5 |
+
import os
|
6 |
+
import warnings
|
7 |
+
import shutil
|
8 |
+
import time
|
9 |
+
from threading import Thread
|
10 |
+
|
11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig, AutoProcessor
|
12 |
+
from transformers import TextIteratorStreamer
|
13 |
+
import torch
|
14 |
+
from dc.model import *
|
15 |
+
from dc.constants import DEFAULT_IMAGE_PATCH_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
|
16 |
+
from dc.conversation import conv_templates, SeparatorStyle
|
17 |
+
from PIL import Image
|
18 |
+
|
19 |
+
|
20 |
+
processor = AutoProcessor.from_pretrained('HuanjinYao/DenseConnector-v1.5-8B')
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained('HuanjinYao/DenseConnector-v1.5-8B', use_fast=False)
|
22 |
+
model = LlavaLlamaForCausalLM.from_pretrained('HuanjinYao/DenseConnector-v1.5-8B', low_cpu_mem_usage=True, **kwargs)
|
23 |
+
image_processor = model.get_vision_tower()
|
24 |
+
if not vision_tower.is_loaded:
|
25 |
+
vision_tower.load_model()
|
26 |
+
vision_tower.to(device=device, dtype=torch.float16)
|
27 |
+
image_processor = vision_tower.image_processor
|
28 |
+
|
29 |
+
model.to('cuda')
|
30 |
+
|
31 |
+
# model.generation_config.eos_token_id = 128009
|
32 |
+
tokenizer.unk_token = "<|reserved_special_token_0|>"
|
33 |
+
tokenizer.pad_token = tokenizer.unk_token
|
34 |
+
terminators = [
|
35 |
+
tokenizer.eos_token_id,
|
36 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
37 |
+
]
|
38 |
+
|
39 |
+
|
40 |
+
@spaces.GPU
|
41 |
+
def bot_streaming(message, history):
|
42 |
+
print(message)
|
43 |
+
if message["files"]:
|
44 |
+
# message["files"][-1] is a Dict or just a string
|
45 |
+
if type(message["files"][-1]) == dict:
|
46 |
+
image = message["files"][-1]["path"]
|
47 |
+
else:
|
48 |
+
image = message["files"][-1]
|
49 |
+
else:
|
50 |
+
# if there's no image uploaded for this turn, look for images in the past turns
|
51 |
+
# kept inside tuples, take the last one
|
52 |
+
for hist in history:
|
53 |
+
if type(hist[0]) == tuple:
|
54 |
+
image = hist[0][0]
|
55 |
+
try:
|
56 |
+
if image is None:
|
57 |
+
# Handle the case where image is None
|
58 |
+
gr.Error("You need to upload an image for LLaVA to work.")
|
59 |
+
except NameError:
|
60 |
+
# Handle the case where 'image' is not defined at all
|
61 |
+
gr.Error("You need to upload an image for LLaVA to work.")
|
62 |
+
|
63 |
+
|
64 |
+
conv = conv_templates['llama_3'].copy()
|
65 |
+
if len(history) == 0:
|
66 |
+
user = DEFAULT_IMAGE_TOKEN + '\n' + message['text']
|
67 |
+
else:
|
68 |
+
for idx, (user, assistant) in enumerate(history):
|
69 |
+
# conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
70 |
+
if idx == 0:
|
71 |
+
user = DEFAULT_IMAGE_TOKEN + '\n' + user
|
72 |
+
conv.append_message(conv.roles[0], user)
|
73 |
+
conv.append_message(conv.roles[1], assistant)
|
74 |
+
conv.append_message(conv.roles[0], user)
|
75 |
+
conv.append_message(conv.roles[1], None)
|
76 |
+
prompt = conv.get_prompt()
|
77 |
+
|
78 |
+
image = Image.open(os.path.join(image, image_file)).convert('RGB')
|
79 |
+
image_tensor = image_processor([image], image_processor, self.model_config)[0]
|
80 |
+
|
81 |
+
inputs = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt')
|
82 |
+
|
83 |
+
|
84 |
+
streamer = TextIteratorStreamer(tokenizer, **{"skip_special_tokens": False, "skip_prompt": True})
|
85 |
+
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024, do_sample=False, eos_token_id = terminators)
|
86 |
+
|
87 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
88 |
+
thread.start()
|
89 |
+
|
90 |
+
|
91 |
+
buffer = ""
|
92 |
+
# time.sleep(0.5)
|
93 |
+
for new_text in streamer:
|
94 |
+
if "<|eot_id|>" in new_text:
|
95 |
+
new_text = new_text.split("<|eot_id|>")[0]
|
96 |
+
buffer += new_text
|
97 |
+
|
98 |
+
generated_text_without_prompt = buffer
|
99 |
+
# time.sleep(0.06)
|
100 |
+
yield generated_text_without_prompt
|
101 |
+
|
102 |
+
|
103 |
+
chatbot=gr.Chatbot(placeholder=PLACEHOLDER,scale=1)
|
104 |
+
chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)
|
105 |
+
with gr.Blocks(fill_height=True, ) as demo:
|
106 |
+
gr.ChatInterface(
|
107 |
+
fn=bot_streaming,
|
108 |
+
title="LLaVA Llama-3-8B",
|
109 |
+
examples=[{"text": "What is on the flower?", "files": ["./bee.jpg"]},
|
110 |
+
{"text": "How to make this pastry?", "files": ["./baklava.png"]}],
|
111 |
+
description="Try [LLaVA Llama-3-8B](https://huggingface.co/xtuner/llava-llama-3-8b-v1_1-transformers). Upload an image and start chatting about it, or simply try one of the examples below. If you don't upload an image, you will receive an error.",
|
112 |
+
stop_btn="Stop Generation",
|
113 |
+
multimodal=True,
|
114 |
+
textbox=chat_input,
|
115 |
+
chatbot=chatbot,
|
116 |
+
)
|
117 |
|
118 |
|
119 |
if __name__ == "__main__":
|