Spaces:
Sleeping
Sleeping
LucasAguetai
commited on
Commit
·
4786c09
1
Parent(s):
050ca6f
first push
Browse files- Dockerfile +13 -0
- main.py +63 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.12.2
|
2 |
+
|
3 |
+
# Copy the current directory contents into the container at .
|
4 |
+
COPY . .
|
5 |
+
|
6 |
+
# Set the working directory to /
|
7 |
+
WORKDIR /
|
8 |
+
|
9 |
+
# Install requirements.txt
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
|
11 |
+
|
12 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, File, UploadFile
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from typing import Annotated
|
4 |
+
import uvicorn
|
5 |
+
from fastapi import FastAPI, UploadFile, File
|
6 |
+
from typing import Union
|
7 |
+
import json
|
8 |
+
import csv
|
9 |
+
|
10 |
+
|
11 |
+
app = FastAPI()
|
12 |
+
|
13 |
+
app.add_middleware(
|
14 |
+
CORSMiddleware,
|
15 |
+
allow_origins=["*"],
|
16 |
+
allow_credentials=True,
|
17 |
+
allow_methods=["*"],
|
18 |
+
allow_headers=["*"],
|
19 |
+
)
|
20 |
+
|
21 |
+
|
22 |
+
@app.on_event("startup")
|
23 |
+
async def startup_event():
|
24 |
+
print("start")
|
25 |
+
|
26 |
+
|
27 |
+
@app.get("/")
|
28 |
+
async def root():
|
29 |
+
return {"message": "Hello World"}
|
30 |
+
|
31 |
+
|
32 |
+
@app.post("/uploadfile/")
|
33 |
+
async def create_upload_file(file: UploadFile, texte: str, model: str):
|
34 |
+
|
35 |
+
return {"model": model, "texte": texte, "filename": file.filename}
|
36 |
+
|
37 |
+
|
38 |
+
@app.post("/contextText/")
|
39 |
+
async def create_upload_file(context: str, texte: str, model: str):
|
40 |
+
|
41 |
+
return {"model": model, "texte": texte, "context": context}
|
42 |
+
|
43 |
+
|
44 |
+
@app.post("/withoutFile/")
|
45 |
+
async def create_upload_file(texte: str, model: str):
|
46 |
+
|
47 |
+
return {"model": model, "texte": texte}
|
48 |
+
|
49 |
+
|
50 |
+
def extract_data(file: UploadFile) -> Union[str, dict, list]:
|
51 |
+
if file.filename.endswith(".txt"):
|
52 |
+
data = file.file.read()
|
53 |
+
return data.decode("utf-8")
|
54 |
+
elif file.filename.endswith(".csv"):
|
55 |
+
data = file.file.read().decode("utf-8")
|
56 |
+
rows = data.split("\n")
|
57 |
+
reader = csv.DictReader(rows)
|
58 |
+
return [dict(row) for row in reader]
|
59 |
+
elif file.filename.endswith(".json"):
|
60 |
+
data = file.file.read().decode("utf-8")
|
61 |
+
return json.loads(data)
|
62 |
+
else:
|
63 |
+
return "Invalid file format"
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
requests
|
4 |
+
Pillow
|
5 |
+
typing
|