Spaces:
Runtime error
Runtime error
init
Browse files- app.py +97 -0
- doc.png +0 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import (
|
| 3 |
+
AutoTokenizer,
|
| 4 |
+
AutoModelForSeq2SeqLM,
|
| 5 |
+
AutoProcessor,
|
| 6 |
+
AutoModelForCausalLM,
|
| 7 |
+
)
|
| 8 |
+
import torch
|
| 9 |
+
import pyttsx3
|
| 10 |
+
|
| 11 |
+
tokenizer_ru2en = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ru-en")
|
| 12 |
+
model_ru2en = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-ru-en")
|
| 13 |
+
|
| 14 |
+
tokenizer_en2ru = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
| 15 |
+
model_en2ru = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
| 16 |
+
|
| 17 |
+
git_processor_base = AutoProcessor.from_pretrained(
|
| 18 |
+
"andgrt/layoutlmv2-base-uncased_finetuned_docvqa"
|
| 19 |
+
)
|
| 20 |
+
git_model_base = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
"andgrt/layoutlmv2-base-uncased_finetuned_docvqa"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
git_model_base.to(device)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
engine = pyttsx3.init()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def translate_ru2en(text):
|
| 32 |
+
inputs = tokenizer_ru2en(text, return_tensors="pt")
|
| 33 |
+
outputs = model_ru2en.generate(**inputs)
|
| 34 |
+
translated_text = tokenizer_ru2en.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
+
return translated_text
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def translate_en2ru(text):
|
| 39 |
+
inputs = tokenizer_en2ru(text, return_tensors="pt")
|
| 40 |
+
outputs = model_en2ru.generate(**inputs)
|
| 41 |
+
translated_text = tokenizer_en2ru.decode(outputs[0], skip_special_tokens=True)
|
| 42 |
+
return translated_text
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def generate_answer_git(processor, model, image, question):
|
| 46 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)
|
| 47 |
+
input_ids = processor(text=question, add_special_tokens=False).input_ids
|
| 48 |
+
input_ids = [processor.tokenizer.cls_token_id] + input_ids
|
| 49 |
+
input_ids = torch.tensor(input_ids).unsqueeze(0).to(device)
|
| 50 |
+
|
| 51 |
+
generated_ids = model.generate(
|
| 52 |
+
pixel_values=pixel_values, input_ids=input_ids, max_length=50
|
| 53 |
+
)
|
| 54 |
+
generated_answer = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 55 |
+
return generated_answer[0]
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def generate_answer(image, question):
|
| 59 |
+
question_en = translate_ru2en(question)
|
| 60 |
+
print(f"Вопрос на английском: {question_en}")
|
| 61 |
+
|
| 62 |
+
answer_en = generate_answer_git(
|
| 63 |
+
git_processor_base, git_model_base, image, question_en
|
| 64 |
+
)
|
| 65 |
+
print(f"Ответ на английском: {answer_en}")
|
| 66 |
+
|
| 67 |
+
answer_ru = translate_en2ru(answer_en)
|
| 68 |
+
|
| 69 |
+
engine.say(answer_ru)
|
| 70 |
+
engine.runAndWait()
|
| 71 |
+
|
| 72 |
+
return answer_ru
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
examples = [
|
| 76 |
+
["doc.png", "О чем данный документ?"],
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
interface = gr.Interface(
|
| 80 |
+
fn=generate_answer,
|
| 81 |
+
inputs=[
|
| 82 |
+
gr.inputs.Image(type="pil"),
|
| 83 |
+
gr.inputs.Textbox(label="Вопрос (на русском)", placeholder="Ваш вопрос"),
|
| 84 |
+
],
|
| 85 |
+
outputs=gr.outputs.Textbox(label="Ответ (на русском)"),
|
| 86 |
+
examples=examples,
|
| 87 |
+
title="Демо визуального ответчика на вопросы (на русском)",
|
| 88 |
+
description=(
|
| 89 |
+
"Gradio демо для модели doc-qa с переводом вопросов и ответов"
|
| 90 |
+
"на русский язык. Загрузите изображение и задайте вопрос, чтобы"
|
| 91 |
+
"получить ответ. Вы также можете использовать голосовой ввод!"
|
| 92 |
+
),
|
| 93 |
+
allow_flagging="never",
|
| 94 |
+
enable_queue=True,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
interface.launch(debug=True, share=True)
|
doc.png
ADDED
|
requirements.txt
ADDED
|
File without changes
|