from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates from transformers import pipeline # FastAPI app instance app = FastAPI() # Initializing the text generation pipeline pipe = pipeline("text2text-generation", model="facebook/m2m100_1.2B") # Jinja2 template configuration templates = Jinja2Templates(directory="templates") @app.get("/", response_class=HTMLResponse) async def home(request: Request): return templates.TemplateResponse("index.html", {"request": request}) @app.get("/generate") def generate(text: str): # Using the pipeline to generate text from given input output = pipe(text, max_length=50) # Adjust max_length as needed return {"output": output[0]['generated_text']}