Spaces:
Sleeping
Sleeping
syncing with old implementation
Browse files
app/routers/analyze.py
CHANGED
|
@@ -3,7 +3,7 @@ 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
|
| 7 |
import logging
|
| 8 |
|
| 9 |
logger = logging.getLogger(__name__)
|
|
@@ -11,6 +11,7 @@ logger = logging.getLogger(__name__)
|
|
| 11 |
router = APIRouter(tags=["analysis"])
|
| 12 |
|
| 13 |
scraper = ArticleScraper()
|
|
|
|
| 14 |
|
| 15 |
@router.post("/analyze", response_model=AnalyzeResponse)
|
| 16 |
async def analyze_content(request: AnalyzeRequest):
|
|
@@ -24,7 +25,7 @@ async def analyze_content(request: AnalyzeRequest):
|
|
| 24 |
)
|
| 25 |
|
| 26 |
# Perform the analysis (like your old code)
|
| 27 |
-
analysis =
|
| 28 |
article["headline"],
|
| 29 |
article["content"]
|
| 30 |
)
|
|
|
|
| 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.scoring import MediaScorer # Assuming you have a scorer module
|
| 7 |
import logging
|
| 8 |
|
| 9 |
logger = logging.getLogger(__name__)
|
|
|
|
| 11 |
router = APIRouter(tags=["analysis"])
|
| 12 |
|
| 13 |
scraper = ArticleScraper()
|
| 14 |
+
scorer = MediaScorer()
|
| 15 |
|
| 16 |
@router.post("/analyze", response_model=AnalyzeResponse)
|
| 17 |
async def analyze_content(request: AnalyzeRequest):
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
# Perform the analysis (like your old code)
|
| 28 |
+
analysis = scorer.calculate_media_score(
|
| 29 |
article["headline"],
|
| 30 |
article["content"]
|
| 31 |
)
|
mediaunmasked/schemas/requests.py
CHANGED
|
@@ -1,5 +1,30 @@
|
|
| 1 |
-
from pydantic import BaseModel
|
|
|
|
| 2 |
|
| 3 |
class AnalyzeRequest(BaseModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
headline: str
|
| 5 |
-
content: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, HttpUrl
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
|
| 4 |
class AnalyzeRequest(BaseModel):
|
| 5 |
+
url: HttpUrl
|
| 6 |
+
|
| 7 |
+
def get_url_str(self) -> str:
|
| 8 |
+
# Convert HttpUrl to string safely
|
| 9 |
+
return str(self.url)
|
| 10 |
+
|
| 11 |
+
class MediaScoreDetails(BaseModel):
|
| 12 |
+
headline_analysis: Dict[str, Any]
|
| 13 |
+
sentiment_analysis: Dict[str, Any]
|
| 14 |
+
bias_analysis: Dict[str, Any]
|
| 15 |
+
evidence_analysis: Dict[str, Any]
|
| 16 |
+
|
| 17 |
+
class MediaScore(BaseModel):
|
| 18 |
+
media_unmasked_score: float
|
| 19 |
+
rating: str
|
| 20 |
+
details: MediaScoreDetails
|
| 21 |
+
|
| 22 |
+
class AnalysisResponse(BaseModel):
|
| 23 |
headline: str
|
| 24 |
+
content: str
|
| 25 |
+
sentiment: str
|
| 26 |
+
bias: str
|
| 27 |
+
bias_score: float
|
| 28 |
+
bias_percentage: float
|
| 29 |
+
flagged_phrases: List[str]
|
| 30 |
+
media_score: MediaScore
|
mediaunmasked/services/analyzer_service.py
CHANGED
|
@@ -1,4 +1,13 @@
|
|
| 1 |
from mediaunmasked.analyzers.headline_analyzer import HeadlineAnalyzer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class AnalyzerService:
|
| 4 |
def __init__(self):
|
|
@@ -6,4 +15,68 @@ class AnalyzerService:
|
|
| 6 |
|
| 7 |
async def analyze_content(self, headline: str, content: str):
|
| 8 |
result = self.headline_analyzer.analyze(headline, content)
|
| 9 |
-
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from mediaunmasked.analyzers.headline_analyzer import HeadlineAnalyzer
|
| 2 |
+
from mediaunmasked.schemas.requests import AnalyzeRequest, AnalysisResponse
|
| 3 |
+
from fastapi import HTTPException
|
| 4 |
+
from mediaunmasked.scrapers.article_scraper import ArticleScraper
|
| 5 |
+
from mediaunmasked.analyzers.scoring import MediaScorer
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
scraper = ArticleScraper()
|
| 10 |
+
scorer = MediaScorer()
|
| 11 |
|
| 12 |
class AnalyzerService:
|
| 13 |
def __init__(self):
|
|
|
|
| 15 |
|
| 16 |
async def analyze_content(self, headline: str, content: str):
|
| 17 |
result = self.headline_analyzer.analyze(headline, content)
|
| 18 |
+
return result
|
| 19 |
+
|
| 20 |
+
async def analyze_url(self, request: AnalyzeRequest) -> AnalysisResponse:
|
| 21 |
+
"""
|
| 22 |
+
Analyze an article for bias, sentiment, and credibility.
|
| 23 |
+
"""
|
| 24 |
+
try:
|
| 25 |
+
logger.info(f"Analyzing article: {request.url}")
|
| 26 |
+
|
| 27 |
+
# Scrape article
|
| 28 |
+
article = await scraper.scrape_article(request.get_url_str())
|
| 29 |
+
if not article:
|
| 30 |
+
raise HTTPException(
|
| 31 |
+
status_code=400,
|
| 32 |
+
detail="Failed to scrape article content"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Analyze content
|
| 36 |
+
analysis = scorer.calculate_media_score(
|
| 37 |
+
article["headline"],
|
| 38 |
+
article["content"]
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Construct response
|
| 42 |
+
response_dict = {
|
| 43 |
+
"headline": str(article['headline']),
|
| 44 |
+
"content": str(article['content']),
|
| 45 |
+
"sentiment": str(analysis['details']['sentiment_analysis']['sentiment']),
|
| 46 |
+
"bias": str(analysis['details']['bias_analysis']['bias']),
|
| 47 |
+
"bias_score": float(analysis['details']['bias_analysis']['bias_score']),
|
| 48 |
+
"bias_percentage": float(analysis['details']['bias_analysis']['bias_percentage']),
|
| 49 |
+
"flagged_phrases": list(analysis['details']['sentiment_analysis']['flagged_phrases']),
|
| 50 |
+
"media_score": {
|
| 51 |
+
"media_unmasked_score": float(analysis['media_unmasked_score']),
|
| 52 |
+
"rating": str(analysis['rating']),
|
| 53 |
+
"details": {
|
| 54 |
+
"headline_analysis": {
|
| 55 |
+
"headline_vs_content_score": float(analysis['details']['headline_analysis']['headline_vs_content_score']),
|
| 56 |
+
"contradictory_phrases": analysis['details']['headline_analysis'].get('contradictory_phrases', [])
|
| 57 |
+
},
|
| 58 |
+
"sentiment_analysis": {
|
| 59 |
+
"sentiment": str(analysis['details']['sentiment_analysis']['sentiment']),
|
| 60 |
+
"manipulation_score": float(analysis['details']['sentiment_analysis']['manipulation_score']),
|
| 61 |
+
"flagged_phrases": list(analysis['details']['sentiment_analysis']['flagged_phrases'])
|
| 62 |
+
},
|
| 63 |
+
"bias_analysis": {
|
| 64 |
+
"bias": str(analysis['details']['bias_analysis']['bias']),
|
| 65 |
+
"bias_score": float(analysis['details']['bias_analysis']['bias_score']),
|
| 66 |
+
"bias_percentage": float(analysis['details']['bias_analysis']['bias_percentage'])
|
| 67 |
+
},
|
| 68 |
+
"evidence_analysis": {
|
| 69 |
+
"evidence_based_score": float(analysis['details']['evidence_analysis']['evidence_based_score'])
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
return AnalysisResponse.parse_obj(response_dict)
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
logger.error(f"Analysis failed: {str(e)}", exc_info=True)
|
| 79 |
+
raise HTTPException(
|
| 80 |
+
status_code=500,
|
| 81 |
+
detail=f"Analysis failed: {str(e)}"
|
| 82 |
+
)
|