Spaces:
Sleeping
Sleeping
File size: 2,742 Bytes
b802c2a 2f21f91 b802c2a 34d659e 8df48e3 3f1d188 4bc69f5 b802c2a 4bc69f5 507260d 4bc69f5 b802c2a 4bc69f5 b802c2a 4bc69f5 b802c2a |
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 52 53 54 55 56 57 58 59 60 61 62 |
import gradio as gr
import requests
from PIL import Image
from transformers import PaliGemmaForConditionalGeneration, PaliGemmaProcessor
import spaces
import os
from huggingface_hub import login
login(os.getenv('hf_token'))
@spaces.GPU
def infer_ocrvqa(image, question):
model = PaliGemmaForConditionalGeneration.from_pretrained("google/paligemma-3b-ft-ocrvqa-896").to("cuda")
processor = PaliGemmaProcessor.from_pretrained("google/paligemma-3b-ft-ocrvqa-896")
systemprompt = "Ты ассистент по анализу финансовых отчетов. Ниже приведены вопросы по данным на изображении. Необходимо отвечать на вопросы по суммам в таблицах максимально точно и обращать внимание на названия колонок таблиц. Вопросы: "
inputs = processor(images=image,text=systemprompt+question, return_tensors="pt").to("cuda")
predictions = model.generate(**inputs, max_new_tokens=100)
return processor.decode(predictions[0], skip_special_tokens=True)[len(question):].lstrip("\n")
@spaces.GPU
def infer_doc(image, question):
model = PaliGemmaForConditionalGeneration.from_pretrained("google/paligemma-3b-ft-docvqa-896").to("cuda")
processor = PaliGemmaProcessor.from_pretrained("google/paligemma-3b-ft-docvqa-896")
inputs = processor(images=image, text=question, return_tensors="pt").to("cuda")
predictions = model.generate(**inputs, max_new_tokens=100)
return processor.decode(predictions[0], skip_special_tokens=True)[len(question):].lstrip("\n")
css = """
#mkd {
height: 500px;
overflow: auto;
border: 1px solid #ccc;
}
"""
with gr.Blocks(css=css) as demo:
gr.HTML("<h1><center>PaliGemma для VQA/OCR 📄<center><h1>")
gr.HTML("<h3><center>Использование модели as is без файнтюнинга на документах. ⚡</h3>")
with gr.Tab(label="Ответы на вопросы по документам"):
with gr.Row():
with gr.Column():
input_img = gr.Image(label="Input Document")
question = gr.Text(label="Question")
submit_btn = gr.Button(value="Submit")
output = gr.Text(label="Answer")
submit_btn.click(infer_doc, [input_img, question], [output])
with gr.Tab(label="Чтение текста со сканов"):
with gr.Row():
with gr.Column():
input_img = gr.Image(label="Input Document")
question = gr.Text(label="Question")
submit_btn = gr.Button(value="Submit")
output = gr.Text(label="Infer")
submit_btn.click(infer_ocrvqa, [input_img, question], [output])
demo.launch(debug=True) |