Sunil Surendra Singh
commited on
Commit
Β·
9b9203a
1
Parent(s):
d29a943
skeleton server code
Browse files- .gitignore +1 -1
- README.md +17 -0
- requirements.txt +2 -0
- src/app_config.py +10 -0
- src/client.py +1 -0
- src/server.py +60 -0
.gitignore
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# MY CHNAGES
|
2 |
-
|
3 |
notebook*/
|
4 |
venv*/
|
5 |
*.excalidraw
|
|
|
1 |
# MY CHNAGES
|
2 |
+
.env
|
3 |
notebook*/
|
4 |
venv*/
|
5 |
*.excalidraw
|
README.md
CHANGED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# GuruZee
|
2 |
+
> Where Math Meets Marvel with AI Wizardry for Primary School Prowess! π§βπβ¨
|
3 |
+
|
4 |
+
## Introduction
|
5 |
+
GuruZee, your go-to guru for primary school math (for now). Picture this. GuruZee whips up question papers with answers and detailed explanations, all based on those elusive course chapter images. Not just that, it's a math-solving maestro, tackling problems presented as images with ease.
|
6 |
+
|
7 |
+
## Objectives
|
8 |
+
1. Given just an image of primary school math's problem, GuruZee shall produce the
|
9 |
+
solution and clear explanation of the solution.
|
10 |
+
2. Given the images of a chapter from primary school maths GuruZee will generate the
|
11 |
+
question paper for various difficulty level based on the content's in the chapter.
|
12 |
+
|
13 |
+
## Proposed Architecture
|
14 |
+
[TODO]
|
15 |
+
|
16 |
+
## How to use
|
17 |
+
[TODO]
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
requests
|
src/app_config.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dataclasses import dataclass
|
2 |
+
|
3 |
+
|
4 |
+
@dataclass
|
5 |
+
class __AppConfig:
|
6 |
+
openai_api_endpoint = "https://api.openai.com/v1/chat/completions"
|
7 |
+
openai_api_key = ""
|
8 |
+
|
9 |
+
|
10 |
+
config = __AppConfig()
|
src/client.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
## Test code for testing the server API goes here
|
src/server.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import requests
|
3 |
+
from app_config import config
|
4 |
+
|
5 |
+
|
6 |
+
# Function to encode the local image into base64 to be send over HTTP
|
7 |
+
def local_image_to_url(image_path):
|
8 |
+
with open(image_path, "rb") as image_file:
|
9 |
+
base64_image = base64.b64encode(image_file.read()).decode("utf-8")
|
10 |
+
return {"url": f"data:image/jpeg;base64,{base64_image}"}
|
11 |
+
|
12 |
+
|
13 |
+
def analyze_single_image(image_path: str, instruction: str, mode="url"): # "local"
|
14 |
+
if mode == "local":
|
15 |
+
image_path = local_image_to_url(image_path)
|
16 |
+
headers = {
|
17 |
+
"Content-Type": "application/json",
|
18 |
+
"Authorization": f"Bearer {config.openai_api_key}",
|
19 |
+
}
|
20 |
+
payload = {
|
21 |
+
"model": "gpt-4-vision-preview",
|
22 |
+
"temperature": 0.2,
|
23 |
+
"messages": [
|
24 |
+
{
|
25 |
+
"role": "system",
|
26 |
+
"contents": """You an expert primary school maths teacher. Given an image
|
27 |
+
of a primary school maths problem you can analyze the problem and produce
|
28 |
+
a detailed solution with explanation."""
|
29 |
+
# In addition to this, given a
|
30 |
+
# collection images from primary school maths text book you can generate
|
31 |
+
# questions and there answers based on the topic shown in text book images,
|
32 |
+
# you will provide the detailed answers with explanation""",
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"role": "user",
|
36 |
+
"content": [
|
37 |
+
{"type": "text", "text": instruction},
|
38 |
+
{
|
39 |
+
"type": "image_url",
|
40 |
+
"image_url": image_path,
|
41 |
+
},
|
42 |
+
],
|
43 |
+
},
|
44 |
+
],
|
45 |
+
"max_tokens": 600,
|
46 |
+
}
|
47 |
+
|
48 |
+
response = requests.post(config.openai_api_endpoint, headers=headers, json=payload)
|
49 |
+
return response.json()
|
50 |
+
|
51 |
+
|
52 |
+
def solve(problem_image: str, mode="local"):
|
53 |
+
print("P R O B L E M:\n--------------")
|
54 |
+
display(Image(filename=problem_image)) # only in notebook
|
55 |
+
instruction = """Analyze the 4th grade math problem in the image. Strictly first provide the answer to the problem and then only the solution explanation.
|
56 |
+
Put a newline after each 80 chars"""
|
57 |
+
output = analyze_image(problem_image, instruction, mode)
|
58 |
+
print("A N S W E R:\n------------")
|
59 |
+
print(output["choices"][0]["message"]["content"])
|
60 |
+
return output["choices"][0]["message"]["content"]
|