from fastapi import FastAPI | |
from transformers import pipeline | |
##create a new fastapi app instance | |
app=FastAPI() | |
##Initialize the text generation pipeline | |
pipe = pipeline("text2text-generation", model="google/flan-t5-small", clean_up_tokenization_spaces=True) | |
def home(): | |
return {"message": "Welcome to the Text Generation API"} | |
## Define a function to handle GET Request at '/generate' | |
def generate(text:str): | |
## use the pipeline to generate text based on the input 'text' | |
output=pipe(text) | |
## return the generated text in Json response | |
return {"output":output[0]['generated_text']} | |