Spaces:
Sleeping
Sleeping
updating analyze_content to take in url and scrape content
Browse files- app/routers/analyze.py +64 -6
app/routers/analyze.py
CHANGED
|
@@ -2,17 +2,75 @@ from fastapi import APIRouter, HTTPException
|
|
| 2 |
from mediaunmasked.schemas.requests import AnalyzeRequest
|
| 3 |
from mediaunmasked.schemas.responses import AnalyzeResponse
|
| 4 |
from mediaunmasked.services.analyzer_service import AnalyzerService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
router = APIRouter(tags=["analysis"])
|
| 7 |
|
|
|
|
|
|
|
| 8 |
@router.post("/analyze", response_model=AnalyzeResponse)
|
| 9 |
async def analyze_content(request: AnalyzeRequest):
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
except Exception as e:
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from mediaunmasked.schemas.requests import AnalyzeRequest
|
| 3 |
from mediaunmasked.schemas.responses import AnalyzeResponse
|
| 4 |
from mediaunmasked.services.analyzer_service import AnalyzerService
|
| 5 |
+
from mediaunmasked.scrapers.article_scraper import ArticleScraper # Assuming you have a scraper module
|
| 6 |
+
from mediaunmasked.analyzers import scoring # Assuming you have a scorer module
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
router = APIRouter(tags=["analysis"])
|
| 12 |
|
| 13 |
+
scraper = ArticleScraper()
|
| 14 |
+
|
| 15 |
@router.post("/analyze", response_model=AnalyzeResponse)
|
| 16 |
async def analyze_content(request: AnalyzeRequest):
|
| 17 |
try:
|
| 18 |
+
# Scrape the article content from the provided URL
|
| 19 |
+
article = scraper.scrape_article(request.url)
|
| 20 |
+
if not article:
|
| 21 |
+
raise HTTPException(
|
| 22 |
+
status_code=400,
|
| 23 |
+
detail="Failed to scrape article content"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Perform the analysis (like your old code)
|
| 27 |
+
analysis = scoring.calculate_media_score(
|
| 28 |
+
article["headline"],
|
| 29 |
+
article["content"]
|
| 30 |
)
|
| 31 |
+
|
| 32 |
+
# Construct the response
|
| 33 |
+
response_dict = {
|
| 34 |
+
"headline": str(article['headline']),
|
| 35 |
+
"content": str(article['content']),
|
| 36 |
+
"sentiment": str(analysis['details']['sentiment_analysis']['sentiment']),
|
| 37 |
+
"bias": str(analysis['details']['bias_analysis']['bias']),
|
| 38 |
+
"bias_score": float(analysis['details']['bias_analysis']['bias_score']),
|
| 39 |
+
"bias_percentage": float(analysis['details']['bias_analysis']['bias_percentage']),
|
| 40 |
+
"flagged_phrases": list(analysis['details']['sentiment_analysis']['flagged_phrases']),
|
| 41 |
+
"media_score": {
|
| 42 |
+
"media_unmasked_score": float(analysis['media_unmasked_score']),
|
| 43 |
+
"rating": str(analysis['rating']),
|
| 44 |
+
"details": {
|
| 45 |
+
"headline_analysis": {
|
| 46 |
+
"headline_vs_content_score": float(analysis['details']['headline_analysis']['headline_vs_content_score']),
|
| 47 |
+
"contradictory_phrases": analysis['details']['headline_analysis'].get('contradictory_phrases', [])
|
| 48 |
+
},
|
| 49 |
+
"sentiment_analysis": {
|
| 50 |
+
"sentiment": str(analysis['details']['sentiment_analysis']['sentiment']),
|
| 51 |
+
"manipulation_score": float(analysis['details']['sentiment_analysis']['manipulation_score']),
|
| 52 |
+
"flagged_phrases": list(analysis['details']['sentiment_analysis']['flagged_phrases'])
|
| 53 |
+
},
|
| 54 |
+
"bias_analysis": {
|
| 55 |
+
"bias": str(analysis['details']['bias_analysis']['bias']),
|
| 56 |
+
"bias_score": float(analysis['details']['bias_analysis']['bias_score']),
|
| 57 |
+
"bias_percentage": float(analysis['details']['bias_analysis']['bias_percentage'])
|
| 58 |
+
},
|
| 59 |
+
"evidence_analysis": {
|
| 60 |
+
"evidence_based_score": float(analysis['details']['evidence_analysis']['evidence_based_score'])
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
logger.info("Final response structure:")
|
| 67 |
+
logger.info(response_dict)
|
| 68 |
+
|
| 69 |
+
return AnalyzeResponse.parse_obj(response_dict)
|
| 70 |
+
|
| 71 |
except Exception as e:
|
| 72 |
+
logger.error(f"Analysis failed: {str(e)}", exc_info=True)
|
| 73 |
+
raise HTTPException(
|
| 74 |
+
status_code=500,
|
| 75 |
+
detail=f"Analysis failed: {str(e)}"
|
| 76 |
+
)
|