Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py (Hugging Face Space用)
|
| 2 |
+
import uvicorn
|
| 3 |
+
from fastapi import FastAPI, Depends, HTTPException, status
|
| 4 |
+
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
| 5 |
+
from fastapi.responses import FileResponse
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
security = HTTPBasic()
|
| 9 |
+
|
| 10 |
+
# ユーザー名とパスワードの設定
|
| 11 |
+
def authenticate(credentials: HTTPBasicCredentials = Depends(security)):
|
| 12 |
+
correct_username = "admin"
|
| 13 |
+
correct_password = "password123"
|
| 14 |
+
|
| 15 |
+
# ユーザー名とパスワードが一致するか確認
|
| 16 |
+
if credentials.username != correct_username or credentials.password != correct_password:
|
| 17 |
+
raise HTTPException(
|
| 18 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 19 |
+
detail="Incorrect email or password",
|
| 20 |
+
headers={"WWW-Authenticate": "Basic"},
|
| 21 |
+
)
|
| 22 |
+
return credentials.username
|
| 23 |
+
|
| 24 |
+
@app.get("/")
|
| 25 |
+
async def read_index(username: str = Depends(authenticate)):
|
| 26 |
+
# 認証に成功したらHTMLファイルを返す
|
| 27 |
+
return FileResponse('index.html')
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|