CoolFace
Apppublic

Studyard/Question2WrongAnswer

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py49 linesDownload Raw Back to root
1from fastapi import FastAPI2from transformers import pipeline3from fastapi.middleware.cors import CORSMiddleware4 5# Create a new FastAPI app instance6app = FastAPI()7origins = ["*"]8app.add_middleware(9    CORSMiddleware,10    allow_origins=origins,11    allow_credentials=True,12    allow_methods=["*"],13    allow_headers=["*"],14) 15# Initialize the text generation pipeline16# This function will be able to generate text17# given an input.18pipe = pipeline("text2text-generation", 19model="Quizzer/Question2WrongAnswer")20 21@app.get("/")22def read_root():23    return {"Hello": "World!"}24 25# Define a function to handle the GET request at `/generate`26# The generate() function is defined as a FastAPI route that takes a 27# string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response 28# containing the generated text under the key "output"29@app.get("/generate")30def generate(text: str):31    """32    Using the text2text-generation pipeline from `transformers`, generate text33    from the given input text. The model used is `google/flan-t5-small`, which34    can be found [here](<https://huggingface.co/google/flan-t5-small>).35    """36    # Use the pipeline to generate text from the given input text37    output = pipe(text)38     39    # Return the generated text in a JSON response40    return {"output": output[0]["generated_text"]}41    42@app.get("/generates")43def generate(topic: str,question:str,context:str,n: int):44    text = "Tópico: {} Questão: {} Context: {}".format(topic,question,context)45    output = pipe(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