rahul7star commited on
Commit
7e22793
·
verified ·
1 Parent(s): c85d2e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from huggingface_hub import upload_file
4
+ import os
5
+
6
+ app = FastAPI()
7
+
8
+ # CORS setup to allow requests from your frontend
9
+ app.add_middleware(
10
+ CORSMiddleware,
11
+ allow_origins=["*"], # Replace "*" with your frontend domain in production
12
+ allow_credentials=True,
13
+ allow_methods=["*"],
14
+ allow_headers=["*"],
15
+ )
16
+
17
+ @app.get("/")
18
+ def health_check():
19
+ return {"status": "✅ FastAPI running on Hugging Face Spaces!"}
20
+
21
+ @app.post("/upload")
22
+ async def upload_image(file: UploadFile = File(...)):
23
+ filename = file.filename
24
+ contents = await file.read()
25
+
26
+ # Save temporarily
27
+ temp_path = f"/tmp/{filename}"
28
+ with open(temp_path, "wb") as f:
29
+ f.write(contents)
30
+
31
+ try:
32
+ upload_file(
33
+ path_or_fileobj=temp_path,
34
+ path_in_repo=f"demo/{filename}",
35
+ repo_id="rahul7star/ohamlab",
36
+ repo_type="model",
37
+ commit_message=f"Upload {filename} to demo folder",
38
+ token=True, # uses HF_TOKEN from Space secrets
39
+ )
40
+ return {"message": f"✅ {filename} uploaded successfully!"}
41
+ except Exception as e:
42
+ return {"error": f"❌ Upload failed: {str(e)}"}