File size: 700 Bytes
5d5094e
059dae2
 
 
 
 
5d5094e
 
 
059dae2
 
 
a6ca712
059dae2
 
 
 
 
 
 
f71032d
059dae2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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}