Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,20 @@
|
|
1 |
-
import gradio as gr
|
2 |
from transformers import AutoModelWithLMHead, AutoTokenizer
|
3 |
|
4 |
-
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
|
5 |
model = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
generated_questions = []
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
input_ids=features['input_ids'],
|
18 |
-
attention_mask=features['attention_mask'],
|
19 |
-
max_length=max_length
|
20 |
-
)
|
21 |
|
22 |
-
|
23 |
-
generated_questions.append(question)
|
24 |
-
return generated_questions
|
25 |
-
|
26 |
-
examples = [ ["Uzbekistan is a Central Asian nation and former Soviet republic. It's known for its mosques, mausoleums and other sites linked to the Silk Road, the ancient trade route between China and the Mediterranean. Samarkand, a major city on the route, contains a landmark of Islamic architecture.", "Silk Road"], ["The Great Barrier Reef is the world's largest coral reef system composed of over 2,900 individual reefs and 900 islands stretching for over 2,300 kilometers. The reef is located in the Coral Sea, off the coast of Australia's state of Queensland.", "Great Barrier Reef"] ]
|
27 |
-
|
28 |
-
def generate_questions(context, *answers):
|
29 |
-
input_data = [context] + list(answers)
|
30 |
-
questions = get_questions(input_data)
|
31 |
-
return questions
|
32 |
-
|
33 |
-
inputs = [ gr.Textbox(lines=3, placeholder="Enter context here", label="Input - Context"), gr.Textbox(lines=1, label="Input - Answer 1"), gr.Textbox(lines=1, label="Input - Answer 2"), gr.Textbox(lines=1, label="Input - Answer 3") ]
|
34 |
-
|
35 |
-
outputs = gr.Textbox(lines=5, label="Output - Generated Questions")
|
36 |
|
37 |
css = """
|
38 |
.output-markdown{display:none !important}
|
@@ -81,7 +63,5 @@ css = """
|
|
81 |
background-color: rgb(229,225,255) !important;
|
82 |
}
|
83 |
"""
|
84 |
-
|
85 |
-
demo = gr.Interface( fn=generate_questions, inputs=inputs, outputs=outputs, title="Question Generator | Data Science Dojo", examples=examples, css=css )
|
86 |
-
|
87 |
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import AutoModelWithLMHead, AutoTokenizer
|
3 |
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
|
5 |
model = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
|
6 |
|
7 |
+
def get_question(context, answer, max_length=64):
|
8 |
+
input_text = "answer: %s context: %s </s>" % (answer, context)
|
9 |
+
features = tokenizer([input_text], return_tensors='pt')
|
|
|
10 |
|
11 |
+
output = model.generate(input_ids=features['input_ids'],
|
12 |
+
attention_mask=features['attention_mask'],
|
13 |
+
max_length=max_length)
|
14 |
|
15 |
+
return tokenizer.decode(output[0])[16:-4]
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
examples = [["Uzbekistan is a Central Asian nation and former Soviet republic. It's known for its mosques, mausoleums and other sites linked to the Silk Road, the ancient trade route between China and the Mediterranean. Samarkand, a major city on the route, contains a landmark of Islamic architecture.", "Silk Road"]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
css = """
|
20 |
.output-markdown{display:none !important}
|
|
|
63 |
background-color: rgb(229,225,255) !important;
|
64 |
}
|
65 |
"""
|
66 |
+
demo = gr.Interface(fn=get_question, inputs=[gr.Textbox(lines=3, placeholder="Enter text here", label="Input # 1 - Context"), gr.Textbox(lines=1, label="Input # 2 - Answer")], outputs=gr.Textbox(label="Output - Generated Question"), title="Question Generator", examples=examples, css=css)
|
|
|
|
|
67 |
demo.launch()
|