Yassine Mhirsi
commited on
Commit
·
7a0be7b
1
Parent(s):
5eb5350
feat: Enhance analysis endpoints to support JSON body input and CSV file uploads for argument analysis
Browse files- routes/analysis.py +61 -30
routes/analysis.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
"""Analysis endpoints for processing arguments, extracting topics, and predicting stance"""
|
| 2 |
|
| 3 |
-
from fastapi import APIRouter, HTTPException, Header, UploadFile, File
|
| 4 |
-
from typing import Optional, List
|
| 5 |
import logging
|
| 6 |
import csv
|
| 7 |
import io
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
from services.analysis_service import analysis_service
|
|
@@ -71,20 +72,63 @@ def parse_csv_file(file_content: bytes) -> List[str]:
|
|
| 71 |
|
| 72 |
@router.post("", response_model=AnalysisResponse, tags=["Analysis"])
|
| 73 |
async def analyse_arguments(
|
| 74 |
-
request:
|
| 75 |
-
file: Optional[UploadFile] = File(None),
|
| 76 |
x_user_id: Optional[str] = Header(None, alias="X-User-ID")
|
| 77 |
):
|
| 78 |
"""
|
| 79 |
-
Analyze arguments: extract topics and predict stance
|
| 80 |
|
| 81 |
-
|
| 82 |
-
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
- **X-User-ID**: User UUID (required in header)
|
| 86 |
-
- **
|
| 87 |
-
- **file**: CSV file with arguments (if using file upload)
|
| 88 |
|
| 89 |
Returns analysis results with extracted topics and stance predictions
|
| 90 |
"""
|
|
@@ -92,31 +136,18 @@ async def analyse_arguments(
|
|
| 92 |
raise HTTPException(status_code=400, detail="X-User-ID header is required")
|
| 93 |
|
| 94 |
try:
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
if file:
|
| 99 |
-
# Read CSV file
|
| 100 |
-
file_content = await file.read()
|
| 101 |
-
arguments = parse_csv_file(file_content)
|
| 102 |
-
|
| 103 |
-
if not arguments:
|
| 104 |
-
raise HTTPException(
|
| 105 |
-
status_code=400,
|
| 106 |
-
detail="CSV file is empty or contains no valid arguments"
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
logger.info(f"Parsed {len(arguments)} arguments from CSV file")
|
| 110 |
-
|
| 111 |
-
elif request and request.arguments:
|
| 112 |
-
arguments = request.arguments
|
| 113 |
-
logger.info(f"Received {len(arguments)} arguments from JSON body")
|
| 114 |
-
else:
|
| 115 |
raise HTTPException(
|
| 116 |
status_code=400,
|
| 117 |
-
detail="
|
| 118 |
)
|
| 119 |
|
|
|
|
|
|
|
| 120 |
# Analyze arguments
|
| 121 |
results = analysis_service.analyze_arguments(
|
| 122 |
user_id=x_user_id,
|
|
|
|
| 1 |
"""Analysis endpoints for processing arguments, extracting topics, and predicting stance"""
|
| 2 |
|
| 3 |
+
from fastapi import APIRouter, HTTPException, Header, UploadFile, File, Form
|
| 4 |
+
from typing import Optional, List, Union
|
| 5 |
import logging
|
| 6 |
import csv
|
| 7 |
import io
|
| 8 |
+
import json
|
| 9 |
from datetime import datetime
|
| 10 |
|
| 11 |
from services.analysis_service import analysis_service
|
|
|
|
| 72 |
|
| 73 |
@router.post("", response_model=AnalysisResponse, tags=["Analysis"])
|
| 74 |
async def analyse_arguments(
|
| 75 |
+
request: AnalysisRequest,
|
|
|
|
| 76 |
x_user_id: Optional[str] = Header(None, alias="X-User-ID")
|
| 77 |
):
|
| 78 |
"""
|
| 79 |
+
Analyze arguments: extract topics and predict stance (JSON body)
|
| 80 |
|
| 81 |
+
- **X-User-ID**: User UUID (required in header)
|
| 82 |
+
- **arguments**: List of argument texts in JSON body
|
| 83 |
+
|
| 84 |
+
Returns analysis results with extracted topics and stance predictions
|
| 85 |
+
"""
|
| 86 |
+
if not x_user_id:
|
| 87 |
+
raise HTTPException(status_code=400, detail="X-User-ID header is required")
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
arguments = request.arguments
|
| 91 |
+
logger.info(f"Received {len(arguments)} arguments from JSON body")
|
| 92 |
+
|
| 93 |
+
# Analyze arguments
|
| 94 |
+
results = analysis_service.analyze_arguments(
|
| 95 |
+
user_id=x_user_id,
|
| 96 |
+
arguments=arguments
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Convert to response models
|
| 100 |
+
analysis_results = [
|
| 101 |
+
AnalysisResult(**result) for result in results
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
logger.info(f"Analysis completed: {len(analysis_results)} results")
|
| 105 |
+
|
| 106 |
+
return AnalysisResponse(
|
| 107 |
+
results=analysis_results,
|
| 108 |
+
total_processed=len(analysis_results),
|
| 109 |
+
timestamp=datetime.now().isoformat()
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
except HTTPException:
|
| 113 |
+
raise
|
| 114 |
+
except ValueError as e:
|
| 115 |
+
logger.error(f"Validation error: {str(e)}")
|
| 116 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 117 |
+
except Exception as e:
|
| 118 |
+
logger.error(f"Analysis error: {str(e)}")
|
| 119 |
+
raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
@router.post("/csv", response_model=AnalysisResponse, tags=["Analysis"])
|
| 123 |
+
async def analyse_arguments_csv(
|
| 124 |
+
file: UploadFile = File(...),
|
| 125 |
+
x_user_id: Optional[str] = Header(None, alias="X-User-ID")
|
| 126 |
+
):
|
| 127 |
+
"""
|
| 128 |
+
Analyze arguments from CSV file: extract topics and predict stance
|
| 129 |
|
| 130 |
- **X-User-ID**: User UUID (required in header)
|
| 131 |
+
- **file**: CSV file with arguments
|
|
|
|
| 132 |
|
| 133 |
Returns analysis results with extracted topics and stance predictions
|
| 134 |
"""
|
|
|
|
| 136 |
raise HTTPException(status_code=400, detail="X-User-ID header is required")
|
| 137 |
|
| 138 |
try:
|
| 139 |
+
# Read CSV file
|
| 140 |
+
file_content = await file.read()
|
| 141 |
+
arguments = parse_csv_file(file_content)
|
| 142 |
|
| 143 |
+
if not arguments:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
raise HTTPException(
|
| 145 |
status_code=400,
|
| 146 |
+
detail="CSV file is empty or contains no valid arguments"
|
| 147 |
)
|
| 148 |
|
| 149 |
+
logger.info(f"Parsed {len(arguments)} arguments from CSV file")
|
| 150 |
+
|
| 151 |
# Analyze arguments
|
| 152 |
results = analysis_service.analyze_arguments(
|
| 153 |
user_id=x_user_id,
|