|
import os |
|
import io |
|
from io import BytesIO |
|
from fastapi import FastAPI, UploadFile, File, Form |
|
from fastapi.responses import JSONResponse, HTMLResponse |
|
from fastapi.middleware.cors import CORSMiddleware |
|
from fastapi.staticfiles import StaticFiles |
|
from huggingface_hub import InferenceClient, login |
|
from PyPDF2 import PdfReader |
|
from docx import Document |
|
from PIL import Image |
|
from routers import ai |
|
|
|
|
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN") |
|
if not HF_TOKEN: |
|
raise RuntimeError( |
|
"Variable d'environnement HF_TOKEN absente ; crΓ©ez un jeton Β« Read Β» " |
|
"sur https://huggingface.co/settings/tokens et exportez-le (voir .env)." |
|
) |
|
|
|
login(token=HF_TOKEN) |
|
|
|
|
|
|
|
|
|
PORT = int(os.getenv("PORT", 7860)) |
|
|
|
app = FastAPI( |
|
title="AI Web App API", |
|
description="Backend API for AI-powered web application", |
|
version="1.0.0" |
|
) |
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["*"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
app.mount("/", StaticFiles(directory=".", html=True), name="static") |
|
app.include_router(ai.router) |
|
|
|
|
|
summary_client = InferenceClient("facebook/bart-large-cnn") |
|
qa_client = InferenceClient("deepset/roberta-base-squad2") |
|
image_caption_client = InferenceClient("nlpconnect/vit-gpt2-image-captioning") |
|
|
|
|
|
|
|
|
|
def extract_text_from_pdf(content: bytes) -> str: |
|
reader = PdfReader(io.BytesIO(content)) |
|
return "\n".join(p.extract_text() or "" for p in reader.pages).strip() |
|
|
|
def extract_text_from_docx(content: bytes) -> str: |
|
doc = Document(io.BytesIO(content)) |
|
return "\n".join(p.text for p in doc.paragraphs).strip() |
|
|
|
def process_uploaded_file(file: UploadFile) -> str: |
|
content = file.file.read() |
|
ext = file.filename.rsplit(".", 1)[-1].lower() |
|
if ext == "pdf": |
|
return extract_text_from_pdf(content) |
|
if ext == "docx": |
|
return extract_text_from_docx(content) |
|
if ext == "txt": |
|
return content.decode("utf-8").strip() |
|
raise ValueError("Type de fichier non supportΓ©") |
|
|
|
|
|
|
|
@app.get("/api/health") |
|
async def health_check(): |
|
return {"status": "healthy", "version": "1.0.0", "hf_token_set": True} |
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run(app, host="0.0.0.0", port=PORT) |
|
|