File size: 1,568 Bytes
4786c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from typing import Annotated
import uvicorn
from fastapi import FastAPI, UploadFile, File 
from typing import Union 
import json 
import csv


app = FastAPI()

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


@app.on_event("startup")
async def startup_event():
    print("start")


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


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile, texte: str, model: str):

    return {"model": model, "texte": texte, "filename": file.filename}


@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}


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"