Kalpokoch commited on
Commit
e769917
·
1 Parent(s): 48ee6cb

upload functoins added to app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -3
app.py CHANGED
@@ -1,7 +1,61 @@
1
- from fastapi import FastAPI
 
 
 
 
 
 
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  @app.get("/")
6
- def greet_json():
7
- return {"Welcome to OpenQuery"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware # Important for frontend
3
+ import uuid
4
+ import logging
5
+
6
+ # Configure logging
7
+ logging.basicConfig(level=logging.INFO)
8
+ logger = logging.getLogger(__name__)
9
 
10
  app = FastAPI()
11
 
12
+ # --- IMPORTANT: CORS Middleware ---
13
+ # This allows your Netlify frontend to talk to this backend.
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=["*"], # Or specify your Netlify URL for production
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+
22
+ # In-memory session store
23
+ SESSION_DATA = {}
24
+ logger.info("Session store initialized.")
25
+
26
  @app.get("/")
27
+ def read_root():
28
+ return {"message": "Universal Data AI is running!"}
29
+
30
+ @app.post("/upload")
31
+ async def upload_file(file: UploadFile = File(...)):
32
+ """
33
+ Accepts a file, creates a session, and stores the file content in memory.
34
+ """
35
+ session_id = str(uuid.uuid4())
36
+ logger.info(f"New upload received. Creating session_id: {session_id}")
37
+
38
+ try:
39
+ # For now, we'll just store the content and filename.
40
+ # Later, this is where you'll call your parsing functions.
41
+ content = await file.read()
42
+ SESSION_DATA[session_id] = {
43
+ "filename": file.filename,
44
+ "content_type": file.content_type,
45
+ "data": content # In a real app, you'd parse this
46
+ }
47
+ logger.info(f"File '{file.filename}' stored for session {session_id}")
48
+ return {"session_id": session_id, "filename": file.filename}
49
+ except Exception as e:
50
+ logger.error(f"Error processing file for session {session_id}: {e}")
51
+ raise HTTPException(status_code=500, detail="Error processing file.")
52
+
53
+ @app.get("/session/{session_id}")
54
+ def get_session_data(session_id: str):
55
+ """
56
+ Retrieves the data stored for a given session.
57
+ """
58
+ if session_id not in SESSION_DATA:
59
+ raise HTTPException(status_code=404, detail="Session not found.")
60
+ # Return metadata, not the raw data, for privacy
61
+ return {"filename": SESSION_DATA[session_id].get("filename")}