Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Request | |
from fastapi.responses import HTMLResponse | |
from fastapi.staticfiles import StaticFiles | |
from fastapi.middleware.cors import CORSMiddleware | |
from fastapi.security import HTTPBasic, HTTPBasicCredentials | |
from fastapi import Depends, HTTPException, status | |
import gradio as gr | |
from app import setup_demo | |
import os | |
import secrets | |
app = FastAPI() | |
security = HTTPBasic() | |
# 인증 설정 | |
def get_current_username(credentials: HTTPBasicCredentials = Depends(security)): | |
correct_username = "daekyo" | |
correct_password = os.environ.get("DEFAULT_PW") | |
is_correct_username = secrets.compare_digest(credentials.username, correct_username) | |
is_correct_password = secrets.compare_digest(credentials.password, correct_password) | |
if not (is_correct_username and is_correct_password): | |
raise HTTPException( | |
status_code=status.HTTP_401_UNAUTHORIZED, | |
detail="Incorrect username or password", | |
headers={"WWW-Authenticate": "Basic"}, | |
) | |
return credentials.username | |
# CORS 설정 | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
# 정적 파일 서빙 설정 | |
app.mount("/static", StaticFiles(directory="static"), name="static") | |
# Gradio 인터페이스 생성 | |
demo = setup_demo() | |
app = gr.mount_gradio_app( | |
app, demo, path="/", auth=("daekyo", os.environ.get("DEFAULT_PW")) | |
) | |
# 메인 라우트 | |
async def root(request: Request, username: str = Depends(get_current_username)): | |
return await demo.render_template(request) | |
if __name__ == "__main__": | |
print("To run the server, use:") | |
print("uvicorn main:app --host 0.0.0.0 --port 8000 --reload") | |