wintergw commited on
Commit
3ae7e82
·
verified ·
1 Parent(s): e4f0059

Upload app2.py

Browse files
Files changed (1) hide show
  1. app2.py +46 -0
app2.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from fastapi import FastAPI, File, UploadFile
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import uvicorn
5
+ import io
6
+ import pandas as pd
7
+ import threading
8
+
9
+ # Create FastAPI app
10
+ app = FastAPI()
11
+
12
+ # Allow public Space to access this private Space API
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"], # You can restrict this to your public Space URL if needed
16
+ allow_methods=["POST"],
17
+ allow_headers=["*"],
18
+ )
19
+
20
+ # Define API endpoint to process files
21
+ @app.post("/run")
22
+ async def process_file(file: UploadFile = File(...)):
23
+ try:
24
+ contents = await file.read()
25
+ df = pd.read_excel(io.BytesIO(contents))
26
+
27
+ # Process the file (example: return first few rows)
28
+ result = df.head().to_dict()
29
+
30
+ return {"status": "success", "preview": result}
31
+
32
+ except Exception as e:
33
+ return {"status": "error", "message": str(e)}
34
+
35
+ # Function to run FastAPI on a separate thread
36
+ def run_fastapi():
37
+ uvicorn.run(app, host="0.0.0.0", port=7860)
38
+
39
+ # Start FastAPI in a separate thread
40
+ threading.Thread(target=run_fastapi, daemon=True).start()
41
+
42
+ # Streamlit UI (For Debugging in Private Space)
43
+ st.title("📐 Math Assignment Optimizer (Private API)")
44
+ st.write("This private Space runs a FastAPI backend.")
45
+
46
+ st.warning("⚠️ This app is designed to be accessed via API, not directly.")