Spaces:
Sleeping
Sleeping
single QCM prompt
Browse files
app.py
CHANGED
@@ -1,9 +1,73 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
6 |
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import base64
|
3 |
|
4 |
|
5 |
+
# Function to encode the image
|
6 |
+
def encode_image(image_path):
|
7 |
+
with open(image_path, "rb") as image_file:
|
8 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
9 |
|
10 |
|
11 |
+
from openai import OpenAI
|
12 |
+
|
13 |
+
client = OpenAI()
|
14 |
+
|
15 |
+
SINGLE_QCM_PROMPT = """
|
16 |
+
You are an assistant for a primary or secondary school teacher.
|
17 |
+
The attached image is a photo or screenshot of a multiple choice question found by the teacher in an exercise book.
|
18 |
+
Your job is to convert this image into a JSON file containing the question and proposed answers.
|
19 |
+
|
20 |
+
Let's proceed step by step:
|
21 |
+
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.
|
22 |
+
2. Identify all the proposed answers.
|
23 |
+
3. Write the question and answers in a JSON file following the given example format.
|
24 |
+
|
25 |
+
Example:
|
26 |
+
- Input MCQ : "What year was America discovered? Answers: 1400, 1492, 1587, 1321"
|
27 |
+
- Output JSON: {"Question": "What year was America discovered?", {"Answers": {"Answer":"1400"}, {"Answer":"1492"}, {"Answer":"1587"}, {"Answer":"1321"}}
|
28 |
+
Answer only with the JSON file, don’t comment. Do not generate output that isn’t in properly formatted JSON. Respect the JSON format and especially the JSON keys “Question”,”Answers”,”Answer”.
|
29 |
+
"""
|
30 |
+
|
31 |
+
|
32 |
+
def process(image_path):
|
33 |
+
try:
|
34 |
+
response = client.chat.completions.create(
|
35 |
+
model="gpt-4o",
|
36 |
+
# response_format={ "type": "json_object" }, # si nécessaire
|
37 |
+
messages=[
|
38 |
+
# {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
|
39 |
+
{
|
40 |
+
"role": "user",
|
41 |
+
"content": [
|
42 |
+
{"type": "text", "text": prompts.SINGLE_QCM_PROMPT},
|
43 |
+
{
|
44 |
+
"type": "image_url",
|
45 |
+
"image_url": {
|
46 |
+
"url": f"data:image/jpeg;base64,{encode_image(image_path)}"
|
47 |
+
},
|
48 |
+
},
|
49 |
+
],
|
50 |
+
}
|
51 |
+
],
|
52 |
+
temperature=0.2,
|
53 |
+
# max_tokens=256,
|
54 |
+
# top_p=1,
|
55 |
+
# frequency_penalty=0,
|
56 |
+
# presence_penalty=0
|
57 |
+
)
|
58 |
+
return response.choices[0].message.content
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
print(f"an error occurred : {e}")
|
62 |
+
return {"error": str(e)}
|
63 |
+
|
64 |
+
|
65 |
+
import gradio as gr
|
66 |
+
|
67 |
+
iface = gr.Interface(
|
68 |
+
fn=process,
|
69 |
+
inputs=gr.Image(type="filepath"),
|
70 |
+
outputs=gr.JSON(),
|
71 |
+
)
|
72 |
+
|
73 |
+
iface.launch()
|