Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,51 +1,72 @@
|
|
1 |
-
import base64
|
2 |
-
from flask import Flask, request, jsonify, render_template
|
3 |
-
from io import BytesIO
|
4 |
-
from maincode import *
|
5 |
-
import re
|
6 |
-
|
7 |
-
app = Flask(__name__)
|
8 |
-
|
9 |
-
def calculate(expression):
|
10 |
-
try:
|
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 |
-
image_data =
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
from flask import Flask, request, jsonify, render_template
|
3 |
+
from io import BytesIO
|
4 |
+
from maincode import *
|
5 |
+
import re
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
def calculate(expression):
|
10 |
+
try:
|
11 |
+
if isinstance(expression, list):
|
12 |
+
result = []
|
13 |
+
for eq in expression:
|
14 |
+
result.append(eval(eq.replace("=", "").replace(" ", "").strip()))
|
15 |
+
return result
|
16 |
+
else:
|
17 |
+
return eval(expression.replace("=", ""))
|
18 |
+
except Exception as e:
|
19 |
+
print(f"Error : {e}, {expression}")
|
20 |
+
return []
|
21 |
+
|
22 |
+
def advanced_calculator(text):
|
23 |
+
text = text.replace(" ", "").strip()
|
24 |
+
pattern = r'(\d+(?:\s*[+\-*/]\s*\d+)*)\s?='
|
25 |
+
matches = re.findall(pattern, text)
|
26 |
+
filtered_expressions = []
|
27 |
+
for match in matches:
|
28 |
+
equation = match + '='
|
29 |
+
remaining_text = text[text.find(equation)+len(equation):].strip()
|
30 |
+
if remaining_text and remaining_text[0].isdigit():
|
31 |
+
continue
|
32 |
+
filtered_expressions.append(equation.replace(" ", "").strip())
|
33 |
+
return filtered_expressions
|
34 |
+
|
35 |
+
@app.route('/')
|
36 |
+
def home():
|
37 |
+
return render_template('index.html')
|
38 |
+
|
39 |
+
@app.route('/whitebai', methods=['POST'])
|
40 |
+
def whitebai():
|
41 |
+
data = request.get_json()
|
42 |
+
image_data = data.get('image')
|
43 |
+
if not image_data:
|
44 |
+
return jsonify({'message': 'No image data received'}), 400
|
45 |
+
image_data = image_data.split(',')[1]
|
46 |
+
image_bytes = base64.b64decode(image_data)
|
47 |
+
image_file = BytesIO(image_bytes)
|
48 |
+
elt = ExtractTextFromImage(image_file)
|
49 |
+
elt.process_file()
|
50 |
+
dat = elt.get_text()
|
51 |
+
dtaf = []
|
52 |
+
for eq in advanced_calculator(dat):
|
53 |
+
font_size, new_location = elt.get_font_size_and_position(eq)
|
54 |
+
dtaf.append(
|
55 |
+
{
|
56 |
+
"equation" : eq,
|
57 |
+
"size" : font_size,
|
58 |
+
"location" : new_location,
|
59 |
+
"result" : calculate(eq)
|
60 |
+
}
|
61 |
+
)
|
62 |
+
return jsonify(
|
63 |
+
{
|
64 |
+
'message': "Done",
|
65 |
+
"status" : True,
|
66 |
+
"data" : dtaf,
|
67 |
+
"text" : dat.strip()
|
68 |
+
}
|
69 |
+
)
|
70 |
+
|
71 |
+
if __name__ == '__main__':
|
72 |
+
app.run(debug=True)
|