Spaces:
Sleeping
Sleeping
| # ui/format.py | |
| from string import Template | |
| class ResultFormatter: | |
| def format_entity_list(entities, entity_type): | |
| items = entities.get(entity_type, []) | |
| if not items: | |
| return '<li>None detected</li>' | |
| return ''.join(f'<li>{item}</li>' for item in items) | |
| def format_entity_relationships(relationships): | |
| if not relationships: | |
| return '' | |
| items = [] | |
| for rel in relationships: | |
| item = f""" | |
| <li class="mb-2"> | |
| <strong>{rel['source']}</strong> → | |
| <span class="text-blue-600">{rel['type'].replace('_to_', ' to ')}</span> → | |
| <strong>{rel['target']}</strong> | |
| <br/> | |
| <small class="text-gray-600">Confidence: {int(rel['confidence'] * 100)}%</small> | |
| </li> | |
| """ | |
| items.append(item) | |
| return ''.join(items) | |
| def format_related_events(events): | |
| if not events: | |
| return '' | |
| items = [] | |
| for event in events: | |
| shared = f" | Shared Entities: {event['shared_entities']}" if event.get('shared_entities') else '' | |
| item = f""" | |
| <li class="mb-2"> | |
| <div class="flex justify-between items-center"> | |
| <div>{event['text']}</div> | |
| <div class="text-sm text-gray-600"> | |
| {event['timestamp']} | | |
| Confidence: {int(event['confidence'] * 100)}%{shared} | |
| </div> | |
| </div> | |
| </li> | |
| """ | |
| items.append(item) | |
| return ''.join(items) | |
| def format_results(analysis_result): | |
| if "error" in analysis_result: | |
| return f"<div style='color: red'>Error: {analysis_result['error']}</div>" | |
| # Load template | |
| with open('templates/results.html', 'r') as f: | |
| template = Template(f.read()) | |
| # Prepare template variables | |
| confidence = analysis_result['confidence'] | |
| confidence_class = "confidence-high" if confidence >= 0.6 else "confidence-low" | |
| return template.substitute( | |
| confidence_class=confidence_class, | |
| confidence_score=int(confidence * 100), | |
| verification_warning=ResultFormatter._verification_warning(analysis_result), | |
| people_list=ResultFormatter.format_entity_list(analysis_result['entities'], 'people'), | |
| org_list=ResultFormatter.format_entity_list(analysis_result['entities'], 'organizations'), | |
| location_list=ResultFormatter.format_entity_list(analysis_result['entities'], 'locations'), | |
| temporal_list=ResultFormatter.format_entity_list(analysis_result['entities'], 'temporal'), | |
| hashtag_list=ResultFormatter.format_entity_list(analysis_result['entities'], 'hashtags'), | |
| entity_relationships=ResultFormatter.format_entity_relationships(analysis_result.get('entity_relationships')), | |
| validation_success=ResultFormatter._validation_success(analysis_result), | |
| related_events=ResultFormatter.format_related_events(analysis_result.get('related_events')), | |
| base_confidence=int(confidence * 70), | |
| entity_boost=int((confidence - 0.7 if confidence > 0.7 else 0) * 100), | |
| types_detected=len([t for t in ['people', 'organizations', 'locations', 'temporal', 'hashtags'] | |
| if analysis_result['entities'].get(t)]), | |
| total_entities=sum(len(e) for e in analysis_result['entities'].values() if isinstance(e, list)), | |
| direct_relationships=len(analysis_result.get('entity_relationships', [])), | |
| related_event_count=len(analysis_result.get('related_events', [])) | |
| ) | |
| def _verification_warning(result): | |
| if not result["verification_needed"]: | |
| return '' | |
| return ''' | |
| <div class="alert-warning"> | |
| ⚠ <strong>Verification Required:</strong> Low confidence score detected. Please verify the extracted information. | |
| </div> | |
| ''' | |
| def _validation_success(result): | |
| if result["verification_needed"]: | |
| return '' | |
| return ''' | |
| <div class="alert-success mt-4"> | |
| ✅ <strong>Event Validated:</strong> The extracted information meets confidence thresholds. | |
| </div> | |
| ''' |