Spaces:
Sleeping
Sleeping
modified app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
from datetime import datetime
|
|
|
|
| 5 |
|
| 6 |
# Data folder paths
|
| 7 |
DATA_BASE = "Viewer/Data/Our_system"
|
|
@@ -54,94 +55,130 @@ def render_data_as_html(data, event_name, format_type, view_type):
|
|
| 54 |
# Process clusters
|
| 55 |
clusters = process_clusters(data, format_type)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
# Render HTML con modal
|
| 58 |
-
html_parts = ['''
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
<div style="
|
| 62 |
-
<div style="background:
|
| 63 |
-
<
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
<div style="padding: 25px;">
|
| 67 |
-
<div style="margin-bottom: 20px;">
|
| 68 |
-
<div style="font-weight: 600; color: #495057; margin-bottom: 8px; font-size: 1.05em;">Context:</div>
|
| 69 |
-
<div id="modalContext" style="background: #f8f9fa; padding: 15px; border-radius: 8px; line-height: 1.6; border-left: 4px solid #007bff;"></div>
|
| 70 |
</div>
|
| 71 |
-
<div style="
|
| 72 |
-
<div style="
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
<div style="
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
</div>
|
| 79 |
</div>
|
| 80 |
</div>
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
๐ ''' + escape_html(file_name) + '''
|
| 86 |
-
</div>
|
| 87 |
-
<div style="padding: 25px;">
|
| 88 |
-
<div style="background: #f8f9fa; border-left: 4px solid #667eea; padding: 20px; margin: 20px 0; border-radius: 0 8px 8px 0; line-height: 1.6;">
|
| 89 |
-
<strong>Summary:</strong><br>''' + process_citations(escape_html(summary), summary_contexts) + '''
|
| 90 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
''']
|
| 92 |
|
| 93 |
# Render clusters
|
| 94 |
for cluster in clusters:
|
| 95 |
-
html_parts.append(render_cluster(cluster, format_type))
|
| 96 |
|
| 97 |
-
html_parts.append('''
|
|
|
|
| 98 |
</div>
|
| 99 |
</div>
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
<script>
|
| 102 |
-
(function() {
|
| 103 |
-
const
|
| 104 |
-
const
|
|
|
|
| 105 |
|
| 106 |
-
// Close
|
| 107 |
-
closeBtn.
|
| 108 |
modal.style.display = 'none';
|
| 109 |
-
};
|
| 110 |
|
| 111 |
-
// Close
|
| 112 |
-
|
| 113 |
-
if (event.target === modal) {
|
| 114 |
modal.style.display = 'none';
|
| 115 |
-
}
|
| 116 |
-
};
|
| 117 |
|
| 118 |
-
// Close
|
| 119 |
-
document.addEventListener('keydown', function(event) {
|
| 120 |
-
if (event.key === 'Escape' && modal.style.display === 'block') {
|
| 121 |
modal.style.display = 'none';
|
| 122 |
-
}
|
| 123 |
-
});
|
| 124 |
|
| 125 |
// Handle citation clicks
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
const
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
modal.style.display = 'block';
|
| 139 |
-
}
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
});
|
| 144 |
-
})();
|
| 145 |
</script>
|
| 146 |
''')
|
| 147 |
|
|
@@ -184,7 +221,7 @@ def process_clusters(data, format_type):
|
|
| 184 |
|
| 185 |
return clusters
|
| 186 |
|
| 187 |
-
def render_cluster(cluster, format_type):
|
| 188 |
"""Render a single cluster as HTML"""
|
| 189 |
header = cluster.get('cluster_headline', 'Cluster')
|
| 190 |
|
|
@@ -200,7 +237,7 @@ def render_cluster(cluster, format_type):
|
|
| 200 |
if format_type == 'summary':
|
| 201 |
summary_text = cluster.get('cluster_summary', 'No summary')
|
| 202 |
contexts = cluster.get('used_contexts', {})
|
| 203 |
-
html += f'<div style="background: #e8f5e8; border-radius: 8px; padding: 15px; line-height: 1.6;">{process_citations(escape_html(summary_text), contexts)}</div>'
|
| 204 |
else:
|
| 205 |
qas = cluster.get('questions_and_answers', [])
|
| 206 |
if not qas:
|
|
@@ -217,7 +254,7 @@ def render_cluster(cluster, format_type):
|
|
| 217 |
โ {escape_html(question)}
|
| 218 |
</div>
|
| 219 |
<div style="color: #6c757d; line-height: 1.5;">
|
| 220 |
-
{process_citations(escape_html(answer), contexts)}
|
| 221 |
</div>
|
| 222 |
</div>
|
| 223 |
'''
|
|
@@ -225,7 +262,7 @@ def render_cluster(cluster, format_type):
|
|
| 225 |
html += '</div></details></div>'
|
| 226 |
return html
|
| 227 |
|
| 228 |
-
def process_citations(text, contexts):
|
| 229 |
"""Add citation spans with popup data"""
|
| 230 |
import re
|
| 231 |
|
|
@@ -236,17 +273,11 @@ def process_citations(text, contexts):
|
|
| 236 |
if not ctx:
|
| 237 |
return match.group(0)
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
'url': ctx.get('url', '')
|
| 243 |
-
}
|
| 244 |
-
|
| 245 |
-
# URL encode the JSON data
|
| 246 |
-
import urllib.parse
|
| 247 |
-
encoded = urllib.parse.quote(json.dumps(context_data))
|
| 248 |
|
| 249 |
-
return f'<span class="citation" data-context="{
|
| 250 |
|
| 251 |
return re.sub(r'\[(\d+)\]', replace_citation, text)
|
| 252 |
|
|
@@ -288,20 +319,17 @@ def save_feedback(event, format_type, view_type, rating, comment):
|
|
| 288 |
# Get events
|
| 289 |
events = get_available_events()
|
| 290 |
|
| 291 |
-
# CSS personalizzato
|
| 292 |
custom_css = """
|
|
|
|
|
|
|
|
|
|
| 293 |
body {
|
| 294 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 295 |
}
|
| 296 |
.gradio-container {
|
| 297 |
max-width: 1200px !important;
|
| 298 |
}
|
| 299 |
-
/* Hover effect for citations */
|
| 300 |
-
.citation:hover {
|
| 301 |
-
background: #0056b3 !important;
|
| 302 |
-
transform: translateY(-1px);
|
| 303 |
-
box-shadow: 0 2px 8px rgba(0, 123, 255, 0.3);
|
| 304 |
-
}
|
| 305 |
"""
|
| 306 |
|
| 307 |
# Create Gradio interface
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
from datetime import datetime
|
| 5 |
+
import urllib.parse
|
| 6 |
|
| 7 |
# Data folder paths
|
| 8 |
DATA_BASE = "Viewer/Data/Our_system"
|
|
|
|
| 55 |
# Process clusters
|
| 56 |
clusters = process_clusters(data, format_type)
|
| 57 |
|
| 58 |
+
# Genera un ID unico per questa istanza
|
| 59 |
+
unique_id = f"viewer_{abs(hash(event_name + format_type + view_type)) % 100000}"
|
| 60 |
+
|
| 61 |
# Render HTML con modal
|
| 62 |
+
html_parts = [f'''
|
| 63 |
+
<div id="{unique_id}_container">
|
| 64 |
+
<!-- Modal for citations -->
|
| 65 |
+
<div id="{unique_id}_modal" class="citation-modal" style="display: none; position: fixed; z-index: 9999; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); backdrop-filter: blur(3px);">
|
| 66 |
+
<div class="citation-modal-content" style="background: white; margin: 5% auto; padding: 0; border-radius: 15px; width: 90%; max-width: 700px; max-height: 80vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); animation: slideIn 0.3s ease-out;">
|
| 67 |
+
<div style="background: linear-gradient(135deg, #667eea, #764ba2); color: white; padding: 20px; border-radius: 15px 15px 0 0; display: flex; justify-content: space-between; align-items: center;">
|
| 68 |
+
<h3 style="font-size: 1.2em; font-weight: 600; margin: 0;">Citation Details</h3>
|
| 69 |
+
<span class="citation-close" style="color: white; font-size: 28px; font-weight: bold; cursor: pointer; line-height: 1; transition: opacity 0.3s;">×</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
</div>
|
| 71 |
+
<div style="padding: 25px;">
|
| 72 |
+
<div style="margin-bottom: 20px;">
|
| 73 |
+
<div style="font-weight: 600; color: #495057; margin-bottom: 8px; font-size: 1.05em;">Context:</div>
|
| 74 |
+
<div class="citation-context" style="background: #f8f9fa; padding: 15px; border-radius: 8px; line-height: 1.6; border-left: 4px solid #007bff;"></div>
|
| 75 |
+
</div>
|
| 76 |
+
<div style="margin-bottom: 20px;">
|
| 77 |
+
<div style="font-weight: 600; color: #495057; margin-bottom: 8px; font-size: 1.05em;">Title:</div>
|
| 78 |
+
<div class="citation-title"></div>
|
| 79 |
+
</div>
|
| 80 |
+
<div style="margin-top: 15px;">
|
| 81 |
+
<div style="font-weight: 600; color: #495057; margin-bottom: 8px; font-size: 1.05em;">Source URL:</div>
|
| 82 |
+
<div><a class="citation-url" href="#" target="_blank" style="color: #007bff; text-decoration: none; word-break: break-all; font-size: 0.95em;"></a></div>
|
| 83 |
+
</div>
|
| 84 |
</div>
|
| 85 |
</div>
|
| 86 |
</div>
|
| 87 |
+
|
| 88 |
+
<div style="margin-bottom: 40px; border: 2px solid #e1e5e9; border-radius: 12px; overflow: hidden;">
|
| 89 |
+
<div style="background: linear-gradient(135deg, #667eea, #764ba2); color: white; padding: 20px; font-size: 1.3em; font-weight: 600;">
|
| 90 |
+
๐ {escape_html(file_name)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
</div>
|
| 92 |
+
<div style="padding: 25px;">
|
| 93 |
+
<div style="background: #f8f9fa; border-left: 4px solid #667eea; padding: 20px; margin: 20px 0; border-radius: 0 8px 8px 0; line-height: 1.6;">
|
| 94 |
+
<strong>Summary:</strong><br>{process_citations(escape_html(summary), summary_contexts, unique_id)}
|
| 95 |
+
</div>
|
| 96 |
''']
|
| 97 |
|
| 98 |
# Render clusters
|
| 99 |
for cluster in clusters:
|
| 100 |
+
html_parts.append(render_cluster(cluster, format_type, unique_id))
|
| 101 |
|
| 102 |
+
html_parts.append(f'''
|
| 103 |
+
</div>
|
| 104 |
</div>
|
| 105 |
</div>
|
| 106 |
|
| 107 |
+
<style>
|
| 108 |
+
@keyframes slideIn {{
|
| 109 |
+
from {{ transform: translateY(-50px); opacity: 0; }}
|
| 110 |
+
to {{ transform: translateY(0); opacity: 1; }}
|
| 111 |
+
}}
|
| 112 |
+
.citation-close:hover {{
|
| 113 |
+
opacity: 0.7;
|
| 114 |
+
}}
|
| 115 |
+
.citation-badge {{
|
| 116 |
+
display: inline;
|
| 117 |
+
background: #007bff;
|
| 118 |
+
color: white;
|
| 119 |
+
padding: 2px 6px;
|
| 120 |
+
border-radius: 4px;
|
| 121 |
+
font-size: 0.85em;
|
| 122 |
+
font-weight: 600;
|
| 123 |
+
cursor: pointer;
|
| 124 |
+
margin: 0 2px;
|
| 125 |
+
transition: all 0.3s ease;
|
| 126 |
+
}}
|
| 127 |
+
.citation-badge:hover {{
|
| 128 |
+
background: #0056b3;
|
| 129 |
+
transform: translateY(-1px);
|
| 130 |
+
box-shadow: 0 2px 8px rgba(0, 123, 255, 0.3);
|
| 131 |
+
}}
|
| 132 |
+
</style>
|
| 133 |
+
|
| 134 |
<script>
|
| 135 |
+
(function() {{
|
| 136 |
+
const container = document.getElementById('{unique_id}_container');
|
| 137 |
+
const modal = container.querySelector('#{unique_id}_modal');
|
| 138 |
+
const closeBtn = modal.querySelector('.citation-close');
|
| 139 |
|
| 140 |
+
// Close on X button
|
| 141 |
+
closeBtn.addEventListener('click', function() {{
|
| 142 |
modal.style.display = 'none';
|
| 143 |
+
}});
|
| 144 |
|
| 145 |
+
// Close on outside click
|
| 146 |
+
modal.addEventListener('click', function(event) {{
|
| 147 |
+
if (event.target === modal) {{
|
| 148 |
modal.style.display = 'none';
|
| 149 |
+
}}
|
| 150 |
+
}});
|
| 151 |
|
| 152 |
+
// Close on ESC
|
| 153 |
+
document.addEventListener('keydown', function(event) {{
|
| 154 |
+
if (event.key === 'Escape' && modal.style.display === 'block') {{
|
| 155 |
modal.style.display = 'none';
|
| 156 |
+
}}
|
| 157 |
+
}});
|
| 158 |
|
| 159 |
// Handle citation clicks
|
| 160 |
+
container.addEventListener('click', function(event) {{
|
| 161 |
+
const target = event.target;
|
| 162 |
+
if (target.classList && target.classList.contains('citation-badge')) {{
|
| 163 |
+
const contextData = target.getAttribute('data-context');
|
| 164 |
+
const titleData = target.getAttribute('data-title');
|
| 165 |
+
const urlData = target.getAttribute('data-url');
|
| 166 |
|
| 167 |
+
if (contextData || titleData || urlData) {{
|
| 168 |
+
const contextEl = modal.querySelector('.citation-context');
|
| 169 |
+
const titleEl = modal.querySelector('.citation-title');
|
| 170 |
+
const urlEl = modal.querySelector('.citation-url');
|
| 171 |
+
|
| 172 |
+
contextEl.textContent = decodeURIComponent(contextData || 'No context available.');
|
| 173 |
+
titleEl.textContent = decodeURIComponent(titleData || 'No title available.');
|
| 174 |
+
urlEl.textContent = decodeURIComponent(urlData || 'No URL provided.');
|
| 175 |
+
urlEl.href = decodeURIComponent(urlData || '#');
|
| 176 |
+
|
| 177 |
modal.style.display = 'block';
|
| 178 |
+
}}
|
| 179 |
+
}}
|
| 180 |
+
}});
|
| 181 |
+
}})();
|
|
|
|
|
|
|
| 182 |
</script>
|
| 183 |
''')
|
| 184 |
|
|
|
|
| 221 |
|
| 222 |
return clusters
|
| 223 |
|
| 224 |
+
def render_cluster(cluster, format_type, unique_id):
|
| 225 |
"""Render a single cluster as HTML"""
|
| 226 |
header = cluster.get('cluster_headline', 'Cluster')
|
| 227 |
|
|
|
|
| 237 |
if format_type == 'summary':
|
| 238 |
summary_text = cluster.get('cluster_summary', 'No summary')
|
| 239 |
contexts = cluster.get('used_contexts', {})
|
| 240 |
+
html += f'<div style="background: #e8f5e8; border-radius: 8px; padding: 15px; line-height: 1.6;">{process_citations(escape_html(summary_text), contexts, unique_id)}</div>'
|
| 241 |
else:
|
| 242 |
qas = cluster.get('questions_and_answers', [])
|
| 243 |
if not qas:
|
|
|
|
| 254 |
โ {escape_html(question)}
|
| 255 |
</div>
|
| 256 |
<div style="color: #6c757d; line-height: 1.5;">
|
| 257 |
+
{process_citations(escape_html(answer), contexts, unique_id)}
|
| 258 |
</div>
|
| 259 |
</div>
|
| 260 |
'''
|
|
|
|
| 262 |
html += '</div></details></div>'
|
| 263 |
return html
|
| 264 |
|
| 265 |
+
def process_citations(text, contexts, unique_id):
|
| 266 |
"""Add citation spans with popup data"""
|
| 267 |
import re
|
| 268 |
|
|
|
|
| 273 |
if not ctx:
|
| 274 |
return match.group(0)
|
| 275 |
|
| 276 |
+
context_text = urllib.parse.quote(ctx.get('context', '')[:500])
|
| 277 |
+
title_text = urllib.parse.quote(ctx.get('title', ''))
|
| 278 |
+
url_text = urllib.parse.quote(ctx.get('url', ''))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
+
return f'<span class="citation-badge" data-context="{context_text}" data-title="{title_text}" data-url="{url_text}">[{cit_id}]</span>'
|
| 281 |
|
| 282 |
return re.sub(r'\[(\d+)\]', replace_citation, text)
|
| 283 |
|
|
|
|
| 319 |
# Get events
|
| 320 |
events = get_available_events()
|
| 321 |
|
| 322 |
+
# CSS personalizzato con font originale
|
| 323 |
custom_css = """
|
| 324 |
+
* {
|
| 325 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
|
| 326 |
+
}
|
| 327 |
body {
|
| 328 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 329 |
}
|
| 330 |
.gradio-container {
|
| 331 |
max-width: 1200px !important;
|
| 332 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
"""
|
| 334 |
|
| 335 |
# Create Gradio interface
|