Spaces:
Running
Running
File size: 2,112 Bytes
58295fd |
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 |
import base64
from flask import Flask, request, jsonify, render_template
from io import BytesIO
from maincode import *
import re
app = Flask(__name__)
def calculate(expression):
try:
if isinstance(expression, list):
result = []
for eq in expression:
result.append(eval(eq.replace("=", "").replace(" ", "").strip()))
return result
else:
return eval(expression.replace("=", ""))
except Exception as e:
print(f"Error : {e}, {expression}")
return []
def advanced_calculator(text):
text = text.replace(" ", "").strip()
pattern = r'(\d+(?:\s*[+\-*/]\s*\d+)*)\s?='
matches = re.findall(pattern, text)
filtered_expressions = []
for match in matches:
equation = match + '='
remaining_text = text[text.find(equation)+len(equation):].strip()
if remaining_text and remaining_text[0].isdigit():
continue
filtered_expressions.append(equation.replace(" ", "").strip())
return filtered_expressions
@app.route('/')
def home():
return render_template('index.html')
@app.route('/whitebai', methods=['POST'])
def whitebai():
data = request.get_json()
image_data = data.get('image')
if not image_data:
return jsonify({'message': 'No image data received'}), 400
image_data = image_data.split(',')[1]
image_bytes = base64.b64decode(image_data)
image_file = BytesIO(image_bytes)
elt = ExtractTextFromImage(image_file)
elt.process_file()
dat = elt.get_text()
dtaf = []
for eq in advanced_calculator(dat):
font_size, new_location = elt.get_font_size_and_position(eq)
dtaf.append(
{
"equation" : eq,
"size" : font_size,
"location" : new_location,
"result" : calculate(eq)
}
)
return jsonify(
{
'message': "Done",
"status" : True,
"data" : dtaf,
"text" : dat.strip()
}
)
if __name__ == '__main__':
app.run(debug=True)
|