Update app.py
Browse filesimport gradio as gr
import torch
from transformers import AutoProcessor, AutoModelForCausalLM
from PIL import Image
import os
model_id = "wisdomik/Quilt-Llava-v1.5-7b"
device = "cuda" if torch.cuda.is_available() else "cpu"
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16,
load_in_4bit=True,
device_map="auto"
)
def predict(image, message, history):
if image is not None:
prompt = f"USER: <image>\n{message} ASSISTANT:"
else:
prompt = f"USER: {message} ASSISTANT:"
inputs = processor(prompt, images=image, return_tensors="pt").to(device)
with torch.no_grad():
output = model.generate(**inputs, max_new_tokens=512, temperature=0.7)
response = processor.decode(output[0], skip_special_tokens=True)
response = response.split("ASSISTANT:")[-1].strip()
history.append((message, response))
return history, ""
with gr.Blocks(title="Quilt-LLaVA Patología") as demo:
gr.ChatInterface(predict, multimodal=True)
if __name__ == "__main__":
demo.launch()
|
@@ -1,32 +0,0 @@
|
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|