Spaces:
Sleeping
Sleeping
Commit
·
a4222c4
1
Parent(s):
1835149
Update app.py
Browse files
app.py
CHANGED
@@ -11,22 +11,25 @@ model = AutoModelForQuestionAnswering.from_pretrained(model_name, revision=revis
|
|
11 |
model.eval()
|
12 |
|
13 |
def qna(image, question):
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
start, end = word_ids[start_scores.argmax(-1).item()], word_ids[end_scores.argmax(-1).item()]
|
28 |
|
29 |
-
|
|
|
30 |
|
31 |
|
32 |
return answer
|
|
|
11 |
model.eval()
|
12 |
|
13 |
def qna(image, question):
|
14 |
+
try:
|
15 |
+
res = image_processor(image, apply_ocr = True)
|
16 |
+
words = res["words"][0]
|
17 |
+
boxes = res["boxes"][0]
|
18 |
|
19 |
+
encoding = tokenizer(question, words, boxes = boxes, return_token_type_ids=True, return_tensors="pt", truncation=True, padding="max_length")
|
20 |
+
|
21 |
+
word_ids = encoding.word_ids(0)
|
22 |
+
outputs = model(**encoding)
|
23 |
+
|
24 |
+
start_scores = outputs.start_logits
|
25 |
+
end_scores = outputs.end_logits
|
26 |
+
|
27 |
+
start, end = word_ids[start_scores.argmax(-1).item()], word_ids[end_scores.argmax(-1).item()]
|
28 |
+
|
29 |
+
answer = " ".join(words[start : end + 1])
|
|
|
|
|
30 |
|
31 |
+
except:
|
32 |
+
answer = "No Answer"
|
33 |
|
34 |
|
35 |
return answer
|