Spaces:
Sleeping
Sleeping
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": 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() | |