File size: 921 Bytes
049d3ce
ba5e8fe
 
7f992e4
465d036
ba5e8fe
7f992e4
ba5e8fe
 
 
 
7f992e4
ba5e8fe
 
 
7f992e4
ba5e8fe
 
7f992e4
ba5e8fe
465d036
7f992e4
ba5e8fe
 
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
import gradio as gr
import torch
from transformers import AutoModelForVisualQuestionAnswering

# Загружаем модель
model = AutoModelForVisualQuestionAnswering.from_pretrained("microsoft/visual-turing-nlg-question-answering")

# Создаем интерфейс gradio
def generate_answer(image, prompt=None):
    # Преобразуем изображение в тензор
    image = torch.as_tensor(image)

    # Если указан дополнительный prompt, добавляем его к запросу
    if prompt is not None:
        prompt = f"{prompt} {image.shape[1]} {image.shape[0]}"

    # Получаем ответ от модели
    answer = model(image, prompt=prompt).logits[0].argmax(dim=-1)

    # Возвращаем ответ
    return answer

# Создаем интерфейс
gr.Interface(generate_answer, inputs=[gr.Image(), gr.Text()], outputs=gr.Text())