brichett commited on
Commit
0b15d02
·
verified ·
1 Parent(s): 2e46e9b

Update src/classification_module/dio_support_detector.py

Browse files
src/classification_module/dio_support_detector.py CHANGED
@@ -1,135 +1,135 @@
1
- import ast
2
- from hamilton.function_modifiers import extract_fields
3
- from types import ModuleType
4
- import math
5
- from langchain.llms import OpenAI
6
- import requests
7
- import requests.models
8
- from knowledge_service.knowledge_retrieval import get_information
9
- import traceback
10
-
11
- prompt = """
12
-
13
- Envy:
14
-
15
- 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?"
16
-
17
- Humiliation:
18
-
19
- Pride:
20
-
21
- Elation:
22
-
23
- """
24
-
25
- def detect_entity_support(user_input: str, analyze_affect: dict, ner_public_url: str) -> dict:
26
- """
27
- Detect whether the user input text is glorifying or supporting an entity
28
- from the Dangerous Individuals & Organizations KG.
29
- """
30
- input_text = {
31
- "text": user_input,
32
- "entity_type": "organization"
33
- }
34
-
35
- # Function to fetch the response from the Mistral model
36
- def fetch_ner_entities(input_text):
37
- response: requests.models.Response = requests.post(f'{ner_public_url}/universal-ner', json=input_text, stream=False)
38
- response.raise_for_status()
39
- result = response.json()
40
- print(result)
41
- if result.get('ner_output'):
42
- output = result.get('ner_output').strip()
43
- try:
44
- output = ast.literal_eval(output)
45
- except:
46
- traceback.print_exc()
47
- return []
48
-
49
- return output
50
- return []
51
-
52
- def fetch_ner_orgs(user_input):
53
- input_text = {
54
- "text": user_input,
55
- "entity_type": "organization"
56
- }
57
- return fetch_ner_entities(input_text)
58
-
59
- def fetch_ner_persons(user_input):
60
- input_text = {
61
- "text": user_input,
62
- "entity_type": "person"
63
- }
64
- return fetch_ner_entities(input_text)
65
-
66
- # Fetch the entities
67
- extracted_entities = fetch_ner_orgs(user_input)
68
- extracted_entities.extend(fetch_ner_persons(user_input))
69
-
70
- for entity in extracted_entities:
71
- entity = entity.strip()
72
- entity_info = get_information(entity, "dangerous_organizations")
73
- if entity_info:
74
- analyze_affect.update({
75
- "entity_detected": True,
76
- "entity_name": entity,
77
- "entity_info": entity_info
78
- })
79
- return analyze_affect
80
- entity_info = get_information(entity, 'dangerous_individuals')
81
- if entity_info:
82
- analyze_affect.update({
83
- "entity_detected": True,
84
- "entity_name": entity,
85
- "entity_info": entity_info
86
- })
87
- return analyze_affect
88
- analyze_affect['entity_detected'] = False
89
- return analyze_affect
90
-
91
- @extract_fields(
92
- dict(
93
- extremism_detected=bool,
94
- ideology=str,
95
- type_label=str,
96
- entity_detected=bool,
97
- entity_name=str,
98
- entity_info=dict,
99
- aspect_sentiment=str
100
- )
101
- )
102
- def detect_glorification(
103
- user_input: str,
104
- detect_entity_support: dict,
105
- mistral_public_url: str
106
- ) -> dict:
107
- """
108
- Analyze the sentiment of the input text and determine if it glorifies or supports an entity.
109
- """
110
- if detect_entity_support["entity_detected"]:
111
- prompt = (
112
- f"Analyze the following text to determine if it glorifies or supports the following entity:\n\n"
113
- f"Entity: {detect_entity_support['entity_name']}\n\n"
114
- f"Text: '{user_input}'\n\n"
115
- "The answer should be one of the following: Glorifying, Supporting, Neutral, Negative."
116
- )
117
-
118
- input_text = {
119
- "context": f"User input text: {user_input}\n\nEntity information: {detect_entity_support['entity_info']}",
120
- "question": "Does the text glorify, support, or speak neutrally or negatively about the entity? Classify the sentiment as one of Glorifying, Supporting, Neutral, Negative:"
121
- }
122
-
123
- response = requests.post(f'{mistral_public_url}/mistral-inference', json=input_text, stream=False)
124
-
125
- detect_entity_support.update({
126
- "aspect_sentiment": response.text.strip()
127
- })
128
- return dict(
129
- **detect_entity_support
130
- )
131
-
132
- detect_entity_support["aspect_sentiment"] = "None"
133
- return dict(
134
- **detect_entity_support
135
  )
 
1
+ import ast
2
+ from hamilton.function_modifiers import extract_fields
3
+ from types import ModuleType
4
+ import math
5
+ from langchain.llms import OpenAI
6
+ import requests
7
+ import requests.models
8
+ from knowledge_service.knowledge_retrieval import get_information
9
+ import traceback
10
+
11
+ prompt = """
12
+
13
+ Envy:
14
+
15
+ 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?"
16
+
17
+ Humiliation:
18
+
19
+ Pride:
20
+
21
+ Elation:
22
+
23
+ """
24
+
25
+ def detect_entity_support(user_input: str, analyze_affect: dict, ner_public_url: str) -> dict:
26
+ """
27
+ Detect whether the user input text is glorifying or supporting an entity
28
+ from the Dangerous Individuals & Organizations KG.
29
+ """
30
+ input_text = {
31
+ "text": user_input,
32
+ "entity_type": "organization"
33
+ }
34
+
35
+ # Function to fetch the response from the Mistral model
36
+ def fetch_ner_entities(input_text):
37
+ response: requests.models.Response = requests.post(f'{ner_public_url}/universal-ner', json=input_text, stream=False)
38
+ response.raise_for_status()
39
+ result = response.json()
40
+ print(result)
41
+ if result.get('ner_output'):
42
+ output = result.get('ner_output').strip()
43
+ try:
44
+ output = ast.literal_eval(output)
45
+ except:
46
+ traceback.print_exc()
47
+ return []
48
+
49
+ return output
50
+ return []
51
+
52
+ def fetch_ner_orgs(user_input):
53
+ input_text = {
54
+ "text": user_input,
55
+ "entity_type": "organization"
56
+ }
57
+ return fetch_ner_entities(input_text)
58
+
59
+ def fetch_ner_persons(user_input):
60
+ input_text = {
61
+ "text": user_input,
62
+ "entity_type": "person"
63
+ }
64
+ return fetch_ner_entities(input_text)
65
+
66
+ # Fetch the entities
67
+ extracted_entities = fetch_ner_orgs(user_input)
68
+ extracted_entities.extend(fetch_ner_persons(user_input))
69
+
70
+ for entity in extracted_entities:
71
+ entity = entity.strip()
72
+ entity_info = get_information(entity, "dangerous_organizations")
73
+ if entity_info:
74
+ analyze_affect.update({
75
+ "entity_detected": True,
76
+ "entity_name": entity,
77
+ "entity_info": entity_info
78
+ })
79
+ return analyze_affect
80
+ entity_info = get_information(entity, 'dangerous_individuals')
81
+ if entity_info:
82
+ analyze_affect.update({
83
+ "entity_detected": True,
84
+ "entity_name": entity,
85
+ "entity_info": entity_info
86
+ })
87
+ return analyze_affect
88
+ analyze_affect['entity_detected'] = False
89
+ return analyze_affect
90
+
91
+ @extract_fields(
92
+ dict(
93
+ extremism_detected=bool,
94
+ ideology=str,
95
+ type_label=str,
96
+ entity_detected=bool,
97
+ entity_name=str,
98
+ entity_info=dict,
99
+ aspect_sentiment=str
100
+ )
101
+ )
102
+ def detect_glorification(
103
+ user_input: str,
104
+ detect_entity_support: dict,
105
+ mistral_public_url: str
106
+ ) -> dict:
107
+ """
108
+ Analyze the sentiment of the input text and determine if it glorifies or supports an entity.
109
+ """
110
+ if detect_entity_support["entity_detected"]:
111
+ prompt = (
112
+ f"Analyze the following text to determine if it glorifies or supports the following entity:\n\n"
113
+ f"Entity: {detect_entity_support['entity_name']}\n\n"
114
+ f"Text: '{user_input}'\n\n"
115
+ "The answer should be one of the following: Glorifying, Supporting, Neutral, Negative."
116
+ )
117
+
118
+ input_text = {
119
+ "context": f"Analyze the following text to determine if it glorifies, supports, or speaks neutrally or negatively about the following entity:\n\nUser input text: {user_input}\n\nEntity information: {detect_entity_support['entity_info']}",
120
+ "question": "Does the text glorify, support, or speak neutrally or negatively about the entity? Classify the opinion sentiment expressed by the text *towards the mentioned entity* as one of Glorifying, Supporting, Neutral, Negative:"
121
+ }
122
+
123
+ response = requests.post(f'{mistral_public_url}/mistral-inference', json=input_text, stream=False)
124
+
125
+ detect_entity_support.update({
126
+ "aspect_sentiment": response.text.strip()
127
+ })
128
+ return dict(
129
+ **detect_entity_support
130
+ )
131
+
132
+ detect_entity_support["aspect_sentiment"] = "None"
133
+ return dict(
134
+ **detect_entity_support
135
  )