File size: 3,673 Bytes
f1f38a1
4786c09
f1f38a1
4786c09
 
 
00837ec
f1f38a1
a7d8ebb
00837ec
4786c09
 
37e2495
4786c09
 
 
 
 
 
 
 
 
 
 
 
00837ec
 
 
4786c09
 
 
 
 
 
 
a7d8ebb
 
 
 
4786c09
a7d8ebb
 
 
4786c09
 
 
 
 
 
 
 
ac14130
4786c09
 
 
 
c37d535
 
 
 
 
 
 
00837ec
c37d535
 
 
 
 
 
 
e957e83
 
 
 
b4d0d8e
e957e83
 
00837ec
e957e83
 
 
 
 
 
 
c37d535
 
 
 
 
 
 
00837ec
c37d535
 
 
 
 
 
4786c09
 
 
 
 
 
 
 
 
 
 
 
 
 
04fbbfa
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
111
112
113
114
115
116
from fastapi import FastAPI, HTTPException, UploadFile
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



app = FastAPI()

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



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], texte: str, model: str):
    res = []
    for file in files:
        fileToText = await file_to_text(file)

        res.append({"model": model, "texte": texte, "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}

# # Modèle Pydantic pour les requêtes SqueezeBERT
# class SqueezeBERTRequest(BaseModel):
#     context: str
#     question: str
@app.post("/squeezebert/")
async def qasqueezebert(context: str, question: str):
    try:
        squeezebert_answer = squeezebert(context, 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)}")

# # Modèle Pydantic pour les requêtes BERT
# class BERTRequest(BaseModel):
#     context: str
#     question: str
@app.post("/bert/")
async def qabert(context: str, question: str):
    try:
        bert_answer = bert(context, 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)}")

# # Modèle Pydantic pour les requêtes DeBERTa
# class DeBERTaRequest(BaseModel):
#     context: str
#     question: str
@app.post("/deberta-v2/")
async def qadeberta(context: str, question: str):
    try:
        deberta_answer = deberta(context, 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"