5m4ck3r commited on
Commit
58295fd
·
verified ·
1 Parent(s): a288c83

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +72 -51
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
- result = []
12
- for eq in expression:
13
- result.append(eval(eq.replace("=", "")))
14
- print(result)
15
- return result
16
- except Exception as e:
17
- print(f"Error : {e}")
18
- return []
19
-
20
- def advanced_calculator(text):
21
- pattern = r'(\d+(?:\s*[+\-*/]\s*\d+)*)\s?='
22
- matches = re.findall(pattern, text)
23
- filtered_expressions = []
24
- for match in matches:
25
- equation = match + '='
26
- remaining_text = text[text.find(equation)+len(equation):].strip()
27
- if remaining_text and remaining_text[0].isdigit():
28
- continue
29
- filtered_expressions.append(equation.replace(" ", "").strip())
30
- return calculate(filtered_expressions)
31
-
32
- @app.route('/')
33
- def home():
34
- return render_template('index.html')
35
-
36
- @app.route('/whitebai', methods=['POST'])
37
- def whitebai():
38
- data = request.get_json()
39
- image_data = data.get('image')
40
- if not image_data:
41
- return jsonify({'message': 'No image data received'}), 400
42
- image_data = image_data.split(',')[1]
43
- image_bytes = base64.b64decode(image_data)
44
- image_file = BytesIO(image_bytes)
45
- elt = ExtractTextFromImage(image_file)
46
- elt.process_file()
47
- dat = elt.get_text()
48
- return jsonify({'message': advanced_calculator(dat)})
49
-
50
- if __name__ == '__main__':
51
- app.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)