File size: 6,529 Bytes
c803eb6 7ef4827 c803eb6 7ef4827 c803eb6 7ef4827 c803eb6 7ef4827 c803eb6 |
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import base64
import requests
from app_config import config
import gradio as gr
import mongo_utils as mongo
def clear():
return None, ""
# Function to encode the local image into base64 to be send over HTTP
def __encode_image(image_path: str) -> bytes:
"""
Encodes the passed image to base64 byte-stream.
This is required for passing the image over a HTTP get/post call.
"""
with open(image_path, "rb") as image_file:
base64_image = base64.b64encode(image_file.read()).decode("utf-8")
return base64_image
def solve(image_path: str):
"""
Invokes the GuruZee API passing the raw image bytes and returns the response
to client
"""
image_data = __encode_image(image_path=image_path)
headers = {"Content-Type": "application/json"}
payload = {"data": image_data}
end_point = config.GURUZEE_API_ENDPOINT + "/solve"
response = requests.post(end_point, headers=headers, json=payload)
# increment the openai access counter and compute count stats
mongo.increment_curr_access_count()
max_count = config.openai_max_access_count
curr_count = config.openai_curr_access_count
available_count = max_count - curr_count
return response.json(), max_count, curr_count, available_count
def create_interface():
js_enable_darkmode = """() =>
{
document.querySelector('body').classList.add('dark');
}"""
js_toggle_darkmode = """() =>
{
if (document.querySelectorAll('.dark').length) {
document.querySelector('body').classList.remove('dark');
} else {
document.querySelector('body').classList.add('dark');
}
}"""
with gr.Blocks(title=config.title, theme=config.theme, css=config.css) as app:
# enable darkmode
app.load(fn=None, inputs=None, outputs=None, _js=js_enable_darkmode)
with gr.Row():
darkmode_checkbox = gr.Checkbox(
label="Dark Mode", value=True, interactive=True
)
# toggle darkmode on/off when checkbox is checked/unchecked
darkmode_checkbox.change(
None, None, None, _js=js_toggle_darkmode, api_name=False
)
with gr.Row():
with gr.Column():
gr.Markdown(
"""
# GuruZee
***Where Math Meets Marvel with AI Wizardry for Primary School
Prowess! π§βπβ¨***
**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..
<br>
Select/upload an `Image` containing the problem Then hit
`GO!` button.
Alternatively, just select one of the pre-configured `Example` image
from the Example section at the bottom**
<br>
Visit the [project's repo](https://github.com/sssingh/GuruZee)
<br>
***Please exercise patience, as the models employed are extensive
and may require a few seconds to load. If you encounter an unrelated
response, it is likely still loading; wait a moment and try again.***
"""
)
with gr.Column():
max_count = gr.Textbox(
label="Max allowed OpenAI requests:",
value=config.openai_max_access_count,
)
curr_count = gr.Textbox(
label="Used up OpenAI requests:",
value=config.openai_curr_access_count,
)
available_count = gr.Textbox(
label="Available OpenAI requests:",
value=config.openai_max_access_count
- config.openai_curr_access_count,
)
with gr.Row():
with gr.Column():
image = gr.Image(
type="filepath",
)
with gr.Row():
submit_button = gr.Button(value="GO!", elem_classes="orange-button")
clear_button = gr.ClearButton(elem_classes="gray-button")
with gr.Column():
answer = gr.Textbox(
label="Answer:",
placeholder="Answer will appear here.",
lines=20,
)
with gr.Row():
with gr.Accordion("Expand for examples:", open=False):
gr.Examples(
examples=[
[
"assets/examples/1.png",
],
[
"assets/examples/2.png",
],
[
"assets/examples/3.png",
],
[
"assets/examples/4.png",
],
[
"assets/examples/5.png",
],
[
"assets/examples/6.png",
],
[
"assets/examples/7.png",
],
[
"assets/examples/8.png",
],
[
"assets/examples/9.png",
],
[
"assets/examples/10.png",
],
],
fn=solve,
inputs=[image],
outputs=[answer, max_count, curr_count, available_count],
run_on_click=True,
)
submit_button.click(
fn=solve,
inputs=[image],
outputs=[answer, max_count, curr_count, available_count],
)
clear_button.click(fn=clear, inputs=[], outputs=[image, answer])
image.clear(fn=clear, inputs=[], outputs=[image, answer])
return app
if __name__ == "__main__":
mongo.fetch_curr_access_count()
app = create_interface()
app.launch()
|