File size: 4,821 Bytes
0b15d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d85e6c8
 
0b15d02
 
d85e6c8
0b15d02
 
 
 
 
 
 
 
 
 
 
854f61d
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
import ast
from hamilton.function_modifiers import extract_fields
from types import ModuleType
import math
from langchain.llms import OpenAI
import requests
import requests.models
from knowledge_service.knowledge_retrieval import get_information
import traceback

prompt = """

Envy:

Contempt: "What the hell is wrong with this country? Why is the official page of police in NRW tweeting in Arabic? Are they seeking to appease the barbaric, Muslim, rapist hordes of men?"

Humiliation: 

Pride:

Elation: 

"""

def detect_entity_support(user_input: str, analyze_affect: dict, ner_public_url: str) -> dict:
    """
    Detect whether the user input text is glorifying or supporting an entity
    from the Dangerous Individuals & Organizations KG.
    """
    input_text = {
        "text": user_input,
        "entity_type": "organization"
    }

    # Function to fetch the response from the Mistral model
    def fetch_ner_entities(input_text):
        response: requests.models.Response = requests.post(f'{ner_public_url}/universal-ner', json=input_text, stream=False)
        response.raise_for_status()
        result = response.json()
        print(result)
        if result.get('ner_output'):
            output = result.get('ner_output').strip()
            try:
                output = ast.literal_eval(output)
            except:
                traceback.print_exc()
                return []
            
            return output
        return []

    def fetch_ner_orgs(user_input):
        input_text = {
            "text": user_input,
            "entity_type": "organization"
        }
        return fetch_ner_entities(input_text)
    
    def fetch_ner_persons(user_input):
        input_text = {
            "text": user_input,
            "entity_type": "person"
        }
        return fetch_ner_entities(input_text)
        
    # Fetch the entities
    extracted_entities = fetch_ner_orgs(user_input)
    extracted_entities.extend(fetch_ner_persons(user_input))
    
    for entity in extracted_entities:
        entity = entity.strip()
        entity_info = get_information(entity, "dangerous_organizations")
        if entity_info:
            analyze_affect.update({
                "entity_detected": True,
                "entity_name": entity,
                "entity_info": entity_info
            })
            return analyze_affect
        entity_info = get_information(entity, 'dangerous_individuals')
        if entity_info:
            analyze_affect.update({
                "entity_detected": True,
                "entity_name": entity,
                "entity_info": entity_info
            })
            return analyze_affect
    analyze_affect['entity_detected'] = False
    return analyze_affect

@extract_fields(
    dict(
        extremism_detected=bool,
        ideology=str,
        type_label=str,
        entity_detected=bool,
        entity_name=str,
        entity_info=dict,
        aspect_sentiment=str
    )
)
def detect_glorification(
    user_input: str,
    detect_entity_support: dict,
    mistral_public_url: str
) -> dict:
    """
    Analyze the sentiment of the input text and determine if it glorifies or supports an entity.
    """
    if detect_entity_support["entity_detected"]:
        prompt = (
            f"Analyze the following text to determine if it glorifies or supports the following entity:\n\n"
            f"Entity: {detect_entity_support['entity_name']}\n\n"
            f"Text: '{user_input}'\n\n"
            "The answer should be one of the following: Glorifying, Supporting, Neutral, Negative."
        )
        
        input_text = {
            "context": f"Analyze the included **User Input Text** to determine if it glorifies, supports, or speaks neutrally or negatively about the entity described in **Entity information.**\n\n\n\n\n##CONTEXT INPUTS TO BE CLASSIFIED:\n**User Input Text**: {user_input}\n\n**Entity information**: {detect_entity_support['entity_info']}",
            "question": "Does the above **User Input Text** glorify, support, or speak neutrally or negatively about the entity? Classify the opinion expressed by the text *towards the mentioned entity* as one of Glorification, Support, Neutral, Negative. Do not include your reasoning for the classification in your answer.\nThe above **User Input Text's** opinion expressed towards the mentioned **Entity** is:"
        }
        
        response = requests.post(f'{mistral_public_url}/mistral-sentiment-inference', json=input_text, stream=False)
        
        detect_entity_support.update({
            "aspect_sentiment": response.text.strip()
        })
        return dict(
            **detect_entity_support
        )
    
    detect_entity_support["aspect_sentiment"] = "None"
    return dict(
            **detect_entity_support
        )