Spaces:
Runtime error
Runtime error
| from fastapi import FastAPI, Request, HTTPException | |
| from fastapi.responses import JSONResponse | |
| from transformers import pipeline | |
| app = FastAPI() | |
| # Initialize the text generation pipeline with trust_remote_code=True | |
| pipe = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True) | |
| 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." | |
| } | |
| def generate(payload: dict): | |
| text = payload.get("text", "") | |
| if not text: | |
| raise HTTPException(status_code=400, detail="Text input is required.") | |
| try: | |
| output = pipe(text) | |
| return {"output": output[0]["generated_text"]} | |
| except Exception as e: | |
| return JSONResponse( | |
| status_code=500, | |
| content={"error": f"An error occurred: {str(e)}"} | |
| ) | |