Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -129,36 +129,39 @@ class EventAnalyzer:
|
|
| 129 |
|
| 130 |
return entities
|
| 131 |
|
| 132 |
-
|
| 133 |
return self.ontology.validate_pattern(text, 'temporal')
|
| 134 |
|
| 135 |
async def extract_locations(self, text):
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
| 137 |
pattern_locations = self.ontology.validate_pattern(text, 'location')
|
| 138 |
return list(set(ml_locations + pattern_locations))
|
| 139 |
|
| 140 |
async def analyze_event(self, text):
|
| 141 |
try:
|
| 142 |
# Parallel extraction
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
|
| 147 |
-
# Gather results
|
| 148 |
-
entities,
|
| 149 |
-
|
| 150 |
)
|
| 151 |
|
| 152 |
-
#
|
| 153 |
entities['locations'] = locations
|
| 154 |
-
entities['temporal'] =
|
| 155 |
|
| 156 |
# Calculate initial confidence
|
| 157 |
confidence = min(1.0, (
|
| 158 |
0.2 * bool(entities["people"]) +
|
| 159 |
0.2 * bool(entities["organizations"]) +
|
| 160 |
0.3 * bool(entities["locations"]) +
|
| 161 |
-
0.3 * bool(
|
| 162 |
))
|
| 163 |
|
| 164 |
# Find related events
|
|
@@ -172,7 +175,7 @@ class EventAnalyzer:
|
|
| 172 |
relationship_confidence = max(
|
| 173 |
self.relationship_engine.calculate_relationship_confidence(
|
| 174 |
{'entities': entities},
|
| 175 |
-
{'text': event[1]}
|
| 176 |
)
|
| 177 |
for event in related_events
|
| 178 |
)
|
|
@@ -285,6 +288,7 @@ def format_results(analysis_result):
|
|
| 285 |
"""
|
| 286 |
return html
|
| 287 |
|
|
|
|
| 288 |
async def process_input(text):
|
| 289 |
result = await analyzer.analyze_event(text)
|
| 290 |
return format_results(result)
|
|
@@ -299,7 +303,7 @@ demo = gr.Interface(
|
|
| 299 |
)
|
| 300 |
],
|
| 301 |
outputs=gr.HTML(),
|
| 302 |
-
title="
|
| 303 |
description="Analyze text to extract entities, assess confidence, and identify key event information with relationship tracking.",
|
| 304 |
css=css,
|
| 305 |
theme=gr.themes.Soft(),
|
|
|
|
| 129 |
|
| 130 |
return entities
|
| 131 |
|
| 132 |
+
def extract_temporal(self, text):
|
| 133 |
return self.ontology.validate_pattern(text, 'temporal')
|
| 134 |
|
| 135 |
async def extract_locations(self, text):
|
| 136 |
+
# First await the entities result
|
| 137 |
+
entities = await self.extract_entities(text)
|
| 138 |
+
ml_locations = entities.get('locations', [])
|
| 139 |
+
# Get pattern-based locations
|
| 140 |
pattern_locations = self.ontology.validate_pattern(text, 'location')
|
| 141 |
return list(set(ml_locations + pattern_locations))
|
| 142 |
|
| 143 |
async def analyze_event(self, text):
|
| 144 |
try:
|
| 145 |
# Parallel extraction
|
| 146 |
+
entities_future = self.extract_entities(text)
|
| 147 |
+
temporal_data = self.extract_temporal(text) # This is synchronous now
|
| 148 |
+
locations_future = self.extract_locations(text)
|
| 149 |
|
| 150 |
+
# Gather async results
|
| 151 |
+
entities, locations = await asyncio.gather(
|
| 152 |
+
entities_future, locations_future
|
| 153 |
)
|
| 154 |
|
| 155 |
+
# Add temporal and locations to entities
|
| 156 |
entities['locations'] = locations
|
| 157 |
+
entities['temporal'] = temporal_data
|
| 158 |
|
| 159 |
# Calculate initial confidence
|
| 160 |
confidence = min(1.0, (
|
| 161 |
0.2 * bool(entities["people"]) +
|
| 162 |
0.2 * bool(entities["organizations"]) +
|
| 163 |
0.3 * bool(entities["locations"]) +
|
| 164 |
+
0.3 * bool(temporal_data)
|
| 165 |
))
|
| 166 |
|
| 167 |
# Find related events
|
|
|
|
| 175 |
relationship_confidence = max(
|
| 176 |
self.relationship_engine.calculate_relationship_confidence(
|
| 177 |
{'entities': entities},
|
| 178 |
+
{'text': event[1]}
|
| 179 |
)
|
| 180 |
for event in related_events
|
| 181 |
)
|
|
|
|
| 288 |
"""
|
| 289 |
return html
|
| 290 |
|
| 291 |
+
# Modified to properly handle async
|
| 292 |
async def process_input(text):
|
| 293 |
result = await analyzer.analyze_event(text)
|
| 294 |
return format_results(result)
|
|
|
|
| 303 |
)
|
| 304 |
],
|
| 305 |
outputs=gr.HTML(),
|
| 306 |
+
title="ToY Event Analysis System",
|
| 307 |
description="Analyze text to extract entities, assess confidence, and identify key event information with relationship tracking.",
|
| 308 |
css=css,
|
| 309 |
theme=gr.themes.Soft(),
|