Spaces:
Paused
Paused
| 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 | |
| def home(): | |
| return render_template('index.html') | |
| 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) | |