Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import (
|
3 |
+
AutoTokenizer,
|
4 |
+
AutoModelForSeq2SeqLM,
|
5 |
+
AdamW,
|
6 |
+
get_linear_schedule_with_warmup
|
7 |
+
)
|
8 |
+
|
9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("ADEL/ANQG")
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("ADEL/ANQG")
|
11 |
+
|
12 |
+
|
13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
+
print("device",device)
|
15 |
+
model=model.to(device)
|
16 |
+
|
17 |
+
import gradio as gr
|
18 |
+
def generate__questions(cnt,ans):
|
19 |
+
text="context: " +cnt + " " + "answer: " + ans + " </s>"
|
20 |
+
text_encoding = tokenizer.encode_plus(
|
21 |
+
text,max_length=512,padding=True,return_tensors="pt"
|
22 |
+
)
|
23 |
+
model.eval()
|
24 |
+
generated_ids = model.generate(
|
25 |
+
input_ids=text_encoding['input_ids'].to(device),
|
26 |
+
attention_mask=text_encoding['attention_mask'].to(device),
|
27 |
+
max_length=72,
|
28 |
+
early_stopping=True,
|
29 |
+
num_beams=5,
|
30 |
+
num_return_sequences=1
|
31 |
+
)
|
32 |
+
|
33 |
+
preds = [
|
34 |
+
tokenizer.decode(gen_id,skip_special_tokens=True,clean_up_tokenization_spaces=True)
|
35 |
+
for gen_id in generated_ids
|
36 |
+
]
|
37 |
+
|
38 |
+
t=' '.join(preds)
|
39 |
+
st=t.replace('question: ',' ')
|
40 |
+
return(st)
|
41 |
+
|
42 |
+
demo = gr.Interface(fn=generate__questions, inputs=[gr.Textbox(label='Context'),
|
43 |
+
gr.Textbox(label='Answer')] ,
|
44 |
+
outputs=gr.Textbox(label='Question'),
|
45 |
+
title="Arabic Question Generation",
|
46 |
+
description="Get the Question from given Context and a Answer")
|
47 |
+
demo.launch()
|