FredOru commited on
Commit
c76398e
·
1 Parent(s): b799092

(chores) reorganised code

Browse files
Files changed (3) hide show
  1. .gitignore +131 -1
  2. app.py +9 -24
  3. prompts.py +15 -0
.gitignore CHANGED
@@ -1 +1,131 @@
1
- .DS_Store
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .DS_Store
2
+
3
+ # Byte-compiled / optimized / DLL files
4
+ __pycache__/
5
+ *.py[cod]
6
+ *$py.class
7
+
8
+ # C extensions
9
+ *.so
10
+
11
+ # Distribution / packaging
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ pip-wheel-metadata/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
30
+ MANIFEST
31
+
32
+ # PyInstaller
33
+ # Usually these files are written by a python script from a template
34
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
35
+ *.manifest
36
+ *.spec
37
+
38
+ # Installer logs
39
+ pip-log.txt
40
+ pip-delete-this-directory.txt
41
+
42
+ # Unit test / coverage reports
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+ .coverage
47
+ .coverage.*
48
+ .cache
49
+ nosetests.xml
50
+ coverage.xml
51
+ *.cover
52
+ *.py,cover
53
+ .hypothesis/
54
+ .pytest_cache/
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ local_settings.py
63
+ db.sqlite3
64
+ db.sqlite3-journal
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ .python-version
88
+
89
+ # pipenv
90
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
91
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
92
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
93
+ # install all needed dependencies.
94
+ #Pipfile.lock
95
+
96
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
97
+ __pypackages__/
98
+
99
+ # Celery stuff
100
+ celerybeat-schedule
101
+ celerybeat.pid
102
+
103
+ # SageMath parsed files
104
+ *.sage.py
105
+
106
+ # Environments
107
+ .env
108
+ .venv
109
+ env/
110
+ venv/
111
+ ENV/
112
+ env.bak/
113
+ venv.bak/
114
+
115
+ # Spyder project settings
116
+ .spyderproject
117
+ .spyproject
118
+
119
+ # Rope project settings
120
+ .ropeproject
121
+
122
+ # mkdocs documentation
123
+ /site
124
+
125
+ # mypy
126
+ .mypy_cache/
127
+ .dmypy.json
128
+ dmypy.json
129
+
130
+ # Pyre type checker
131
+ .pyre/
app.py CHANGED
@@ -1,32 +1,19 @@
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):
@@ -39,7 +26,7 @@ def process(image_path):
39
  {
40
  "role": "user",
41
  "content": [
42
- {"type": "text", "text": SINGLE_QCM_PROMPT},
43
  {
44
  "type": "image_url",
45
  "image_url": {
@@ -62,8 +49,6 @@ def process(image_path):
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"),
 
1
  import gradio as gr
2
  import base64
3
+ import prompts
 
 
 
 
 
 
 
4
  from openai import OpenAI
5
+ from dotenv import load_dotenv
6
 
7
+ load_dotenv()
8
  client = OpenAI()
9
 
10
+ PROMPT = prompts.SINGLE_QCM_PROMPT
 
 
 
11
 
 
 
 
 
12
 
13
+ # Function to encode the image
14
+ def encode_image(image_path):
15
+ with open(image_path, "rb") as image_file:
16
+ return base64.b64encode(image_file.read()).decode("utf-8")
 
17
 
18
 
19
  def process(image_path):
 
26
  {
27
  "role": "user",
28
  "content": [
29
+ {"type": "text", "text": PROMPT},
30
  {
31
  "type": "image_url",
32
  "image_url": {
 
49
  return {"error": str(e)}
50
 
51
 
 
 
52
  iface = gr.Interface(
53
  fn=process,
54
  inputs=gr.Image(type="filepath"),
prompts.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ SINGLE_QCM_PROMPT = """
2
+ You are an assistant for a primary or secondary school teacher.
3
+ The attached image is a photo or screenshot of a multiple choice question found by the teacher in an exercise book.
4
+ Your job is to convert this image into a JSON file containing the question and proposed answers.
5
+
6
+ Let's proceed step by step:
7
+ 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.
8
+ 2. Identify all the proposed answers.
9
+ 3. Write the question and answers in a JSON file following the given example format.
10
+
11
+ Example:
12
+ - Input MCQ : "What year was America discovered? Answers: 1400, 1492, 1587, 1321"
13
+ - Output JSON: {"Question": "What year was America discovered?", {"Answers": {"Answer":"1400"}, {"Answer":"1492"}, {"Answer":"1587"}, {"Answer":"1321"}}
14
+ 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”.
15
+ """