TruthLens-Backend / services /analysis /perspective_detector.py
Gargi Monga
Deploy backend
c8b1fd7
Raw
History Blame Contribute Delete
3.91 kB
import json
from typing import Dict, Any
import spacy
from ai.sarvam_client import generate_response, extract_json
try:
nlp = spacy.load("en_core_web_sm")
except OSError:
raise OSError(
"spaCy model not found. Run: python -m spacy download en_core_web_sm"
)
def extract_context(text: str) -> Dict[str, list]:
doc = nlp(text)
entities = list(
set(
ent.text
for ent in doc.ents
)
)
keywords = []
for token in doc:
if (
token.is_stop
or token.is_punct
or token.is_space
):
continue
if len(token.text) <= 2:
continue
keywords.append(
token.lemma_.lower()
)
keywords = list(set(keywords))
return {
"entities": entities,
"keywords": keywords
}
def create_perspective_prompt(text: str) -> str:
context = extract_context(text)
prompt = f"""
You are an expert media analysis assistant.
Analyze the following content and determine:
1. Whether expert opinions are missing.
2. Whether opposing viewpoints are missing.
3. Whether stakeholder perspectives are missing.
4. Suggest additional perspectives that should be considered.
5. Provide a short explanation.
Definitions:
Missing expert opinions:
The content does not include insights from qualified experts.
Missing opposing viewpoints:
The content presents one side but omits reasonable alternative viewpoints.
Missing stakeholder perspectives:
The content ignores groups affected by the topic.
Detected Entities:
{context["entities"]}
Detected Keywords:
{context["keywords"]}
Do not include markdown, code blocks, explanations, or additional text outside the JSON object.
Return ONLY valid JSON:
{{
"missing_expert_opinions": false,
"missing_opposing_viewpoints": false,
"missing_stakeholder_perspectives": false,
"additional_perspectives": [
"Perspective 1",
"Perspective 2"
],
"explanation": "Short explanation."
}}
Text:
{text}
"""
return prompt
def get_perspective_from_model(text: str) -> str:
prompt = create_perspective_prompt(text)
return generate_response(prompt)
def parse_model_response(response: str) -> Dict[str, Any]:
if response is None:
return {
"missing_expert_opinions": False,
"missing_opposing_viewpoints": False,
"missing_stakeholder_perspectives": False,
"additional_perspectives": [],
"explanation": "No response received from Sarvam AI."
}
parsed = extract_json(response)
if parsed is None:
return {
"missing_expert_opinions": False,
"missing_opposing_viewpoints": False,
"missing_stakeholder_perspectives": False,
"additional_perspectives": [],
"explanation": "Could not parse model response."
}
return parsed
def analyze_perspectives(text: str) -> Dict[str, Any]:
if not text or not text.strip():
return {
"missing_expert_opinions": False,
"missing_opposing_viewpoints": False,
"missing_stakeholder_perspectives": False,
"additional_perspectives": [],
"explanation": "Empty input text."
}
raw_response = get_perspective_from_model(text)
return parse_model_response(raw_response)
if __name__ == "__main__":
sample_text = """
The government announced a new tax policy.
Officials claim it will improve economic growth.
"""
print(
analyze_perspectives(sample_text)
)