Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, UploadFile, File, HTTPException | |
| from api.services import ocr_service, document_service, whisper_service, llm_service | |
| router = APIRouter() | |
| 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 "", | |
| } | |
| 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 "", | |
| } | |
| 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, | |
| } | |