Spaces:
Running
Running
File size: 2,355 Bytes
7ddb64a | 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 65 66 67 | from fastapi import APIRouter, UploadFile, File, HTTPException
from api.services import ocr_service, document_service, whisper_service, llm_service
router = APIRouter()
@router.post("/image")
async def upload_image(file: UploadFile = File(...)):
"""Reçoit une image, vérifie le format, puis utilise OCR pour extraire le texte."""
if file.content_type and not file.content_type.startswith("image/"):
raise HTTPException(400, "Le fichier doit être une image.")
image_bytes = await file.read()
raw_text = await ocr_service.extract_text_from_image(image_bytes)
# if not raw_text:
# raise HTTPException(422, "Impossible d'extraire du texte de cette image.")
summary = await llm_service.summarize_for_medical_context(raw_text)
return {
"filename": file.filename,
"extracted_text": raw_text or "Aucun texte extrait",
"summary": summary or "",
}
@router.post("/document")
async def upload_document(file: UploadFile = File(...)):
"""
Recois un PDF, extrait le texte, puis génère un résumé adapté au contexte médical.
"""
# if file.content_type != "application/pdf":
# raise HTTPException(400, "Le fichier doit être un PDF.")
pdf_bytes = await file.read()
raw_text = document_service.extract_text_from_pdf(pdf_bytes)
if not raw_text:
raise HTTPException(422, "Impossible d'extraire le texte de ce PDF.")
summary = await llm_service.summarize_for_medical_context(raw_text)
return {
"filename": file.filename,
"extracted_text": raw_text or "Aucun texte extrait",
"summary": summary or "",
}
@router.post("/voice")
async def upload_voice(file: UploadFile = File(...)):
"""Reçoit un fichier audio, vérifie le format, puis utilise Whisper pour transcrire le contenu.
formats supportés : wav, mpeg, ogg, webm, mp4
"""
allowed = ["audio/wav", "audio/mpeg", "audio/ogg", "audio/webm", "audio/mp4"]
if file.content_type and file.content_type not in allowed:
raise HTTPException(400, f"Format audio non supporté: {file.content_type}")
audio_bytes = await file.read()
transcription = await whisper_service.transcribe(audio_bytes, file.filename or "unknown")
return {
"filename": file.filename,
"transcription": transcription,
}
|