aiWeb / main.py
benkada's picture
Update main.py
1ece3c6 verified
raw
history blame
4.01 kB
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 # conservez vos routes annexes
# ──────────────────────────────────────────────────────────────────────────────
# 1) Authentification Hugging Face
# ──────────────────────────────────────────────────────────────────────────────
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) # Authentifie tout le process
# ──────────────────────────────────────────────────────────────────────────────
# 2) Configuration FastAPI
# ──────────────────────────────────────────────────────────────────────────────
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)
# Clients HF (token passΓ© implicitement)
summary_client = InferenceClient("facebook/bart-large-cnn")
qa_client = InferenceClient("deepset/roberta-base-squad2")
image_caption_client = InferenceClient("nlpconnect/vit-gpt2-image-captioning")
# ──────────────────────────────────────────────────────────────────────────────
# 3) Utils : extraction texte, routes API (inchangΓ©s ou presque)
# ──────────────────────────────────────────────────────────────────────────────
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Γ©")
# … (gardez vos trois routes /analyze, /ask, /interpret_image identiques)
@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)