File size: 1,108 Bytes
4bc1572
 
 
1838b80
 
4bc1572
 
 
 
 
 
1838b80
fec45e6
4bc1572
fec45e6
4bc1572
 
fec45e6
4bc1572
 
 
 
 
 
 
fec45e6
4bc1572
 
fec45e6
4bc1572
 
 
 
 
 
 
fec45e6
4bc1572
1
2
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
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from PIL import Image
import gradio as gr

model = AutoModelForCausalLM.from_pretrained(
    "MILVLG/imp-v1-3b", 
    torch_dtype=torch.float16, 
    device_map="auto",
    trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("MILVLG/imp-v1-3b", trust_remote_code=True)


def generate_answer(text, image):

    input_ids = tokenizer(text, return_tensors='pt').input_ids
    image_tensor = model.image_preprocess(image)

    output_ids = model.generate(
        input_ids,
        max_new_tokens=100,
        images=image_tensor,
        use_cache=True)[0]
    
    return tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()

text_input = gr.Textbox(lines=5, label="Enter text")
image_input = gr.Image(shape=(224, 224), label="Upload Image")

iface = gr.Interface(
    fn=generate_answer, 
    inputs=[text_input, image_input], 
    outputs="text",
    title="DD360-Bot-Multimodal",
    description="Enter text and upload an image to receive a response from the chatbot."
)

iface.launch()