HariLogicgo commited on
Commit
500d1fa
Β·
1 Parent(s): 22e382d

fastapi added

Browse files
Files changed (4) hide show
  1. Dockerfile +23 -0
  2. README.md +3 -6
  3. app.py +28 -1
  4. requirements.txt +2 -1
Dockerfile ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # System deps for OpenCV and others
4
+ RUN apt-get update && apt-get install -y --no-install-recommends \
5
+ ffmpeg libsm6 libxext6 git \
6
+ && rm -rf /var/lib/apt/lists/*
7
+
8
+ ENV PYTHONDONTWRITEBYTECODE=1 \
9
+ PYTHONUNBUFFERED=1 \
10
+ OMP_NUM_THREADS=4
11
+
12
+ WORKDIR /app
13
+
14
+ COPY requirements.txt /app/requirements.txt
15
+ RUN pip install --no-cache-dir -r /app/requirements.txt
16
+
17
+ COPY . /app
18
+
19
+ EXPOSE 7860
20
+
21
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
22
+
23
+
README.md CHANGED
@@ -1,12 +1,9 @@
1
  ---
2
- title: Face Upscale
3
  emoji: πŸš€
4
  colorFrom: purple
5
  colorTo: purple
6
- sdk: gradio
7
- sdk_version: 5.45.0
8
- app_file: app.py
9
  pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Face Upscale API
3
  emoji: πŸš€
4
  colorFrom: purple
5
  colorTo: purple
6
+ sdk: docker
7
+ app_port: 7860
 
8
  pinned: false
9
  ---
 
 
app.py CHANGED
@@ -25,6 +25,7 @@ os.makedirs('input', exist_ok=True)
25
  from fastapi import FastAPI, UploadFile, File, Form, Depends, HTTPException, status
26
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
27
  from fastapi.responses import FileResponse, JSONResponse
 
28
 
29
  # Mongo imports (optional)
30
  _mongo_client = None
@@ -843,6 +844,13 @@ if __name__ == "__main__":
843
  os.makedirs('input', exist_ok=True)
844
 
845
  fastapi_app = FastAPI(title="Face Upscale API")
 
 
 
 
 
 
 
846
 
847
  _bearer_scheme = HTTPBearer(auto_error=False)
848
  _api_bearer_token = os.getenv("API_BEARER_TOKEN", "changeme")
@@ -976,5 +984,24 @@ def download(filename: str, _: bool = Depends(_verify_bearer_token)):
976
  return FileResponse(p, filename=safe_name)
977
  raise HTTPException(status_code=404, detail="File not found")
978
 
979
- # Expose FastAPI app for Uvicorn/HF Spaces
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980
  app = fastapi_app
 
25
  from fastapi import FastAPI, UploadFile, File, Form, Depends, HTTPException, status
26
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
27
  from fastapi.responses import FileResponse, JSONResponse
28
+ from fastapi.middleware.cors import CORSMiddleware
29
 
30
  # Mongo imports (optional)
31
  _mongo_client = None
 
844
  os.makedirs('input', exist_ok=True)
845
 
846
  fastapi_app = FastAPI(title="Face Upscale API")
847
+ fastapi_app.add_middleware(
848
+ CORSMiddleware,
849
+ allow_origins=["*"],
850
+ allow_credentials=True,
851
+ allow_methods=["*"],
852
+ allow_headers=["*"],
853
+ )
854
 
855
  _bearer_scheme = HTTPBearer(auto_error=False)
856
  _api_bearer_token = os.getenv("API_BEARER_TOKEN", "changeme")
 
984
  return FileResponse(p, filename=safe_name)
985
  raise HTTPException(status_code=404, detail="File not found")
986
 
987
+ # Optional: mount Gradio UI if enabled, otherwise run API-only
988
+ ENABLE_GRADIO_UI = os.getenv("ENABLE_GRADIO_UI", "0") == "1"
989
+ if ENABLE_GRADIO_UI:
990
+ try:
991
+ import gradio as gr
992
+ # Minimal read-only UI showing models list
993
+ with gr.Blocks(title="Face Upscale API") as demo:
994
+ gr.Markdown("# Face Upscale API\nThis Space exposes FastAPI endpoints. UI is optional.")
995
+ try:
996
+ from fastapi.middleware.wsgi import WSGIMiddleware
997
+ # Mount at /gradio for compatibility; Hugging Face can still hit API at root
998
+ from gradio.routes import mount_gradio_app
999
+ fastapi_app = mount_gradio_app(fastapi_app, demo, path="/gradio")
1000
+ except Exception:
1001
+ # Fallback mounting
1002
+ fastapi_app.mount("/gradio", WSGIMiddleware(demo.server_app))
1003
+ except Exception:
1004
+ pass
1005
+
1006
+ # Expose FastAPI app for Uvicorn/HF Spaces (root ASGI)
1007
  app = fastapi_app
requirements.txt CHANGED
@@ -23,4 +23,5 @@ gdown # supports downloading the large file from Google Drive
23
  fastapi
24
  uvicorn[standard]
25
  pymongo
26
- python-multipart
 
 
23
  fastapi
24
  uvicorn[standard]
25
  pymongo
26
+ python-multipart
27
+ gradio