Nick Starkov commited on
Commit
059dae2
·
1 Parent(s): 83e2fd4

Files Added

Browse files
Files changed (4) hide show
  1. Dockerfile.txt +25 -0
  2. README.md +37 -1
  3. app.py +18 -0
  4. requirements.txt +4 -0
Dockerfile.txt ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python slim image for a lightweight base
2
+ FROM python:3.10-slim
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Install system dependencies
8
+ RUN apt-get update && apt-get install -y \
9
+ gcc \
10
+ && rm -rf /var/lib/apt/lists/*
11
+
12
+ # Copy requirements file
13
+ COPY requirements.txt .
14
+
15
+ # Install Python dependencies
16
+ RUN pip install --no-cache-dir -r requirements.txt
17
+
18
+ # Copy application code
19
+ COPY app.py .
20
+
21
+ # Expose port 7860 (default for Hugging Face Spaces)
22
+ EXPOSE 7860
23
+
24
+ # Command to run the FastAPI app with uvicorn
25
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -8,4 +8,40 @@ pinned: false
8
  short_description: Quotes API Space
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  short_description: Quotes API Space
9
  ---
10
 
11
+ # Quotes API Space
12
+
13
+ This Hugging Face Space provides an API to fetch 50 random quotes from the [jstet/quotes-500k](https://huggingface.co/datasets/jstet/quotes-500k) dataset.
14
+
15
+ ## Usage
16
+
17
+ Send a GET request to `/api/quotes` to retrieve 50 random quotes. Example response:
18
+
19
+ ```json
20
+ {
21
+ "quotes": [
22
+ {"quote": "Life is what happens when you're busy making other plans.", "author": "John Lennon", "category": "Life"},
23
+ ...
24
+ ]
25
+ }
26
+ ```
27
+
28
+ ## Setup
29
+
30
+ This Space uses the Docker Space SDK with Python, FastAPI, and the Hugging Face `datasets` library. The configuration is defined in the following files:
31
+
32
+ - `Dockerfile`: Defines the Docker image.
33
+ - `requirements.txt`: Lists Python dependencies.
34
+ - `app.py`: Contains the FastAPI application code.
35
+
36
+ ## Deployment
37
+
38
+ To deploy this Space on Hugging Face:
39
+
40
+ 1. Create a new Space on Hugging Face, selecting the Docker template.
41
+ 2. Upload the `Dockerfile`, `requirements.txt`, `app.py`, and `README.md` files to the Space repository.
42
+ 3. Configure the Space to use the Docker Space SDK.
43
+ 4. Build and deploy the Space.
44
+
45
+ The Space will be available at `https://<your-username>-<space-name>.hf.space/api/quotes`.
46
+
47
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from datasets import load_dataset
3
+ import pandas as pd
4
+ import random
5
+
6
+ app = FastAPI()
7
+
8
+ # Load the dataset once at startup
9
+ dataset = load_dataset("jstet/quotes-500k")
10
+ quotes_df = dataset["train"].to_pandas()
11
+
12
+ @app.get("/api/quotes")
13
+ async def get_random_quotes():
14
+ # Sample 50 random quotes
15
+ random_quotes = quotes_df.sample(n=50, random_state=random.randint(1, 100000))
16
+ # Convert to list of dictionaries for JSON response
17
+ quotes_list = random_quotes[["quote", "author", "category"]].to_dict(orient="records")
18
+ return {"quotes": quotes_list}
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi==0.115.0
2
+ uvicorn==0.32.0
3
+ datasets==3.0.1
4
+ pandas==2.2.3