UdasriHasindu commited on
Commit
3190f06
·
1 Parent(s): a2a05f0

add CORS middleware

Browse files
Files changed (2) hide show
  1. .github/workflows/deploy.yml +24 -10
  2. main.py +10 -0
.github/workflows/deploy.yml CHANGED
@@ -1,4 +1,4 @@
1
- name: Deploy to Hugging Face
2
 
3
  on:
4
  push:
@@ -14,19 +14,33 @@ jobs:
14
  with:
15
  fetch-depth: 0
16
 
17
- - name: Push to Hugging Face Hub
 
 
 
 
 
18
  env:
19
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
20
- HF_USERNAME: xplorers
21
- SPACE_NAME: MIS_API
22
  run: |
 
23
  set -euo pipefail
24
 
25
- # Strip possible CR/LF just in case the secret was pasted with a trailing newline
26
- HF_TOKEN="$(printf '%s' "$HF_TOKEN" | tr -d '\r\n')"
 
 
 
 
 
 
 
 
 
 
27
 
28
- remote="https://huggingface.co/spaces/${HF_USERNAME}/${SPACE_NAME}"
 
 
29
 
30
- # Use the token via HTTP header (no URL parsing issues)
31
- git -c http.extraheader="Authorization: Bearer ${HF_TOKEN}" \
32
- push --force "${remote}" HEAD:main
 
1
+ name: Deploy to Hugging Face Spaces
2
 
3
  on:
4
  push:
 
14
  with:
15
  fetch-depth: 0
16
 
17
+ - name: Configure Git
18
+ run: |
19
+ git config --global user.name "GitHub Actions"
20
+ git config --global user.email "actions@github.com"
21
+
22
+ - name: Push to Hugging Face Spaces
23
  env:
24
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
 
 
25
  run: |
26
+ # Fail immediately on any error
27
  set -euo pipefail
28
 
29
+ # Validate token exists
30
+ if [ -z "$HF_TOKEN" ]; then
31
+ echo "❌ Error: HF_TOKEN secret is not set!"
32
+ exit 1
33
+ fi
34
+
35
+ # Remove any trailing whitespace from token
36
+ HF_TOKEN="$(echo "$HF_TOKEN" | xargs)"
37
+
38
+ # Add Hugging Face remote with embedded token (URL encoded)
39
+ HF_REMOTE="https://huggingface_user:${HF_TOKEN}@huggingface.co/spaces/xplorers/MIS_API"
40
+ git remote add hf "$HF_REMOTE" || git remote set-url hf "$HF_REMOTE"
41
 
42
+ # Push to Hugging Face main branch
43
+ echo "🚀 Pushing to Hugging Face Spaces..."
44
+ git push -u hf main --force
45
 
46
+ echo "✅ Successfully deployed to Hugging Face Spaces!"
 
 
main.py CHANGED
@@ -1,4 +1,5 @@
1
  from fastapi import FastAPI, UploadFile, File, HTTPException
 
2
  from services import predictor
3
  from contextlib import asynccontextmanager
4
 
@@ -32,6 +33,15 @@ app = FastAPI(
32
  lifespan=lifespan
33
  )
34
 
 
 
 
 
 
 
 
 
 
35
  @app.get("/")
36
  async def root():
37
  return {
 
1
  from fastapi import FastAPI, UploadFile, File, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
  from services import predictor
4
  from contextlib import asynccontextmanager
5
 
 
33
  lifespan=lifespan
34
  )
35
 
36
+ # Configure CORS
37
+ app.add_middleware(
38
+ CORSMiddleware,
39
+ allow_origins=["*"], # Allow all origins
40
+ allow_credentials=True,
41
+ allow_methods=["*"], # Allow all HTTP methods (GET, POST, etc.)
42
+ allow_headers=["*"], # Allow all headers
43
+ )
44
+
45
  @app.get("/")
46
  async def root():
47
  return {