| |
|
| | 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) |