Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model = AutoModelForQuestionAnswering.from_pretrained("CUTD/qnAr")
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("CUTD/qnAr")
|
| 7 |
+
|
| 8 |
+
def generate_answer(question, context):
|
| 9 |
+
inputs = tokenizer.encode_plus(question, context, add_special_tokens=True, return_tensors="pt")
|
| 10 |
+
input_ids = inputs["input_ids"].tolist()[0]
|
| 11 |
+
|
| 12 |
+
outputs = model(**inputs)
|
| 13 |
+
answer_start_scores = outputs.start_logits
|
| 14 |
+
answer_end_scores = outputs.end_logits
|
| 15 |
+
|
| 16 |
+
answer_start = torch.argmax(answer_start_scores)
|
| 17 |
+
answer_end = torch.argmax(answer_end_scores) + 1
|
| 18 |
+
|
| 19 |
+
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
|
| 20 |
+
return answer
|
| 21 |
+
|
| 22 |
+
iface = gr.Interface(fn=generate_answer,
|
| 23 |
+
inputs=[gr.Textbox(lines=2, placeholder="أدخل السؤال هنا..."),
|
| 24 |
+
gr.Textbox(lines=5, placeholder="أدخل السياق هنا...")],
|
| 25 |
+
outputs=gr.Textbox(),
|
| 26 |
+
title="نظام الإجابة عن الأسئلة",
|
| 27 |
+
description="اكتب سؤالك والسياق، وسيقوم النظام بتزويدك بالإجابة.")
|
| 28 |
+
|
| 29 |
+
iface.launch()
|