Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
|
|
|
|
|
|
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/test/png/74801584018932.png', 'chart_example_1.png')
|
| 7 |
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')
|
| 8 |
|
| 9 |
-
#
|
| 10 |
model = PaliGemmaForConditionalGeneration.from_pretrained("ahmed-masry/chartgemma")
|
| 11 |
processor = AutoProcessor.from_pretrained("ahmed-masry/chartgemma")
|
| 12 |
|
|
@@ -22,25 +25,21 @@ def predict(image, input_text):
|
|
| 22 |
|
| 23 |
prompt_length = inputs['input_ids'].shape[1]
|
| 24 |
|
| 25 |
-
#
|
| 26 |
generate_ids = model.generate(**inputs, max_new_tokens=512)
|
| 27 |
output_text = processor.batch_decode(generate_ids[:, prompt_length:], skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 28 |
|
| 29 |
return output_text
|
| 30 |
|
| 31 |
-
#
|
| 32 |
image = gr.components.Image(type="pil", label="Imagem do Gráfico")
|
| 33 |
-
input_prompt = gr.components.Textbox(label="
|
| 34 |
model_output = gr.components.Textbox(label="Saída do Modelo")
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
["chart_example_2.png", "Qual é a proporção de respondentes que preferem o Facebook Messenger no grupo etário de 30 a 59 anos?"]]
|
| 39 |
-
|
| 40 |
-
# Título da interface
|
| 41 |
-
title = "Demo Interativa do Modelo ChartGemma"
|
| 42 |
-
|
| 43 |
-
# Criando e lançando a interface
|
| 44 |
interface = gr.Interface(fn=predict,
|
| 45 |
inputs=[image, input_prompt],
|
| 46 |
outputs=model_output,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image
|
| 5 |
import torch
|
| 6 |
+
import spaces
|
| 7 |
|
| 8 |
+
# Baixar exemplos de gráficos
|
| 9 |
torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/test/png/74801584018932.png', 'chart_example_1.png')
|
| 10 |
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')
|
| 11 |
|
| 12 |
+
# Carregar modelo e processador
|
| 13 |
model = PaliGemmaForConditionalGeneration.from_pretrained("ahmed-masry/chartgemma")
|
| 14 |
processor = AutoProcessor.from_pretrained("ahmed-masry/chartgemma")
|
| 15 |
|
|
|
|
| 25 |
|
| 26 |
prompt_length = inputs['input_ids'].shape[1]
|
| 27 |
|
| 28 |
+
# Gerar resposta
|
| 29 |
generate_ids = model.generate(**inputs, max_new_tokens=512)
|
| 30 |
output_text = processor.batch_decode(generate_ids[:, prompt_length:], skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 31 |
|
| 32 |
return output_text
|
| 33 |
|
| 34 |
+
# Definir componentes da interface
|
| 35 |
image = gr.components.Image(type="pil", label="Imagem do Gráfico")
|
| 36 |
+
input_prompt = gr.components.Textbox(label="Prompt de Entrada")
|
| 37 |
model_output = gr.components.Textbox(label="Saída do Modelo")
|
| 38 |
+
examples = [["chart_example_1.png", "Descreva a tendência das taxas de mortalidade para crianças com menos de 5 anos"],
|
| 39 |
+
["chart_example_2.png", "Qual é a porcentagem de respondentes que preferem o Facebook Messenger no grupo etário de 30-59 anos?"]]
|
| 40 |
|
| 41 |
+
# Configurar e lançar a interface
|
| 42 |
+
title = "Demonstração Interativa do Modelo ChartGemma"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
interface = gr.Interface(fn=predict,
|
| 44 |
inputs=[image, input_prompt],
|
| 45 |
outputs=model_output,
|