Spaces:
Runtime error
Runtime error
fix: image preprocessing
Browse files
app.py
CHANGED
|
@@ -28,8 +28,7 @@ image_processor = git_processor_base.image_processor
|
|
| 28 |
def preprocess_image(image):
|
| 29 |
"""Преобразуем изображение для модели"""
|
| 30 |
image_rgb = image.convert("RGB")
|
| 31 |
-
|
| 32 |
-
return processed.pixel_values, processed.words, processed.boxes
|
| 33 |
|
| 34 |
|
| 35 |
def translate_ru2en(text):
|
|
@@ -46,13 +45,13 @@ def translate_en2ru(text):
|
|
| 46 |
return translated_text
|
| 47 |
|
| 48 |
|
| 49 |
-
def generate_answer_git(
|
| 50 |
|
| 51 |
qa_pipeline = pipeline(
|
| 52 |
"document-question-answering",
|
| 53 |
model="andgrt/layoutlmv2-base-uncased_finetuned_docvqa",
|
| 54 |
)
|
| 55 |
-
return qa_pipeline(image, question)[0]["answer"]
|
| 56 |
# pixel_values, _, _ = preprocess_image(image)
|
| 57 |
# input_ids = processor(text=question, add_special_tokens=False).input_ids
|
| 58 |
# input_ids = [processor.tokenizer.cls_token_id] + input_ids
|
|
@@ -69,9 +68,7 @@ def generate_answer(image, question):
|
|
| 69 |
question_en = translate_ru2en(question)
|
| 70 |
print(f"Вопрос на английском: {question_en}")
|
| 71 |
|
| 72 |
-
answer_en = generate_answer_git(
|
| 73 |
-
git_processor_base, git_model_base, image, question_en
|
| 74 |
-
)
|
| 75 |
print(f"Ответ на английском: {answer_en}")
|
| 76 |
|
| 77 |
answer_ru = translate_en2ru(answer_en)
|
|
|
|
| 28 |
def preprocess_image(image):
|
| 29 |
"""Преобразуем изображение для модели"""
|
| 30 |
image_rgb = image.convert("RGB")
|
| 31 |
+
return image_processor(image_rgb, return_tensors="pt").pixel_values.to(device)
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
def translate_ru2en(text):
|
|
|
|
| 45 |
return translated_text
|
| 46 |
|
| 47 |
|
| 48 |
+
def generate_answer_git(image, question):
|
| 49 |
|
| 50 |
qa_pipeline = pipeline(
|
| 51 |
"document-question-answering",
|
| 52 |
model="andgrt/layoutlmv2-base-uncased_finetuned_docvqa",
|
| 53 |
)
|
| 54 |
+
return qa_pipeline(preprocess_image(image), question)[0]["answer"]
|
| 55 |
# pixel_values, _, _ = preprocess_image(image)
|
| 56 |
# input_ids = processor(text=question, add_special_tokens=False).input_ids
|
| 57 |
# input_ids = [processor.tokenizer.cls_token_id] + input_ids
|
|
|
|
| 68 |
question_en = translate_ru2en(question)
|
| 69 |
print(f"Вопрос на английском: {question_en}")
|
| 70 |
|
| 71 |
+
answer_en = generate_answer_git(image, question_en)
|
|
|
|
|
|
|
| 72 |
print(f"Ответ на английском: {answer_en}")
|
| 73 |
|
| 74 |
answer_ru = translate_en2ru(answer_en)
|