Spaces:
Paused
Paused
Download main.py from ITZane/robot-talks: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/ITZane/robot-talks/resolve/main/main.py
- Command line
-
hf download hf://spaces/ITZane/robot-talks/main.py
-
curl -L -o main.py https://huggingface.co/spaces/ITZane/robot-talks/resolve/main/main.py
1.37 kB
| from fastapi import FastAPI, Request | |
| from fastapi.responses import HTMLResponse | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.templating import Jinja2Templates | |
| from pydantic import BaseModel | |
| from typing import List | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import os | |
| import uvicorn | |
| app = FastAPI() | |
| # CORS Middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Mount static files | |
| app.mount("/static", StaticFiles(directory="static"), name="static") | |
| # Setup templates | |
| templates = Jinja2Templates(directory="templates") | |
| # In-memory message store | |
| messages = [] | |
| class Message(BaseModel): | |
| user: str | |
| message: str | |
| async def read_root(request: Request): | |
| return templates.TemplateResponse("index.html", {"request": request}) | |
| async def get_messages(): | |
| return messages | |
| async def create_message(message: Message): | |
| messages.append(message) | |
| return {"status": "success"} | |
| async def delete_messages(): | |
| messages.clear() | |
| return {"status": "success"} | |
| if __name__ == "__main__": | |
| port = int(os.environ.get("PORT", 8000)) | |
| uvicorn.run(app, host="0.0.0.0", port=port) |