LucasAguetai
fix post url
e0724f2
raw
history blame
3.61 kB
from fastapi import FastAPI, HTTPException, UploadFile, File, Form, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI, UploadFile
from typing import Union
import json
import csv
from modeles import bert, squeezebert, deberta, loadSqueeze
from uploadFile import file_to_text
from typing import List
from transformers import pipeline
from pydantic import BaseModel
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class SqueezeBERTRequest(BaseModel):
context: str
question: str
class BERTRequest(BaseModel):
context: str
question: str
class DeBERTaRequest(BaseModel):
context: str
question: str
pipBert = pipeline('question-answering', model="ALOQAS/bert-large-uncased-finetuned-squad-v2", tokenizer="ALOQAS/bert-large-uncased-finetuned-squad-v2")
pipDeberta = pipeline('question-answering', model="ALOQAS/deberta-large-finetuned-squad-v2", tokenizer="ALOQAS/deberta-large-finetuned-squad-v2")
tokenizer, model = loadSqueeze()
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.post("/uploadfile/")
async def create_upload_file(files: List[UploadFile], question: str, model: str):
res = []
for file in files:
fileToText = await file_to_text(file)
res.append({"model": model, "texte": question, "filename": file.filename, "file_to_text": fileToText})
return res
@app.post("/contextText/")
async def create_upload_file(context: str, texte: str, model: str):
return {"model": model, "texte": texte, "context": context}
@app.post("/withoutFile/")
async def create_upload_file(texte: str, model: str):
return {"model": model, "texte": texte}
@app.post("/squeezebert/")
async def qasqueezebert(request: SqueezeBERTRequest):
try:
squeezebert_answer = squeezebert(request.context, request.question, model, tokenizer)
if squeezebert_answer:
return squeezebert_answer
else:
raise HTTPException(status_code=404, detail="No answer found")
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@app.post("/bert/")
async def qabert(request: BERTRequest):
try:
bert_answer = bert(request.context, request.question, pipBert)
if bert_answer:
return bert_answer
else:
raise HTTPException(status_code=404, detail="No answer found")
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@app.post("/deberta-v2/")
async def qadeberta(request: DeBERTaRequest):
try:
deberta_answer = deberta(request.context, request.question, pipDeberta)
if deberta_answer:
return deberta_answer
else:
raise HTTPException(status_code=404, detail="No answer found")
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
def extract_data(file: UploadFile) -> Union[str, dict, list]:
if file.filename.endswith(".txt"):
data = file.file.read()
return data.decode("utf-8")
elif file.filename.endswith(".csv"):
data = file.file.read().decode("utf-8")
rows = data.split("\n")
reader = csv.DictReader(rows)
return [dict(row) for row in reader]
elif file.filename.endswith(".json"):
data = file.file.read().decode("utf-8")
return json.loads(data)
else:
return "Invalid file format"