File size: 3,909 Bytes
c8b1fd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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)
    )