Spaces:
Sleeping
Sleeping
output in blocks
Browse files- .gitignore +1 -0
- app.py +81 -9
- prompts.py +3 -3
.gitignore
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
.DS_Store
|
|
|
2 |
|
3 |
# Byte-compiled / optimized / DLL files
|
4 |
__pycache__/
|
|
|
1 |
.DS_Store
|
2 |
+
questions.json
|
3 |
|
4 |
# Byte-compiled / optimized / DLL files
|
5 |
__pycache__/
|
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import base64
|
4 |
import prompts
|
|
|
5 |
from openai import OpenAI
|
6 |
from dotenv import load_dotenv
|
7 |
|
@@ -16,6 +17,25 @@ def encode_image(image_path):
|
|
16 |
return base64.b64encode(image_file.read()).decode("utf-8")
|
17 |
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def process(image_path):
|
20 |
try:
|
21 |
response = client.chat.completions.create(
|
@@ -43,17 +63,69 @@ def process(image_path):
|
|
43 |
# presence_penalty=0
|
44 |
)
|
45 |
# print(response["usage"]["total_tokens"])
|
46 |
-
|
|
|
47 |
|
48 |
except Exception as e:
|
49 |
print(f"an error occurred : {e}")
|
50 |
-
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
outputs=gr.JSON(),
|
57 |
-
)
|
58 |
-
authorized_users = [("test", os.environ["TEST_PASSWORD"])]
|
59 |
-
iface.launch(auth=authorized_users)
|
|
|
2 |
import gradio as gr
|
3 |
import base64
|
4 |
import prompts
|
5 |
+
import json
|
6 |
from openai import OpenAI
|
7 |
from dotenv import load_dotenv
|
8 |
|
|
|
17 |
return base64.b64encode(image_file.read()).decode("utf-8")
|
18 |
|
19 |
|
20 |
+
def load_qcm(file_path):
|
21 |
+
try:
|
22 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
23 |
+
return json.load(file)
|
24 |
+
except json.JSONDecodeError as e:
|
25 |
+
print(f"Error decoding JSON: {e}")
|
26 |
+
return {}
|
27 |
+
|
28 |
+
|
29 |
+
def get_answers(qcm): # qcm is in json format
|
30 |
+
answers = [answer["value"] for answer in qcm["Answers"]]
|
31 |
+
correct_answers = [
|
32 |
+
answer["value"] for answer in qcm["Answers"] if answer["correct"]
|
33 |
+
]
|
34 |
+
md_answers = "\n".join([f"* {answer}" for answer in answers])
|
35 |
+
md_correct_answers = "\n".join([f"* {answer}" for answer in correct_answers])
|
36 |
+
return {"md_answers": md_answers, "md_correct_answers": md_correct_answers}
|
37 |
+
|
38 |
+
|
39 |
def process(image_path):
|
40 |
try:
|
41 |
response = client.chat.completions.create(
|
|
|
63 |
# presence_penalty=0
|
64 |
)
|
65 |
# print(response["usage"]["total_tokens"])
|
66 |
+
json_output = response.choices[0].message.content
|
67 |
+
return json.loads(json_output)
|
68 |
|
69 |
except Exception as e:
|
70 |
print(f"an error occurred : {e}")
|
71 |
+
return {"error": str(e)}, str(e)
|
72 |
+
|
73 |
+
|
74 |
+
with gr.Blocks() as demo:
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
image = gr.Image(label="Image", type="filepath")
|
78 |
+
with gr.Column():
|
79 |
+
submit_btn = gr.Button("Soumettre")
|
80 |
+
progress = gr.Textbox(label="Traitement")
|
81 |
+
with gr.Accordion(
|
82 |
+
open=False,
|
83 |
+
):
|
84 |
+
gr_json_output = gr.JSON(label="json output")
|
85 |
+
|
86 |
+
with gr.Tab(label="QCM", visible=False) as gr_qcm_column:
|
87 |
+
|
88 |
+
gr_question = gr.Textbox(label="Question")
|
89 |
+
with gr.Accordion(label="Réponses possibles"):
|
90 |
+
gr_answers = gr.Markdown()
|
91 |
+
gr_hint = gr.Textbox(label="Aide à la réponse")
|
92 |
+
with gr.Accordion(label="Bonnes réponses"):
|
93 |
+
gr_correct_answers = gr.Markdown()
|
94 |
+
gr_explanation = gr.Textbox(label="Explication")
|
95 |
+
|
96 |
+
def submit(image_path):
|
97 |
+
|
98 |
+
qcm = process(image_path)
|
99 |
+
# qcm = load_qcm("questions.json")
|
100 |
+
ga = get_answers(qcm)
|
101 |
+
|
102 |
+
return {
|
103 |
+
progress: "Terminé !",
|
104 |
+
gr_qcm_column: gr.Tab(visible=True),
|
105 |
+
gr_json_output: qcm,
|
106 |
+
gr_question: qcm["Question"],
|
107 |
+
gr_answers: ga["md_answers"],
|
108 |
+
gr_hint: qcm["hint"],
|
109 |
+
gr_correct_answers: ga["md_correct_answers"],
|
110 |
+
gr_explanation: qcm["explanation"],
|
111 |
+
}
|
112 |
|
113 |
+
submit_btn.click(
|
114 |
+
fn=submit,
|
115 |
+
inputs=image,
|
116 |
+
outputs=[
|
117 |
+
progress,
|
118 |
+
gr_qcm_column,
|
119 |
+
gr_json_output,
|
120 |
+
gr_question,
|
121 |
+
gr_hint,
|
122 |
+
gr_answers,
|
123 |
+
gr_correct_answers,
|
124 |
+
gr_explanation,
|
125 |
+
],
|
126 |
+
api_name="submit",
|
127 |
+
)
|
128 |
|
129 |
+
if __name__ == "__main__":
|
130 |
+
authorized_users = [("test", os.environ["TEST_PASSWORD"])]
|
131 |
+
demo.launch(auth=authorized_users)
|
|
|
|
|
|
|
|
prompts.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
SINGLE_QCM_PROMPT = """
|
2 |
-
You are an assistant for a primary or secondary school teacher. The attached image is a photo or screenshot of a multiple choice question found by the teacher in an exercise book. Your job is to convert this image into a JSON file containing the question, proposed answers,
|
3 |
|
4 |
Let's proceed step by step:
|
5 |
|
6 |
1. Start by identifying the question. Note that the question may be numbered with a digit (such as “1.”) or a letter (such as “a.”). This numbering is not part of the question. Keep only the question, without changing, deleting or adding any word
|
7 |
2. Identify all the proposed answers
|
8 |
-
3. For each proposed answer, check if it is a correct answer. You will have
|
9 |
4. Evaluate the age of the student to which this exercise is targeted.
|
10 |
-
4. Think about a short hint sentence for this MCQ. It should be adapted to the students age and be very short. It should
|
11 |
5. Think about a short explanation that will help the students who didn’t fin the correct answer. This explanation must be adapted to their age and be very short.
|
12 |
6. Write the question, answers, and in a JSON file following the given example format.
|
13 |
|
|
|
1 |
SINGLE_QCM_PROMPT = """
|
2 |
+
You are an assistant for a primary or secondary school teacher. The attached image is a photo or screenshot of a multiple choice question found by the teacher in an exercise book. Your job is to convert this image into a JSON file containing the question, proposed answers, a boolean indicator telling which answer is correct, and an explanation.
|
3 |
|
4 |
Let's proceed step by step:
|
5 |
|
6 |
1. Start by identifying the question. Note that the question may be numbered with a digit (such as “1.”) or a letter (such as “a.”). This numbering is not part of the question. Keep only the question, without changing, deleting or adding any word
|
7 |
2. Identify all the proposed answers
|
8 |
+
3. For each proposed answer, check if it is a correct answer. You will have to set a boolean field indicator which tells if the answer is correct or not.
|
9 |
4. Evaluate the age of the student to which this exercise is targeted.
|
10 |
+
4. Think about a short hint sentence for this MCQ. It should be adapted to the students age and be very short. It should target the terms in the exercise that might pose a problem for your students. Remember to formulate clues, not answers.
|
11 |
5. Think about a short explanation that will help the students who didn’t fin the correct answer. This explanation must be adapted to their age and be very short.
|
12 |
6. Write the question, answers, and in a JSON file following the given example format.
|
13 |
|