Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import networkx as nx
|
3 |
import random
|
4 |
import logging
|
|
|
5 |
|
6 |
# Configure logging
|
7 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -22,16 +23,38 @@ def triplextract(text: str, entity_types: list[str], predicates: list[str]) -> s
|
|
22 |
A string representation of the extracted triples, or an error message if extraction fails.
|
23 |
"""
|
24 |
logging.debug(f"triplextract called with text: {text}, entity_types: {entity_types}, predicates: {predicates}")
|
|
|
25 |
try:
|
26 |
# Replace this with your actual NLP pipeline logic
|
27 |
# This is a placeholder for demonstration purposes
|
28 |
# Example: "Alice knows Bob" -> ("Alice", "knows", "Bob")
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
else:
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
except Exception as e:
|
36 |
error_message = f"Error in triplextract: {str(e)}"
|
37 |
logging.exception(error_message) # Log the full exception with traceback
|
@@ -50,26 +73,40 @@ def parse_triples(triples_str: str) -> tuple[list[str], list[tuple[str, str, str
|
|
50 |
- A list of relationships (tuples of (subject, predicate, object)).
|
51 |
"""
|
52 |
logging.debug(f"parse_triples called with triples_str: {triples_str}")
|
|
|
53 |
try:
|
54 |
# Replace this with your actual parsing logic based on triplextract's output
|
55 |
# This is a placeholder for demonstration purposes
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
entities = set()
|
60 |
relationships = []
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
return list(entities), relationships
|
68 |
-
except (SyntaxError, ValueError) as e:
|
69 |
-
error_message = f"Error in parse_triples: Invalid triples string format: {str(e)}"
|
70 |
-
logging.error(error_message)
|
71 |
-
return [], [] # Return empty lists to prevent further errors
|
72 |
-
|
73 |
except Exception as e:
|
74 |
error_message = f"Error in parse_triples: {str(e)}"
|
75 |
logging.exception(error_message)
|
@@ -269,6 +306,16 @@ snippets = {
|
|
269 |
text_input="The cat sat on the mat.",
|
270 |
entity_types="Animal, Object",
|
271 |
predicates="sat on"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
)
|
273 |
}
|
274 |
|
@@ -277,6 +324,7 @@ snippets = {
|
|
277 |
WORD_LIMIT = 300
|
278 |
|
279 |
def process_text(text: str, entity_types: str, predicates: str, layout_type: str, visualization_type: str):
|
|
|
280 |
if not text:
|
281 |
return None, None, "Please enter some text."
|
282 |
|
|
|
2 |
import networkx as nx
|
3 |
import random
|
4 |
import logging
|
5 |
+
import ast
|
6 |
|
7 |
# Configure logging
|
8 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
23 |
A string representation of the extracted triples, or an error message if extraction fails.
|
24 |
"""
|
25 |
logging.debug(f"triplextract called with text: {text}, entity_types: {entity_types}, predicates: {predicates}")
|
26 |
+
print(f"triplextract input:\ntext: {text}\nentity_types: {entity_types}\npredicates: {predicates}")
|
27 |
try:
|
28 |
# Replace this with your actual NLP pipeline logic
|
29 |
# This is a placeholder for demonstration purposes
|
30 |
# Example: "Alice knows Bob" -> ("Alice", "knows", "Bob")
|
31 |
+
|
32 |
+
# TEMPORARY - Hardcoded triple for testing
|
33 |
+
# if "Global warming" in text:
|
34 |
+
# prediction = "[('Global warming', 'causes', 'climate changes')]" #hardcoded string
|
35 |
+
# print(f"triplextract output: {prediction}")
|
36 |
+
# return prediction
|
37 |
+
# else:
|
38 |
+
# prediction = "[]"
|
39 |
+
# print(f"triplextract output: {prediction}")
|
40 |
+
# return "[]"
|
41 |
+
|
42 |
+
if "Global warming" in text:
|
43 |
+
triples = [
|
44 |
+
('Global warming', 'causes', 'climate changes'),
|
45 |
+
('temperature', 'increased by', '1C'),
|
46 |
+
('warming', 'caused by', 'human activities'),
|
47 |
+
('Paris Agreement', 'aims to limit', 'temperature increase')
|
48 |
+
]
|
49 |
+
prediction = str(triples)
|
50 |
+
print(f"triplextract output: {prediction}")
|
51 |
+
return prediction
|
52 |
else:
|
53 |
+
prediction = "[]"
|
54 |
+
print(f"triplextract output: {prediction}")
|
55 |
+
return "[]"
|
56 |
+
|
57 |
+
# ... (Remove this hardcoding once the rest works)
|
58 |
except Exception as e:
|
59 |
error_message = f"Error in triplextract: {str(e)}"
|
60 |
logging.exception(error_message) # Log the full exception with traceback
|
|
|
73 |
- A list of relationships (tuples of (subject, predicate, object)).
|
74 |
"""
|
75 |
logging.debug(f"parse_triples called with triples_str: {triples_str}")
|
76 |
+
print(f"parse_triples input: {triples_str}")
|
77 |
try:
|
78 |
# Replace this with your actual parsing logic based on triplextract's output
|
79 |
# This is a placeholder for demonstration purposes
|
80 |
+
# Properly handle error cases where ast.literal_eval fails
|
81 |
+
|
82 |
+
try:
|
83 |
+
triples_list = ast.literal_eval(triples_str) # Safely evaluate the string as a list
|
84 |
+
except (SyntaxError, ValueError) as e:
|
85 |
+
error_message = f"Error in parse_triples: Invalid triples string format: {str(e)}"
|
86 |
+
logging.error(error_message)
|
87 |
+
return [], [] # Return empty lists to prevent further errors
|
88 |
+
except Exception as e:
|
89 |
+
error_message = f"Unexpected error in parse_triples during literal_eval: {str(e)}"
|
90 |
+
logging.exception(error_message)
|
91 |
+
return [], []
|
92 |
+
|
93 |
|
94 |
entities = set()
|
95 |
relationships = []
|
96 |
+
if isinstance(triples_list, list):
|
97 |
+
for triple in triples_list:
|
98 |
+
if isinstance(triple, tuple) and len(triple) == 3:
|
99 |
+
subject, predicate, object_ = triple # Unpack the triple
|
100 |
+
entities.add(subject)
|
101 |
+
entities.add(object_)
|
102 |
+
relationships.append((subject, predicate, object_))
|
103 |
+
else:
|
104 |
+
logging.warning(f"Invalid triple format: {triple}. Skipping.")
|
105 |
+
else:
|
106 |
+
logging.warning(f"Triples list is not a list, but {type(triples_list)}. Returning empty lists.")
|
107 |
+
return [], []
|
108 |
|
109 |
return list(entities), relationships
|
|
|
|
|
|
|
|
|
|
|
110 |
except Exception as e:
|
111 |
error_message = f"Error in parse_triples: {str(e)}"
|
112 |
logging.exception(error_message)
|
|
|
306 |
text_input="The cat sat on the mat.",
|
307 |
entity_types="Animal, Object",
|
308 |
predicates="sat on"
|
309 |
+
),
|
310 |
+
"Sample 3": Sample(
|
311 |
+
text_input="Global warming is causing significant changes to Earth's climate. The average global \
|
312 |
+
temperature has increased by approximately 1C since the pre-industrial era. This warming is \
|
313 |
+
primarily caused by human activities, particularly the emission of greenhouse gases like carbon dioxide. \
|
314 |
+
The Paris Agreement, signed in 2015, aims to limit global temperature increase to well below 2°C above \
|
315 |
+
pre-industrial levels. To achieve this goal, many countries are implementing policies to reduce carbon \
|
316 |
+
emissions and transition to renewable energy sources.",
|
317 |
+
entity_types="Event, Measure, Activity, Agreement",
|
318 |
+
predicates="causes, increased by, aims to limit"
|
319 |
)
|
320 |
}
|
321 |
|
|
|
324 |
WORD_LIMIT = 300
|
325 |
|
326 |
def process_text(text: str, entity_types: str, predicates: str, layout_type: str, visualization_type: str):
|
327 |
+
print(f"process_text input:\ntext: {text}\nentity_types: {entity_types}\npredicates: {predicates}")
|
328 |
if not text:
|
329 |
return None, None, "Please enter some text."
|
330 |
|