Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Global variable to hold the uploaded CSV data
|
| 10 |
+
uploaded_data = None
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@app.route("/")
|
| 14 |
+
def index():
|
| 15 |
+
return render_template("index.html")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@app.route("/upload", methods=["POST"])
|
| 19 |
+
def upload():
|
| 20 |
+
global uploaded_data
|
| 21 |
+
file = request.files["file"]
|
| 22 |
+
uploaded_data = pd.read_csv(file)
|
| 23 |
+
return jsonify({"message": "File uploaded successfully"})
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@app.route("/plot", methods=["GET"])
|
| 27 |
+
def plot():
|
| 28 |
+
global uploaded_data
|
| 29 |
+
if uploaded_data is None:
|
| 30 |
+
return jsonify({"error": "No data uploaded"})
|
| 31 |
+
|
| 32 |
+
plt.figure()
|
| 33 |
+
uploaded_data.plot(kind="bar")
|
| 34 |
+
img = io.BytesIO()
|
| 35 |
+
plt.savefig(img, format="png")
|
| 36 |
+
img.seek(0)
|
| 37 |
+
plot_url = base64.b64encode(img.getvalue()).decode()
|
| 38 |
+
return jsonify({"plot_url": plot_url})
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@app.route("/ask", methods=["POST"])
|
| 42 |
+
def ask():
|
| 43 |
+
global uploaded_data
|
| 44 |
+
question = request.json.get("question")
|
| 45 |
+
if uploaded_data is None:
|
| 46 |
+
return jsonify({"answer": "No data uploaded"})
|
| 47 |
+
# Implement a simple query or analysis
|
| 48 |
+
if question.lower() == "show summary":
|
| 49 |
+
return jsonify({"answer": uploaded_data.describe().to_html()})
|
| 50 |
+
return jsonify({"answer": "Question not understood"})
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
app.run(debug=True)
|