Upload app.py.txt
Browse files- app.py.txt +32 -0
app.py.txt
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
from llava.model.builder import load_pretrained_model
|
| 4 |
+
from llava.mm_utils import tokenizer_image_token, get_model_name_from_path
|
| 5 |
+
from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN
|
| 6 |
+
from llava.conversation import conv_templates
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
model_path = "wisdomik/Quilt-Llava-v1.5-7b"
|
| 11 |
+
tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, model_base=None, model_name=get_model_name_from_path(model_path))
|
| 12 |
+
|
| 13 |
+
def predict(image, prompt, history):
|
| 14 |
+
if image is not None:
|
| 15 |
+
image_token = DEFAULT_IMAGE_TOKEN
|
| 16 |
+
prompt = image_token + '\n' + prompt
|
| 17 |
+
else:
|
| 18 |
+
prompt = prompt
|
| 19 |
+
inp = f"{prompt}\nAssistant:"
|
| 20 |
+
conv = conv_templates["llava_v1"].copy()
|
| 21 |
+
conv.append_message(conv.roles[0], inp)
|
| 22 |
+
conv.append_message(conv.roles[1], None)
|
| 23 |
+
prompt = conv.get_prompt()
|
| 24 |
+
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
|
| 25 |
+
with torch.inference_mode():
|
| 26 |
+
output_ids = model.generate(input_ids, max_new_tokens=512, do_sample=False)
|
| 27 |
+
response = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True).strip()
|
| 28 |
+
history.append((prompt, response))
|
| 29 |
+
return history, ""
|
| 30 |
+
|
| 31 |
+
iface = gr.ChatInterface(predict, multimodal=True)
|
| 32 |
+
iface.launch()
|