File size: 3,824 Bytes
e0724f2
4786c09
f1f38a1
4786c09
 
 
3104437
f1f38a1
a7d8ebb
00837ec
e0724f2
3104437
37e2495
4786c09
 
3104437
 
 
 
 
 
4786c09
 
 
 
 
 
 
 
3104437
 
 
4786c09
 
 
 
 
3104437
 
 
 
 
 
 
 
 
 
 
 
 
 
4786c09
 
3104437
 
a7d8ebb
3104437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4786c09
c37d535
3104437
c37d535
3104437
c37d535
 
 
 
 
 
 
b4d0d8e
3104437
e957e83
e0724f2
e957e83
 
 
 
 
 
 
3104437
 
c37d535
e0724f2
c37d535
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
from uploadFile import file_to_text
from typing import List
from transformers import pipeline
from pydantic import BaseModel
from typing import Optional

app = FastAPI()

class Request(BaseModel):
    context: str
    question: str
    model: Optional[str] = None
    # files: Optional[List[UploadFile]] = None

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

pipSqueezeBert = pipeline("question-answering", model="ALOQAS/squeezebert-uncased-finetuned-squad-v2")
pipBert = pipeline('question-answering', model="ALOQAS/bert-large-uncased-finetuned-squad-v2")
pipDeberta = pipeline('question-answering', model="ALOQAS/deberta-large-finetuned-squad-v2")

@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.post("/contextText/")
async def create_upload_file(request: Request):
    try:
        if request.model == "squeezebert":
            answer = squeezebert(request.context, request.question, pipSqueezeBert)
        elif request.model == "bert":
            answer = bert(request.context, request.question, pipBert)
        elif request.model == "deberta":
            answer = deberta(request.context, request.question, pipDeberta)
        else:
            raise HTTPException(status_code=400, detail="Model not found.")
        return answer
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")

@app.post("/uploadfile/")
async def create_upload_file(files: List[UploadFile] = File(...), question: str = Form(...), model: str = Form(...)):
    res = ""
    for file in files:
        try:
            res += await file_to_text(file)
        except Exception as e:
            print(f"Failed to process file {file.filename}: {e}") 
            continue 

    if res == "":
        raise HTTPException(status_code=400, detail="All files failed to process.")
    
    answer = None
    if model == "squeezebert":
        answer = squeezebert(res, question, pipSqueezeBert)
    elif model == "bert":
        answer = bert(res, question, pipBert)
    elif model == "deberta":
        answer = deberta(res, question, pipDeberta)
    else:
        raise HTTPException(status_code=400, detail="Model not found.")
    
    return answer

@app.post("/squeezebert/")
async def qasqueezebert(request: Request):
    try:
        squeezebert_answer = squeezebert(request.context, request.question, pipSqueezeBert)
        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: Request):
    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/")
async def qadeberta(request: Request):
    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)}")