Download main.py from pvanand/doc-maker: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/pvanand/doc-maker/resolve/main/main.py
- Command line
-
hf download hf://spaces/pvanand/doc-maker/main.py
-
curl -L -o main.py https://huggingface.co/spaces/pvanand/doc-maker/resolve/main/main.py
1.39 kB
| import os | |
| import asyncio | |
| from fastapi import FastAPI, HTTPException, Security, Depends, Query | |
| from fastapi.security import APIKeyHeader | |
| from pydantic import BaseModel, Field, create_model | |
| from typing import List, Optional | |
| import json | |
| import logging | |
| from fastapi.middleware.cors import CORSMiddleware | |
| # Set up logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = FastAPI( | |
| title="FastAPI", | |
| version="0.1.0", | |
| servers=[ | |
| { | |
| "url": "https://pvanand-doc-maker.hf.space", # Your production URL | |
| "description": "Production server" | |
| } | |
| ] | |
| ) | |
| from doc_maker import router as doc_maker_router | |
| app.include_router(doc_maker_router, prefix="/api/v1") | |
| # API key configuration | |
| CHAT_AUTH_KEY = os.getenv("CHAT_AUTH_KEY") | |
| api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False) | |
| async def verify_api_key(api_key: str = Security(api_key_header)): | |
| if api_key != CHAT_AUTH_KEY: | |
| logger.warning("Invalid API key used") | |
| raise HTTPException(status_code=403, detail="Could not validate credentials") | |
| return api_key | |
| # CORS middleware setup | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["GET", "POST"], | |
| allow_headers=["*"], | |
| ) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) |