File size: 780 Bytes
3c82b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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']}