Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import os
|
| 4 |
+
from tempfile import NamedTemporaryFile
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from docx import Document
|
| 7 |
+
import pdfplumber
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
def extract_text_from_docx(file_path):
|
| 12 |
+
doc = Document(file_path)
|
| 13 |
+
return "\n".join([p.text for p in doc.paragraphs])
|
| 14 |
+
|
| 15 |
+
def extract_text_from_pdf(file_path):
|
| 16 |
+
text = ""
|
| 17 |
+
with pdfplumber.open(file_path) as pdf:
|
| 18 |
+
for page in pdf.pages:
|
| 19 |
+
text += page.extract_text() + "\n"
|
| 20 |
+
return text
|
| 21 |
+
|
| 22 |
+
def extract_text_from_sheet(file_path):
|
| 23 |
+
if file_path.endswith(".csv"):
|
| 24 |
+
df = pd.read_csv(file_path)
|
| 25 |
+
else:
|
| 26 |
+
df = pd.read_excel(file_path)
|
| 27 |
+
return df.to_string(index=False)
|
| 28 |
+
|
| 29 |
+
@app.post("/upload")
|
| 30 |
+
async def upload(file: UploadFile = File(...)):
|
| 31 |
+
extension = os.path.splitext(file.filename)[1].lower()
|
| 32 |
+
mime_type = file.content_type
|
| 33 |
+
|
| 34 |
+
with NamedTemporaryFile(delete=False, suffix=extension) as temp_file:
|
| 35 |
+
temp_file.write(await file.read())
|
| 36 |
+
temp_path = temp_file.name
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
if extension == ".docx":
|
| 40 |
+
texte = extract_text_from_docx(temp_path)
|
| 41 |
+
elif extension == ".pdf":
|
| 42 |
+
texte = extract_text_from_pdf(temp_path)
|
| 43 |
+
elif extension in [".csv", ".xlsx"]:
|
| 44 |
+
texte = extract_text_from_sheet(temp_path)
|
| 45 |
+
else:
|
| 46 |
+
return JSONResponse(status_code=400, content={"erreur": "Type de fichier non supporté"})
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return JSONResponse(status_code=500, content={"erreur": str(e)})
|
| 49 |
+
finally:
|
| 50 |
+
os.remove(temp_path)
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
"nom_fichier": file.filename,
|
| 54 |
+
"type": mime_type,
|
| 55 |
+
"texte": texte
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
@app.get("/")
|
| 59 |
+
def ping():
|
| 60 |
+
return {"message": "L'API fonctionne ✅"}
|