Spaces:
Sleeping
Sleeping
Refactor API structure by consolidating model, prediction, and scraping functionalities into utility modules; update requirements.txt to streamline dependencies; enhance prediction endpoint to utilize new utility functions for improved maintainability and clarity.
5bbd2ed Download api.py from jawwaadsabree/CurryModel: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/jawwaadsabree/CurryModel/resolve/main/api.py
- Command line
-
hf download hf://spaces/jawwaadsabree/CurryModel/api.py
-
curl -L -o api.py https://huggingface.co/spaces/jawwaadsabree/CurryModel/resolve/main/api.py
1.52 kB
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| from huggingface_hub import login | |
| import os | |
| from dotenv import load_dotenv | |
| from utils import ( | |
| get_data, | |
| format_data, | |
| save_data, | |
| make_prediction | |
| ) | |
| # Initialize FastAPI | |
| app = FastAPI() | |
| # Load environment variables | |
| load_dotenv() | |
| class ScrapeRequest(BaseModel): | |
| start_year: int | |
| end_year: int | |
| async def scrape(request: ScrapeRequest): | |
| try: | |
| # Login to Hugging Face | |
| login(token=os.getenv("HF_TOKEN")) | |
| results = [] | |
| for year in range(request.start_year, request.end_year + 1): | |
| try: | |
| stats = get_data(year) | |
| formatted_stats = format_data(stats, year) | |
| await save_data(formatted_stats, year, predict_callback=make_prediction) | |
| results.append({ | |
| "year": year, | |
| "status": "success", | |
| "message": f"Data for year {year-1}-{year} saved successfully" | |
| }) | |
| except Exception as e: | |
| results.append({ | |
| "year": year, | |
| "status": "error", | |
| "message": str(e) | |
| }) | |
| return {"results": results} | |
| except Exception as e: | |
| return {"error": str(e)} | |
| async def predict(): | |
| return await make_prediction() | |
| async def root(): | |
| return {"message": "Welcome to the Curry Model API!"} |