Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -204,24 +204,36 @@ async def process_file(file: UploadFile = File(...)):
|
|
| 204 |
else:
|
| 205 |
raise HTTPException(status_code=400, detail="Unsupported file type")
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
df = df.replace({np.nan: None})
|
| 208 |
text_reps = [" ".join(f"{col}: {str(val)}" for col, val in row.items() if val is not None)
|
| 209 |
for _, row in df.iterrows()]
|
| 210 |
|
| 211 |
embeddings = model.encode(text_reps)
|
| 212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
return JSONResponse({
|
| 214 |
'embeddings': embeddings.tolist(),
|
| 215 |
'metadata': {
|
| 216 |
'columns': list(df.columns),
|
| 217 |
'row_count': len(df)
|
| 218 |
},
|
| 219 |
-
'raw_data':
|
| 220 |
})
|
| 221 |
|
| 222 |
except Exception as e:
|
| 223 |
raise HTTPException(status_code=500, detail=str(e))
|
| 224 |
-
|
| 225 |
@app.post("/api/query")
|
| 226 |
async def query_data(request: QueryRequest):
|
| 227 |
try:
|
|
|
|
| 204 |
else:
|
| 205 |
raise HTTPException(status_code=400, detail="Unsupported file type")
|
| 206 |
|
| 207 |
+
# Convert timestamps to strings
|
| 208 |
+
for col in df.select_dtypes(include=['datetime64[ns]']).columns:
|
| 209 |
+
df[col] = df[col].astype(str)
|
| 210 |
+
|
| 211 |
df = df.replace({np.nan: None})
|
| 212 |
text_reps = [" ".join(f"{col}: {str(val)}" for col, val in row.items() if val is not None)
|
| 213 |
for _, row in df.iterrows()]
|
| 214 |
|
| 215 |
embeddings = model.encode(text_reps)
|
| 216 |
|
| 217 |
+
# Convert DataFrame to JSON-serializable format
|
| 218 |
+
raw_data = df.to_dict('records')
|
| 219 |
+
# Convert any non-serializable types to strings
|
| 220 |
+
for record in raw_data:
|
| 221 |
+
for key, value in record.items():
|
| 222 |
+
if not isinstance(value, (str, int, float, bool, type(None))):
|
| 223 |
+
record[key] = str(value)
|
| 224 |
+
|
| 225 |
return JSONResponse({
|
| 226 |
'embeddings': embeddings.tolist(),
|
| 227 |
'metadata': {
|
| 228 |
'columns': list(df.columns),
|
| 229 |
'row_count': len(df)
|
| 230 |
},
|
| 231 |
+
'raw_data': raw_data
|
| 232 |
})
|
| 233 |
|
| 234 |
except Exception as e:
|
| 235 |
raise HTTPException(status_code=500, detail=str(e))
|
| 236 |
+
|
| 237 |
@app.post("/api/query")
|
| 238 |
async def query_data(request: QueryRequest):
|
| 239 |
try:
|