Spaces:
Sleeping
Sleeping
Download app.py from learneng-space/quotes: direct link, hf CLI and curl.
- Browser
- Download file 700 Bytes
-
https://huggingface.co/spaces/learneng-space/quotes/resolve/main/app.py
- Command line
-
hf download hf://spaces/learneng-space/quotes/app.py
-
curl -L -o app.py https://huggingface.co/spaces/learneng-space/quotes/resolve/main/app.py
700 Bytes
| import os | |
| from fastapi import FastAPI | |
| from datasets import load_dataset | |
| import pandas as pd | |
| import random | |
| # Set Hugging Face cache directory programmatically as a fallback | |
| os.environ["HF_HOME"] = "/app/cache" | |
| app = FastAPI() | |
| # Load the dataset once at startup | |
| dataset = load_dataset("m-ric/english_historical_quotes") | |
| quotes_df = dataset["train"].to_pandas() | |
| async def get_random_quotes(): | |
| # Sample 50 random quotes | |
| random_quotes = quotes_df.sample(n=50, random_state=random.randint(1, 100000)) | |
| # Convert to list of dictionaries for JSON response | |
| quotes_list = random_quotes[["quote", "author"]].to_dict(orient="records") | |
| return {"quotes": quotes_list} |