Team Ai
Apppublic

Studyard/Context2Question

sourceHugging Faceotherupdated 3y agoView on Hugging Face
0likes
app.py49 linesDownload Raw Back to root
1from fastapi import FastAPI2from fastapi.middleware.cors import CORSMiddleware3 4from transformers import pipeline5import os6# Create a new FastAPI app instance7app = FastAPI()8origins = ["*"]9app.add_middleware(10    CORSMiddleware,11    allow_origins=origins,12    allow_credentials=True,13    allow_methods=["*"],14    allow_headers=["*"],15)16# Initialize the text generation pipeline17# This function will be able to generate text18# given an input.19auth_token = os.environ.get("AUTH_TOKEN")20pipe = pipeline("text2text-generation", 21model="Quizzer/Context2Question",use_auth_token=auth_token)22 23# Define a function to handle the GET request at `/generate`24# The generate() function is defined as a FastAPI route that takes a 25# string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response 26# containing the generated text under the key "output"27@app.get("/")28def read_root():29    return {"Hello": "World!"}30@app.get("/generate")31def generate(text: str):32    """33    Using the text2text-generation pipeline from `transformers`, generate text34    from the given input text. The model used is `google/flan-t5-small`, which35    can be found [here](<https://huggingface.co/google/flan-t5-small>).36    """37    # Use the pipeline to generate text from the given input text38    output = pipe("contexto: "+text)39     40    # Return the generated text in a JSON response41    return {"output": output[0]["generated_text"]}42 43@app.get("/generateQuestion")44def generate(text: str,n: int):45    output = pipe("contexto: "+text,num_return_sequences=n,num_beams=n)46     47    # Return the generated text in a JSON response48    return {"output": [output[i]["generated_text"] for i in range(len(output))]}49