|
|
from flask import Flask, request, jsonify, render_template
|
|
|
import os
|
|
|
from werkzeug.utils import secure_filename
|
|
|
from pix2text import Pix2Text
|
|
|
from PIL import Image
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
app.config['UPLOAD_FOLDER'] = 'static/uploads'
|
|
|
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'}
|
|
|
|
|
|
p2t = Pix2Text()
|
|
|
|
|
|
def allowed_file(filename):
|
|
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
|
|
|
|
|
|
@app.route('/math/process', methods=['POST'])
|
|
|
def process_math():
|
|
|
if 'image' not in request.files:
|
|
|
return jsonify({'success': False, 'error': 'No file part'})
|
|
|
|
|
|
file = request.files['image']
|
|
|
if file.filename == '':
|
|
|
return jsonify({'success': False, 'error': 'No selected file'})
|
|
|
|
|
|
if file and allowed_file(file.filename):
|
|
|
filename = secure_filename(file.filename)
|
|
|
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
|
|
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
|
|
file.save(filepath)
|
|
|
|
|
|
try:
|
|
|
result = p2t(Image.open(filepath))
|
|
|
return jsonify({'success': True, 'latex': result, 'image_path': filepath})
|
|
|
except Exception as e:
|
|
|
return jsonify({'success': False, 'error': str(e)})
|
|
|
else:
|
|
|
return jsonify({'success': False, 'error': 'Invalid file type'})
|
|
|
|