Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,90 +14,6 @@ SESSION_KEY_PREFIX = "data_tool_session_id"
|
|
14 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
15 |
app.config['MAX_CONTENT_LENGTH'] = 512 * 1024 * 1024 # 512 MB
|
16 |
|
17 |
-
# === Cleanup Thread: delete files older than 60 minutes ===
|
18 |
-
def clean_old_files(folder=UPLOAD_FOLDER, max_age=60):
|
19 |
-
def cleanup_loop():
|
20 |
-
while True:
|
21 |
-
now = time.time()
|
22 |
-
for f in os.listdir(folder):
|
23 |
-
path = os.path.join(folder, f)
|
24 |
-
if os.path.isfile(path):
|
25 |
-
if now - os.path.getmtime(path) > max_age * 60:
|
26 |
-
try:
|
27 |
-
os.remove(path)
|
28 |
-
print(f"[Cleanup] Deleted: {path}")
|
29 |
-
except Exception as e:
|
30 |
-
print(f"[Cleanup Error] {e}")
|
31 |
-
time.sleep(600) # Every 10 minutes
|
32 |
-
|
33 |
-
threading.Thread(target=cleanup_loop, daemon=True).start()
|
34 |
-
|
35 |
-
# Start cleanup thread
|
36 |
-
clean_old_files()
|
37 |
-
|
38 |
-
# === Instruction Parser ===
|
39 |
-
def apply_instruction(df, instruction):
|
40 |
-
instruction = instruction.lower()
|
41 |
-
|
42 |
-
try:
|
43 |
-
match = re.search(r"drop column (\w+)", instruction)
|
44 |
-
if match:
|
45 |
-
df = df.drop(columns=[match.group(1)])
|
46 |
-
|
47 |
-
if "remove duplicates" in instruction:
|
48 |
-
df = df.drop_duplicates()
|
49 |
-
|
50 |
-
if "drop missing" in instruction or "remove null" in instruction:
|
51 |
-
df = df.dropna()
|
52 |
-
|
53 |
-
match = re.search(r"fill missing.*with ([\w\.]+)", instruction)
|
54 |
-
if match:
|
55 |
-
val = match.group(1)
|
56 |
-
try: val = float(val)
|
57 |
-
except: pass
|
58 |
-
df = df.fillna(val)
|
59 |
-
|
60 |
-
match = re.search(r"sort by (\w+)( descending| desc)?", instruction)
|
61 |
-
if match:
|
62 |
-
col = match.group(1)
|
63 |
-
ascending = not bool(match.group(2))
|
64 |
-
df = df.sort_values(by=col, ascending=ascending)
|
65 |
-
|
66 |
-
match = re.search(r"rename column (\w+) to (\w+)", instruction)
|
67 |
-
if match:
|
68 |
-
df = df.rename(columns={match.group(1): match.group(2)})
|
69 |
-
|
70 |
-
match = re.search(r"filter where (\w+) > (\d+)", instruction)
|
71 |
-
if match:
|
72 |
-
df = df[df[match.group(1)] > float(match.group(2))]
|
73 |
-
|
74 |
-
match = re.search(r"group by (\w+) and sum (\w+)", instruction)
|
75 |
-
if match:
|
76 |
-
df = df.groupby(match.group(1))[match.group(2)].sum().reset_index()
|
77 |
-
|
78 |
-
match = re.search(r"add column (\w+) as (\w+) \+ (\w+)", instruction)
|
79 |
-
if match:
|
80 |
-
df[match.group(1)] = df[match.group(2)] + df[match.group(3)]
|
81 |
-
|
82 |
-
match = re.search(r"normalize column (\w+)", instruction)
|
83 |
-
if match:
|
84 |
-
col = match.group(1)
|
85 |
-
from flask import Flask, request, jsonify, send_file
|
86 |
-
from flask_cors import CORS
|
87 |
-
import pandas as pd
|
88 |
-
import os
|
89 |
-
import threading
|
90 |
-
import time
|
91 |
-
import re
|
92 |
-
|
93 |
-
app = Flask(__name__)
|
94 |
-
CORS(app)
|
95 |
-
|
96 |
-
UPLOAD_FOLDER = "/tmp"
|
97 |
-
SESSION_KEY_PREFIX = "data_tool_session_id"
|
98 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
99 |
-
app.config['MAX_CONTENT_LENGTH'] = 512 * 1024 * 1024 # 512 MB
|
100 |
-
|
101 |
# === Root Route (Required for Hugging Face) ===
|
102 |
@app.route("/", methods=["GET"])
|
103 |
def root():
|
|
|
14 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
15 |
app.config['MAX_CONTENT_LENGTH'] = 512 * 1024 * 1024 # 512 MB
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# === Root Route (Required for Hugging Face) ===
|
18 |
@app.route("/", methods=["GET"])
|
19 |
def root():
|