quotes / app.py
Nick Starkov
Changed dataset. Removed category
f71032d
raw
history blame contribute delete
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()
@app.get("/api/quotes")
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}