File size: 2,629 Bytes
2d11d60
f3efe05
2d11d60
 
f3efe05
 
 
 
2d11d60
 
f3efe05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr
import base64


# Function to encode the image
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")


from openai import OpenAI

client = OpenAI()

SINGLE_QCM_PROMPT = """
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 and proposed answers. 

Let's proceed step by step:
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.
2. Identify all the proposed answers.
3. Write the question and answers in a JSON file following the given example format.

Example:
- Input MCQ : "What year was America discovered? Answers: 1400, 1492, 1587, 1321"
- Output JSON: {"Question": "What year was America discovered?", {"Answers": {"Answer":"1400"}, {"Answer":"1492"}, {"Answer":"1587"}, {"Answer":"1321"}}
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”.
"""


def process(image_path):
    try:
        response = client.chat.completions.create(
            model="gpt-4o",
            # response_format={ "type": "json_object" }, # si nécessaire
            messages=[
                # {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": prompts.SINGLE_QCM_PROMPT},
                        {
                            "type": "image_url",
                            "image_url": {
                                "url": f"data:image/jpeg;base64,{encode_image(image_path)}"
                            },
                        },
                    ],
                }
            ],
            temperature=0.2,
            #    max_tokens=256,
            #    top_p=1,
            #    frequency_penalty=0,
            #    presence_penalty=0
        )
        return response.choices[0].message.content

    except Exception as e:
        print(f"an error occurred : {e}")
        return {"error": str(e)}


import gradio as gr

iface = gr.Interface(
    fn=process,
    inputs=gr.Image(type="filepath"),
    outputs=gr.JSON(),
)

iface.launch()