Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import logging
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 3 |
from pydantic import BaseModel, HttpUrl
|
| 4 |
from typing import List, Dict
|
| 5 |
from concurrent.futures import ThreadPoolExecutor
|
|
@@ -16,20 +18,27 @@ app = FastAPI(
|
|
| 16 |
version="1.0.0"
|
| 17 |
)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
class AudioItem(BaseModel):
|
| 21 |
url: HttpUrl
|
| 22 |
user_id: str
|
| 23 |
|
| 24 |
-
|
| 25 |
class AnalysisRequest(BaseModel):
|
| 26 |
audio_items: List[AudioItem]
|
| 27 |
|
| 28 |
-
|
| 29 |
def process_item_wrapper(item: AudioItem) -> Dict:
|
| 30 |
try:
|
| 31 |
logger.info(f"Starting analysis for user '{item.user_id}' with URL: {item.url}")
|
| 32 |
result = process_interview(str(item.url))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return {
|
| 34 |
"user_id": item.user_id,
|
| 35 |
"url": str(item.url),
|
|
@@ -45,25 +54,37 @@ def process_item_wrapper(item: AudioItem) -> Dict:
|
|
| 45 |
"detail": str(e)
|
| 46 |
}
|
| 47 |
|
| 48 |
-
|
| 49 |
@app.get("/health", summary="Check API Health")
|
| 50 |
def health_check():
|
| 51 |
return {"status": "ok"}
|
| 52 |
|
| 53 |
-
|
| 54 |
@app.post("/analyze", summary="Analyze a list of audio URLs")
|
| 55 |
def analyze_audios(request: AnalysisRequest):
|
| 56 |
if not request.audio_items:
|
| 57 |
raise HTTPException(status_code=400, detail="No audio items provided.")
|
| 58 |
-
|
| 59 |
logger.info(f"Received request to analyze {len(request.audio_items)} audio items.")
|
| 60 |
results = []
|
| 61 |
-
|
| 62 |
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 63 |
futures = [executor.submit(process_item_wrapper, item) for item in request.audio_items]
|
| 64 |
-
|
| 65 |
for future in futures:
|
| 66 |
results.append(future.result())
|
| 67 |
-
|
| 68 |
logger.info("Finished processing all items.")
|
| 69 |
-
return {"analysis_results": results}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import logging
|
| 2 |
+
import os
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from fastapi.responses import FileResponse # --- NEW IMPORT ---
|
| 5 |
from pydantic import BaseModel, HttpUrl
|
| 6 |
from typing import List, Dict
|
| 7 |
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
| 18 |
version="1.0.0"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# --- The directory where output files are stored ---
|
| 22 |
+
OUTPUT_DIR = "./processed_audio"
|
| 23 |
+
|
| 24 |
|
| 25 |
class AudioItem(BaseModel):
|
| 26 |
url: HttpUrl
|
| 27 |
user_id: str
|
| 28 |
|
|
|
|
| 29 |
class AnalysisRequest(BaseModel):
|
| 30 |
audio_items: List[AudioItem]
|
| 31 |
|
|
|
|
| 32 |
def process_item_wrapper(item: AudioItem) -> Dict:
|
| 33 |
try:
|
| 34 |
logger.info(f"Starting analysis for user '{item.user_id}' with URL: {item.url}")
|
| 35 |
result = process_interview(str(item.url))
|
| 36 |
+
# --- MODIFICATION: Return just the filename, not the full path ---
|
| 37 |
+
if result and "pdf_path" in result:
|
| 38 |
+
result["pdf_filename"] = os.path.basename(result["pdf_path"])
|
| 39 |
+
if result and "json_path" in result:
|
| 40 |
+
result["json_filename"] = os.path.basename(result["json_path"])
|
| 41 |
+
|
| 42 |
return {
|
| 43 |
"user_id": item.user_id,
|
| 44 |
"url": str(item.url),
|
|
|
|
| 54 |
"detail": str(e)
|
| 55 |
}
|
| 56 |
|
|
|
|
| 57 |
@app.get("/health", summary="Check API Health")
|
| 58 |
def health_check():
|
| 59 |
return {"status": "ok"}
|
| 60 |
|
|
|
|
| 61 |
@app.post("/analyze", summary="Analyze a list of audio URLs")
|
| 62 |
def analyze_audios(request: AnalysisRequest):
|
| 63 |
if not request.audio_items:
|
| 64 |
raise HTTPException(status_code=400, detail="No audio items provided.")
|
|
|
|
| 65 |
logger.info(f"Received request to analyze {len(request.audio_items)} audio items.")
|
| 66 |
results = []
|
|
|
|
| 67 |
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 68 |
futures = [executor.submit(process_item_wrapper, item) for item in request.audio_items]
|
|
|
|
| 69 |
for future in futures:
|
| 70 |
results.append(future.result())
|
|
|
|
| 71 |
logger.info("Finished processing all items.")
|
| 72 |
+
return {"analysis_results": results}
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# --- NEW ENDPOINT TO DOWNLOAD OUTPUT FILES ---
|
| 76 |
+
@app.get("/outputs/{file_name}", summary="Download an output file")
|
| 77 |
+
def get_output_file(file_name: str):
|
| 78 |
+
"""
|
| 79 |
+
Downloads a generated report (PDF or JSON) by its filename.
|
| 80 |
+
"""
|
| 81 |
+
file_path = os.path.join(OUTPUT_DIR, file_name)
|
| 82 |
+
|
| 83 |
+
# Security check to prevent accessing files outside the output directory
|
| 84 |
+
if not os.path.abspath(file_path).startswith(os.path.abspath(OUTPUT_DIR)):
|
| 85 |
+
raise HTTPException(status_code=403, detail="Forbidden: Access is denied.")
|
| 86 |
+
|
| 87 |
+
if os.path.exists(file_path):
|
| 88 |
+
return FileResponse(path=file_path, media_type='application/octet-stream', filename=file_name)
|
| 89 |
+
else:
|
| 90 |
+
raise HTTPException(status_code=404, detail="File not found.")
|