Spaces:
Runtime error
Runtime error
Commit
·
573538a
1
Parent(s):
2cd8202
Update app.py
Browse files
app.py
CHANGED
@@ -7,59 +7,106 @@ import sys
|
|
7 |
import openai
|
8 |
from openai import Completion as complete
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
openai.api_key = os.environ["openai_api_key"]
|
13 |
access_code = os.environ["access_code"]
|
14 |
|
15 |
-
def
|
16 |
-
|
17 |
-
|
18 |
-
"""
|
19 |
-
prompt = context + mcq_prompt
|
20 |
-
try:
|
21 |
-
if credentials != access_code:
|
22 |
-
return "mauvais code d'accès"
|
23 |
-
else:
|
24 |
-
try:
|
25 |
-
completion = complete.create(model="text-davinci-003", prompt=prompt, max_tokens=1875)
|
26 |
-
return str(completion.choices[0].text)
|
27 |
-
except Exception as e:
|
28 |
-
# return str(e)
|
29 |
-
# return python version
|
30 |
-
return str(sys.version)
|
31 |
-
except:
|
32 |
-
return str(sys.version)
|
33 |
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
with gr.Row():
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
|
49 |
-
quiz_button = gr.Button("Générer dix questions.")
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
slider = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Nombre de questions.")
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
#todo: test generating with just one phrase for one MCQ with smaller models
|
59 |
|
60 |
-
quiz_button.click(fn=generate_questions,
|
61 |
-
|
62 |
-
|
|
|
|
|
63 |
|
64 |
if __name__ == "__main__":
|
65 |
-
demo.launch()
|
|
|
7 |
import openai
|
8 |
from openai import Completion as complete
|
9 |
|
10 |
+
qa_gen_prompt = str("""You are a highly intelligent & complex question-answer generative model.
|
11 |
+
You take a passage as an input and generate 5 high-quality and diverse multiple-choice questions
|
12 |
+
with four choices each from the given passage by imitating the way a human asks questions and give answers.
|
13 |
+
Your output format is only
|
14 |
+
[{\"Q\": Question, \"A1\": Answer, \"A2\": Answer,\"A3\": Answer, \"A4\": Answer}, ...]\") }} form, no other form. \n The passage: """)
|
15 |
+
end_prompt = "the QA pairs:"
|
16 |
|
17 |
openai.api_key = os.environ["openai_api_key"]
|
18 |
access_code = os.environ["access_code"]
|
19 |
|
20 |
+
def convert_questions_to_html(qapairs):
|
21 |
+
qapairs_template = []
|
22 |
+
for qapair in qapairs:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
result = "<problem>\n<choiceresponse> \n<label>" + qapair['Q'] + "</label>\n<checkboxgroup>"
|
25 |
|
26 |
+
for key, value in qapair.items():
|
27 |
+
if key != "Q":
|
28 |
+
result += "\n <choice correct=\"false\">" + value + "</choice>"
|
29 |
+
result+="\n"
|
30 |
+
result += " </checkboxgroup>\n</choiceresponse>\n</problem>"
|
31 |
+
qapairs_template.append(result)
|
32 |
+
return "".join([qa + "\n\n" for qa in qapairs_template])
|
33 |
|
|
|
34 |
|
35 |
+
def convert_questions_to_template(qapairs):
|
36 |
+
qapairs_template = []
|
37 |
+
for qapair in qapairs:
|
38 |
|
39 |
+
result = ">>" + qapair['Q'] + " <<\n\n"
|
40 |
+
choices = [qapair['A1'], qapair['A2'], qapair['A3'], qapair['A4']]
|
41 |
+
for i, choice in enumerate(choices):
|
42 |
+
result += "[ ] " + choice + "\n"
|
43 |
+
|
44 |
+
qapairs_template.append(result)
|
45 |
|
46 |
+
return "".join([qa + "\n\n" for qa in qapairs_template])
|
47 |
|
|
|
48 |
|
49 |
+
def list_of_dicts_to_str(qapairs, indent=0):
|
50 |
+
result = ''
|
51 |
+
for d in qapairs:
|
52 |
+
for key, value in d.items():
|
53 |
+
if key.startswith("A"):
|
54 |
+
result += ' ' * (indent + 4) + str(key) + ': ' + str(value) + '\n'
|
55 |
+
else:
|
56 |
+
result += ' ' * indent + str(key) + ': ' + str(value) + '\n'
|
57 |
+
return result
|
58 |
|
|
|
59 |
|
60 |
+
def generate_questions(context, credentials):
|
61 |
+
"""
|
62 |
+
Generate questions by calling davinci-003.
|
63 |
+
"""
|
64 |
+
if credentials != access_code:
|
65 |
+
return "Code d'accès incorrect. Veuillez réessayer.", "Code d'accès incorrect. Veuillez réessayer.", "Code d'accès incorrect. Veuillez réessayer."
|
66 |
|
67 |
+
prompt = qa_gen_prompt + context + end_prompt
|
68 |
|
69 |
+
try:
|
70 |
+
completion = complete.create(model="text-davinci-003", prompt=prompt, max_tokens=1875)
|
71 |
+
q_dict = eval(completion.choices[0].text)
|
72 |
+
return list_of_dicts_to_str(q_dict), convert_questions_to_html(q_dict), convert_questions_to_template(q_dict)
|
73 |
+
except Exception as e:
|
74 |
+
# return str(e)
|
75 |
+
# return python version
|
76 |
+
return str(sys.version)
|
77 |
+
|
78 |
+
|
79 |
+
with gr.Blocks() as demo:
|
80 |
+
|
81 |
+
# with gr.Row():
|
82 |
+
|
83 |
+
# with gr.Column():
|
84 |
+
|
85 |
+
|
86 |
+
with gr.Row():
|
87 |
+
|
88 |
+
with gr.Column():
|
89 |
+
gr.Markdown("## Générateur de questions à choix multiples")
|
90 |
+
gr.Markdown("Pour créer des questions à choix multiples, insérez un texte dans la boîte à gauche, puis cliquez sur 'Générer des questions.' Attention! N'oubliez pas le code d'accès.")
|
91 |
+
credentials = gr.Textbox(placeholder="Code d'accès", lines=1)
|
92 |
+
context = gr.Textbox(placeholder="insérez le texte ici.", lines=8)
|
93 |
+
|
94 |
+
quiz_button = gr.Button("Générer cinq questions.", variant="primary")
|
95 |
+
|
96 |
+
with gr.Tab("brut"):
|
97 |
+
qa_pairs = gr.Textbox(placeholder="Les questions apparaîtront ici.", lines=19)
|
98 |
+
with gr.Tab("format html"):
|
99 |
+
qa_pairs_html = gr.Textbox(placeholder="Les questions apparaîtront ici.", lines=19)
|
100 |
+
with gr.Tab("format edulib"):
|
101 |
+
qa_pairs_edulib = gr.Textbox(placeholder="Les questions apparaîtront ici.", lines=19)
|
102 |
+
|
103 |
#todo: test generating with just one phrase for one MCQ with smaller models
|
104 |
|
105 |
+
quiz_button.click(fn= generate_questions,
|
106 |
+
inputs=[context, credentials],
|
107 |
+
outputs=[qa_pairs, qa_pairs_html, qa_pairs_edulib]
|
108 |
+
)
|
109 |
+
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
+
demo.launch()
|