Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,32 @@
|
|
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 |
-
input_text = "answer: %s context: %s </s>" % (answer, context)
|
9 |
-
features = tokenizer([input_text], return_tensors='pt')
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
css = """
|
20 |
-
.output-markdown{display:none !important}
|
21 |
-
.gr-button-primary {
|
22 |
-
z-index: 14;
|
23 |
-
height: 43px;
|
24 |
-
width: 130px;
|
25 |
-
left: 0px;
|
26 |
-
top: 0px;
|
27 |
-
padding: 0px;
|
28 |
-
cursor: pointer !important;
|
29 |
-
background: none rgb(17, 20, 45) !important;
|
30 |
-
border: none !important;
|
31 |
-
text-align: center !important;
|
32 |
-
font-family: Poppins !important;
|
33 |
-
font-size: 14px !important;
|
34 |
-
font-weight: 500 !important;
|
35 |
-
color: rgb(255, 255, 255) !important;
|
36 |
-
line-height: 1 !important;
|
37 |
-
border-radius: 12px !important;
|
38 |
-
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
39 |
-
box-shadow: none !important;
|
40 |
-
}
|
41 |
-
.gr-button-primary:hover{
|
42 |
-
z-index: 14;
|
43 |
-
height: 43px;
|
44 |
-
width: 130px;
|
45 |
-
left: 0px;
|
46 |
-
top: 0px;
|
47 |
-
padding: 0px;
|
48 |
-
cursor: pointer !important;
|
49 |
-
background: none rgb(37, 56, 133) !important;
|
50 |
-
border: none !important;
|
51 |
-
text-align: center !important;
|
52 |
-
font-family: Poppins !important;
|
53 |
-
font-size: 14px !important;
|
54 |
-
font-weight: 500 !important;
|
55 |
-
color: rgb(255, 255, 255) !important;
|
56 |
-
line-height: 1 !important;
|
57 |
-
border-radius: 12px !important;
|
58 |
-
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
59 |
-
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
|
60 |
-
}
|
61 |
-
.hover\:bg-orange-50:hover {
|
62 |
-
--tw-bg-opacity: 1 !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 | Data Science Dojo", examples=examples, css=css)
|
67 |
demo.launch()
|
|
|
1 |
+
import gradio as gr from transformers import AutoModelWithLMHead, AutoTokenizer
|
|
|
2 |
|
3 |
+
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap") model = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
|
|
|
4 |
|
5 |
+
def get_questions(input_data, max_length=64): context = input_data[0] answers = input_data[1:] generated_questions = []
|
|
|
|
|
6 |
|
7 |
+
for answer in answers:
|
8 |
+
input_text = "answer: %s context: %s </s>" % (answer, context)
|
9 |
+
features = tokenizer([input_text], return_tensors='pt')
|
10 |
|
11 |
+
output = model.generate(
|
12 |
+
input_ids=features['input_ids'],
|
13 |
+
attention_mask=features['attention_mask'],
|
14 |
+
max_length=max_length
|
15 |
+
)
|
16 |
|
17 |
+
question = tokenizer.decode(output[0])[16:-4]
|
18 |
+
generated_questions.append(question)
|
19 |
+
|
20 |
+
return generated_questions
|
21 |
+
CopyCopy
|
22 |
+
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"] ]
|
23 |
+
|
24 |
+
def generate_questions(context, *answers): input_data = [context] + list(answers) questions = get_questions(input_data) return questions
|
25 |
+
|
26 |
+
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") ]
|
27 |
+
|
28 |
+
outputs = gr.Textbox(lines=5, label="Output - Generated Questions")
|
29 |
+
|
30 |
+
css = """ .output-markdown{display:none !important} .gr-button-primary { z-index: 14; height: 43px; width: 130px; left: 0px; top: 0px; padding: 0px; cursor: pointer !important; background: none rgb(17, 20, 45) !important; border: none !important; text-align: center !important; font-family: Poppins !important; font-size: 14px !important; font-weight: 500 !important; color: rgb(255, 255, 255) !important; line-height: 1 !important; border-radius: 12px !important; transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important; box-shadow: none !important; } .gr-button-primary:hover{ z-index: 14; height: 43px; width: 130px; left: 0px; top: 0px; padding: 0px; cursor: pointer !important; background: none rgb(37, 56, 133) !important; border: none !important; text-align: center !important; font-family: Poppins !important; font-size: 14px !important; font-weight: 500 !important; color: rgb(255, 255, 255) !important; line-height: 1 !important; border-radius: 12px !important; transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important; box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important; } .hover:bg-orange-50:hover { --tw-bg-opacity: 1 !important; background-color: rgb(229,225,255) !important; } """ demo = gr.Interface( fn=generate_questions, inputs=inputs, outputs=outputs, title="Question Generator | Data Science Dojo", examples=examples, css=css )
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
demo.launch()
|