Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from io import StringIO
|
| 4 |
+
from preprocess_test import Preprocess_Test
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class Model(nn.Module):
|
| 13 |
+
def __init__(self, input_shape, num_classes):
|
| 14 |
+
super(Model, self).__init__()
|
| 15 |
+
self.fc1 = nn.Linear(input_shape, 1024)
|
| 16 |
+
self.bn1 = nn.BatchNorm1d(1024)
|
| 17 |
+
self.fc2 = nn.Linear(1024, 512)
|
| 18 |
+
self.bn2 = nn.BatchNorm1d(512)
|
| 19 |
+
self.fc3 = nn.Linear(512, 256)
|
| 20 |
+
self.bn3 = nn.BatchNorm1d(256)
|
| 21 |
+
self.fc4 = nn.Linear(256, num_classes)
|
| 22 |
+
|
| 23 |
+
def forward(self, x):
|
| 24 |
+
x = F.relu(self.bn1(self.fc1(x)))
|
| 25 |
+
x = F.relu(self.bn2(self.fc2(x)))
|
| 26 |
+
x = F.relu(self.bn3(self.fc3(x)))
|
| 27 |
+
x = self.fc4(x)
|
| 28 |
+
return x
|
| 29 |
+
app=Flask(__name__)
|
| 30 |
+
app.config["TEMPLATES_AUTO_RELOAD"] = True
|
| 31 |
+
|
| 32 |
+
@app.route("/",methods=["GET"])
|
| 33 |
+
def root():
|
| 34 |
+
return f"Test"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@app.route("/test", methods=["POST"])
|
| 38 |
+
def test():
|
| 39 |
+
# Check if file is in request
|
| 40 |
+
if 'file' not in request.files:
|
| 41 |
+
return jsonify({"error": "No file part"}), 400
|
| 42 |
+
|
| 43 |
+
file = request.files['file']
|
| 44 |
+
|
| 45 |
+
# Check if file is selected
|
| 46 |
+
if file.filename == '':
|
| 47 |
+
return jsonify({"error": "No selected file"}), 400
|
| 48 |
+
|
| 49 |
+
# Read CSV into DataFrame
|
| 50 |
+
csv_data = file.read().decode('utf-8')
|
| 51 |
+
df = pd.read_csv(StringIO(csv_data))
|
| 52 |
+
obj=Preprocess_Test(df)
|
| 53 |
+
obj.preprocess()
|
| 54 |
+
obj.test()
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
return jsonify({
|
| 58 |
+
"message": "CSV uploaded and printed",
|
| 59 |
+
"rows": len(df),
|
| 60 |
+
"columns": df.columns.tolist(),
|
| 61 |
+
"preview": df.head().to_dict(orient='records')
|
| 62 |
+
}), 200
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
app.run(debug=True,port=7860)
|