from fastapi import FastAPI, HTTPException from transformers import pipeline app=FastAPI() pipe = pipeline("text2text-generation", model="google/flan-t5-small") @app.get("/") def welcome(): """ Welcoming page. """ return {"message": "Welcome to the Text-to-Text Generation API! Use the /generate endpoint to transform text."} @app.post("/generate") def generate_text(data: str): """ Generate text from the input text using the text-to-text generation model. """ try: # Perform text-to-text generation generated = generate_text(data.str, max_length=50, num_return_sequences=1) return {"input": data.str, "generated_text": generated[0]["generated_text"]} except Exception as e: raise HTTPException(status_code=500, detail=str(e))