Download app.py from 1MR/Testing: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/1MR/Testing/resolve/main/app.py
- Command line
-
hf download hf://spaces/1MR/Testing/app.py
-
curl -L -o app.py https://huggingface.co/spaces/1MR/Testing/resolve/main/app.py
1.05 kB
| from fastapi import FastAPI | |
| from fastapi.responses import JSONResponse | |
| from transformers import pipeline | |
| from fastapi.requests import Request | |
| from pydantic import BaseModel | |
| app = FastAPI() | |
| # Initialize the text generation pipeline | |
| pipe = pipeline("text2text-generation", model="google/flan-t5-small") | |
| async def log_requests(request: Request, call_next): | |
| print(f"Incoming request: {request.method} {request.url}") | |
| response = await call_next(request) | |
| return response | |
| def read_root(): | |
| return { | |
| "message": "Welcome to the FastAPI text generation API! Use the /generate endpoint with a 'text' query parameter." | |
| } | |
| class GenerateRequest(BaseModel): | |
| text: str | |
| def generate(request: GenerateRequest): | |
| try: | |
| output = pipe(request.text) | |
| return {"output": output[0]["generated_text"]} | |
| except Exception as e: | |
| return JSONResponse( | |
| status_code=500, | |
| content={"error": f"An error occurred: {str(e)}"} | |
| ) | |