Spaces:
Sleeping
Sleeping
File size: 2,187 Bytes
7eaf983 9b4fe07 5afbda2 9b4fe07 7eaf983 9b4fe07 7eaf983 9b4fe07 7eaf983 9b4fe07 7eaf983 9b4fe07 5afbda2 9b4fe07 5afbda2 9b4fe07 5afbda2 9b4fe07 7eaf983 5afbda2 |
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 40 41 42 43 44 45 46 47 48 49 50 51 |
import gradio as gr
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
import requests
from PIL import Image
import torch
import spaces
# Baixar exemplos de gráficos
torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/test/png/74801584018932.png', 'chart_example_1.png')
torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/val/png/multi_col_1229.png', 'chart_example_2.png')
# Carregar modelo e processador
model = PaliGemmaForConditionalGeneration.from_pretrained("ahmed-masry/chartgemma")
processor = AutoProcessor.from_pretrained("ahmed-masry/chartgemma")
@spaces.GPU
def predict(image, input_text):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
image = image.convert("RGB")
inputs = processor(text=input_text, images=image, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}
prompt_length = inputs['input_ids'].shape[1]
# Gerar resposta
generate_ids = model.generate(**inputs, max_new_tokens=512)
output_text = processor.batch_decode(generate_ids[:, prompt_length:], skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
return output_text
# Definir componentes da interface
image = gr.components.Image(type="pil", label="Imagem do Gráfico")
input_prompt = gr.components.Textbox(label="Prompt de Entrada")
model_output = gr.components.Textbox(label="Saída do Modelo")
examples = [["chart_example_1.png", "Descreva a tendência das taxas de mortalidade para crianças com menos de 5 anos"],
["chart_example_2.png", "Qual é a porcentagem de respondentes que preferem o Facebook Messenger no grupo etário de 30-59 anos?"]]
# Configurar e lançar a interface
title = "Demonstração Interativa do Modelo ChartGemma"
interface = gr.Interface(fn=predict,
inputs=[image, input_prompt],
outputs=model_output,
examples=examples,
title=title,
theme='gradio/soft')
interface.launch()
|