55import matplotlib
66matplotlib .use ('Agg' )
77import matplotlib .pyplot as plt
8- from flask import Flask , request , jsonify , send_from_directory
8+ from flask import Flask , request , jsonify , send_file
9+ from flask_cors import CORS
910from sklearn .linear_model import LinearRegression
1011import numpy as np
1112import openai
1213from datetime import datetime
1314
14- # Initialize Flask app and set static folder
15- app = Flask (__name__ , static_folder = "static" )
15+ # Initialize Flask app
16+ app = Flask (__name__ )
17+ CORS (app ) # Enable CORS for all routes
1618
1719# Directories
1820UPLOAD_FOLDER = "uploads"
19- STATIC_FOLDER = "static"
2021os .makedirs (UPLOAD_FOLDER , exist_ok = True )
21- os .makedirs (STATIC_FOLDER , exist_ok = True )
2222
2323# Capture logs
2424log_stream = io .StringIO ()
@@ -27,12 +27,12 @@ def log_print(*args):
2727 print (* args , file = log_stream )
2828 sys .stdout .flush ()
2929
30- # Serve the frontend
30+ # Root route for health check
3131@app .route ("/" )
3232def index ():
33- return send_from_directory ( STATIC_FOLDER , "index.html" )
33+ return jsonify ({ "message" : "✅ AI DataScience Backend is running on Azure." } )
3434
35- # Handle file upload
35+ # Handle file upload and processing
3636@app .route ("/upload" , methods = ["POST" ])
3737def upload_file ():
3838 log_stream .truncate (0 )
@@ -57,7 +57,7 @@ def upload_file():
5757 plt .xlabel ('X' )
5858 plt .ylabel ('Y' )
5959 plt .title ('Scatter Plot' )
60- plt .savefig (f" { STATIC_FOLDER } / plot.png" )
60+ plt .savefig (" plot.png" )
6161 plt .close ()
6262 log_print ("📊 Scatter plot saved." )
6363
@@ -101,10 +101,15 @@ def upload_file():
101101 "summary" : summary ,
102102 "log" : log_stream .getvalue (),
103103 "forecast" : "Submit future x-values below to get predictions." ,
104- "plot_url" : "/static/ plot.png"
104+ "plot_url" : request . url_root + " plot.png"
105105 })
106106
107- # Handle prediction
107+ # Serve the generated plot
108+ @app .route ("/plot.png" )
109+ def serve_plot ():
110+ return send_file ("plot.png" , mimetype = "image/png" )
111+
112+ # Handle prediction requests
108113@app .route ("/predict" , methods = ["POST" ])
109114def predict ():
110115 future_x = request .form .get ("future_x" )
@@ -149,7 +154,7 @@ def predict():
149154 return jsonify ({
150155 "forecast" : result ,
151156 "log" : log_stream .getvalue (),
152- "plot_url" : "/static/ plot.png"
157+ "plot_url" : request . url_root + " plot.png"
153158 })
154159
155160 except Exception as e :
@@ -159,5 +164,3 @@ def predict():
159164 "log" : log_stream .getvalue (),
160165 "plot_url" : None
161166 })
162-
163-
0 commit comments