File size: 1,499 Bytes
e61842c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d0c057
e61842c
 
2d0c057
e61842c
 
2d0c057
e61842c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

import uvicorn
import pandas as pd
from fastapi import FastAPI

description = """
# Climate news generator

Generate a random news about climate change from [readerbench/fakenews-climate-fr](https://huggingface.co/datasets/readerbench/fakenews-climate-fr) dataset.

Use for testing data pipelines.

Where you can:
* `/get_news` : Get a random news

Check out documentation for more information on each endpoint. 
"""

tags_metadata = [
    {
        "name": "Generator",
        "description": "Endpoints that generate news about climate change",
    },
]

data = pd.read_csv("hf://datasets/readerbench/fakenews-climate-fr/fake-fr.csv")

app = FastAPI(
    title="API for Climate news generator",
    description=description,
    version="1.0",
    contact={
        "name": "Olivier-52",
        "url": "https://huggingface.co/Olivier-52",
    },
    openapi_tags=tags_metadata,)

@app.get("/")
def index():
    """Return a message to the user.

    This endpoint does not take any parameters and returns a message
    to the user. It is used to test the API.

    Returns:
        str: A message to the user.
    """
    return "Hello world! Go to /docs to try the API."

@app.get("/get_news", tags=["Generator"])
def get_news():
    """Return a random news about climate change."""
    if data.empty:
        return {"error": "No data available"}
    return data.sample(1).to_dict(orient="records")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="localhost", port=8000)