Download api.py from SfaxWare/README: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/spaces/SfaxWare/README/resolve/main/api.py
- Command line
-
hf download hf://spaces/SfaxWare/README/api.py
-
curl -L -o api.py https://huggingface.co/spaces/SfaxWare/README/resolve/main/api.py
2.14 kB
| #!/usr/bin/env python3 | |
| # vim: set fileencoding=utf-8 | |
| from fastapi import FastAPI | |
| from fastapi.middleware import Middleware | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import HTMLResponse | |
| from logging import getLogger | |
| from os import environ | |
| from os.path import realpath, sep, splitext | |
| from sfxpkg import rootDir | |
| from sfxpkg.version import API_VERSION | |
| # Create logger for this module | |
| log = getLogger(__name__) | |
| # Instantiate REST application with some basic configuration | |
| app = FastAPI( | |
| title='Welcome to SfaxWare space at Hugging Face', | |
| version=API_VERSION, | |
| #description=__doc__, | |
| openapi_url=None, | |
| docs_url=None, | |
| redoc_url=None, | |
| middleware=[ | |
| # CORS origins accepted patterns | |
| Middleware( | |
| CORSMiddleware, | |
| allow_origin_regex=r'https://.+\.(q-ai\.org|q-ai\.fr|sfaxware\.com)(:(443|4200))?', | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| allow_credentials=True, | |
| ) | |
| ] | |
| ) | |
| async def root(): | |
| response = f''' | |
| <html> | |
| <head> | |
| <title>Some HTML in here</title> | |
| </head> | |
| <body> | |
| <h1>Welcome to SfaxWare</h1> | |
| Current version is {API_VERSION} | |
| </body> | |
| </html> | |
| ''' | |
| return response | |
| # Run application using uvicorn server if started as a python executable script | |
| if __name__ == '__main__': | |
| from uvicorn import main | |
| from uvicorn.config import LOGGING_CONFIG | |
| # Log configuration | |
| LOGGING_CONFIG['formatters']['access']['fmt'] = '%(asctime)s %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s' | |
| LOGGING_CONFIG['formatters']['default']['fmt'] = '%(asctime)s %(levelprefix)s %(name)s | %(message)s' | |
| if environ.get('LOG_PROFILE', 'prod') == 'prod': | |
| LOGGING_CONFIG['handlers']['default'] = { | |
| 'formatter': 'default', | |
| 'class': 'logging.handlers.TimedRotatingFileHandler', | |
| 'filename': '/tmp/fastapi-readme.log', | |
| 'when': 'midnight', | |
| 'utc': True, | |
| } | |
| LOGGING_CONFIG['loggers']['sfxpkg'] = { | |
| 'handlers': ['default'], | |
| 'level': 'INFO', | |
| 'propagate': True | |
| } | |
| pkgPath = splitext(realpath(__file__))[0] | |
| pkgName = pkgPath.replace(rootDir, 'sfxpkg').replace(sep, '.') | |
| main.params[0].default = pkgName + ':app' | |
| main() | |