text_generation / main.py
git.name
updating th model and the task to question answer
bff9622
raw
history blame contribute delete
937 Bytes
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from starlette.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from transformers import pipeline
app = FastAPI()
# Initialize the pipeline
question_answerer = pipeline("question-answering", model='distilbert-base-cased-distilled-squad')
# Serve static files
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
# Serve index.html at the root
@app.get("/", response_class=HTMLResponse)
async def read_index():
return FileResponse("static/index.html")
# Endpoint for text-to-text generation
@app.get("/generate-text")
async def generate_text(question: str, context: str):
output = question_answerer(question=question, context=context)
print(output)
return {"output": f"Answer: '{output['answer']}', score: {round(output['score'], 4)}, start: {output['start']}, end: {output['end']}"}