File size: 2,941 Bytes
d773e1b 4ac0bf8 d773e1b b0741bf d773e1b 4ac0bf8 d773e1b |
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 74 75 76 77 78 79 80 81 82 83 84 85 |
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from llm_agent import LLM_Agent
import os
import logging
import time
from dotenv import load_dotenv
from werkzeug.utils import secure_filename
load_dotenv()
logging.basicConfig(level=logging.INFO)
logging.getLogger('matplotlib').setLevel(logging.WARNING)
logging.getLogger('PIL').setLevel(logging.WARNING)
app = Flask(__name__, static_folder=os.path.join(os.path.dirname(__file__), '..', 'static'))
# Configure CORS to allow all origins for development
CORS(app, origins=["*"], supports_credentials=True)
agent = LLM_Agent()
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), '..', 'data', 'uploads')
ALLOWED_EXTENSIONS = {'csv', 'xls', 'xlsx'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
logging.info("Index route accessed")
return "Welcome to the Excel Plotter API. Use the /plot endpoint to make requests."
@app.route('/plot', methods=['POST'])
def plot():
start_time = time.time()
data = request.json
logging.info(f"Received request data: {data}")
file_path = data.get('file_path')
logging.info(f"File path in plot request: {file_path}")
response = agent.process_request(data)
end_time = time.time()
logging.info(f"Processed request in {end_time - start_time} seconds")
return jsonify(response)
@app.route('/static/<path:filename>')
def serve_static(filename):
logging.info(f"Serving static file: {filename}")
response = send_from_directory(app.static_folder, filename)
# Add CORS headers for images
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
response.headers.add('Access-Control-Allow-Methods', 'GET')
return response
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
# Optionally, validate columns here using DataProcessor
dp = LLM_Agent().data_processor.__class__(file_path)
columns = dp.get_columns()
preview = dp.preview(5)
return jsonify({'message': 'File uploaded successfully', 'columns': columns, 'preview': preview, 'file_path': file_path})
else:
return jsonify({'error': 'Invalid file type'}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860) |