Skip to content

Commit 5364329

Browse files
Update app.py
1 parent 1232854 commit 5364329

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

backend/app.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
import matplotlib
66
matplotlib.use('Agg')
77
import 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
910
from sklearn.linear_model import LinearRegression
1011
import numpy as np
1112
import openai
1213
from 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
1820
UPLOAD_FOLDER = "uploads"
19-
STATIC_FOLDER = "static"
2021
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
21-
os.makedirs(STATIC_FOLDER, exist_ok=True)
2222

2323
# Capture logs
2424
log_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("/")
3232
def 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"])
3737
def 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"])
109114
def 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

Comments
 (0)