LLM-ChatBot-Document / utils /ui_components.py
FauzanAriyatmoko's picture
main : fixing pdf viewer feature
f814ba2
"""
UI Components and styling for Gradio interface
"""
# Custom CSS for premium design
CUSTOM_CSS = """
/* Main theme */
:root {
--primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
--success-gradient: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
--card-bg: rgba(255, 255, 255, 0.05);
--glass-bg: rgba(255, 255, 255, 0.1);
}
/* Header styling */
.header-container {
background: var(--primary-gradient);
padding: 2rem;
border-radius: 12px;
margin-bottom: 1.5rem;
box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
}
.header-title {
color: white;
font-size: 2.5rem;
font-weight: 700;
text-align: center;
margin-bottom: 0.5rem;
}
.header-subtitle {
color: rgba(255, 255, 255, 0.9);
text-align: center;
font-size: 1.1rem;
}
/* Tab styling */
.tab-nav button {
font-size: 1rem;
font-weight: 600;
padding: 0.75rem 1.5rem;
border-radius: 8px;
transition: all 0.3s ease;
}
.tab-nav button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
/* Card styling */
.info-card {
background: var(--card-bg);
backdrop-filter: blur(10px);
border-radius: 12px;
padding: 1.5rem;
margin: 1rem 0;
border: 1px solid rgba(255, 255, 255, 0.1);
}
/* Upload area */
.upload-area {
border: 2px dashed rgba(102, 126, 234, 0.5);
border-radius: 12px;
padding: 2rem;
text-align: center;
transition: all 0.3s ease;
}
.upload-area:hover {
border-color: #667eea;
background: rgba(102, 126, 234, 0.05);
}
/* Chat messages */
.message-bubble {
border-radius: 18px;
padding: 0.75rem 1rem;
margin: 0.5rem 0;
animation: slideIn 0.3s ease;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Source citations */
.source-citation {
background: var(--glass-bg);
border-left: 3px solid #667eea;
padding: 0.75rem;
margin: 0.5rem 0;
border-radius: 6px;
font-size: 0.9rem;
}
/* Buttons */
.primary-button {
background: var(--primary-gradient) !important;
color: white !important;
border: none !important;
padding: 0.75rem 2rem !important;
border-radius: 8px !important;
font-weight: 600 !important;
transition: all 0.3s ease !important;
}
.primary-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4) !important;
}
/* Status indicators */
.status-success {
color: #38ef7d;
font-weight: 600;
}
.status-error {
color: #ff6b6b;
font-weight: 600;
}
/* Loading animation */
.loading {
display: inline-block;
animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
/* Document cards */
.doc-card {
background: var(--glass-bg);
border-radius: 12px;
padding: 1rem;
margin: 0.75rem 0;
border: 1px solid rgba(255, 255, 255, 0.1);
transition: all 0.3s ease;
}
.doc-card:hover {
transform: translateX(5px);
border-color: #667eea;
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}
/* Microphone button and recording styles */
.mic-button {
background: var(--primary-gradient) !important;
color: white !important;
border: none !important;
border-radius: 8px !important;
padding: 0.75rem 1.5rem !important;
font-weight: 600 !important;
transition: all 0.3s ease !important;
position: relative !important;
}
.mic-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4) !important;
}
.mic-recording {
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%) !important;
animation: pulse-recording 1.5s ease-in-out infinite !important;
}
@keyframes pulse-recording {
0%, 100% {
opacity: 1;
box-shadow: 0 0 0 0 rgba(255, 107, 107, 0.7);
}
50% {
opacity: 0.9;
box-shadow: 0 0 0 10px rgba(255, 107, 107, 0);
}
}
.recording-indicator {
display: inline-block;
width: 8px;
height: 8px;
background: #ff6b6b;
border-radius: 50%;
margin-right: 0.5rem;
animation: blink 1s ease-in-out infinite;
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0.3; }
}
.audio-status {
background: rgba(102, 126, 234, 0.1);
border-left: 3px solid #667eea;
padding: 0.75rem 1rem;
border-radius: 6px;
margin: 0.5rem 0;
font-size: 0.9rem;
}
.audio-status.processing {
border-left-color: #ffd93d;
background: rgba(255, 217, 61, 0.1);
}
.audio-status.error {
border-left-color: #ff6b6b;
background: rgba(255, 107, 107, 0.1);
}
.audio-status.success {
border-left-color: #38ef7d;
background: rgba(56, 239, 125, 0.1);
}
/* Responsive */
@media (max-width: 768px) {
.header-title {
font-size: 2rem;
}
.tab-nav button {
font-size: 0.9rem;
padding: 0.5rem 1rem;
}
}
/* ========== Document Viewer Styles ========== */
/* Document viewer container */
.document-viewer-wrapper {
height: 600px;
display: flex;
flex-direction: column;
background: var(--card-bg);
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
overflow: hidden;
}
.viewer-controls {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
background: rgba(102, 126, 234, 0.1);
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.viewer-title {
display: flex;
align-items: center;
gap: 0.5rem;
font-weight: 600;
color: #667eea;
}
.doc-icon {
font-size: 1.2rem;
}
.doc-name {
font-size: 0.95rem;
}
.viewer-actions {
display: flex;
gap: 0.5rem;
}
.viewer-btn {
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 6px;
padding: 0.4rem 0.8rem;
cursor: pointer;
transition: all 0.2s ease;
font-size: 0.9rem;
}
.viewer-btn:hover {
background: rgba(102, 126, 234, 0.3);
border-color: #667eea;
transform: translateY(-1px);
}
/* Document content area */
.document-viewer-content {
flex: 1;
overflow-y: auto;
padding: 1.5rem;
font-size: 1em;
line-height: 1.6;
}
.document-content {
max-width: 800px;
margin: 0 auto;
}
.document-header {
text-align: center;
margin-bottom: 2rem;
padding-bottom: 1rem;
border-bottom: 2px solid rgba(102, 126, 234, 0.3);
}
.document-header h3 {
color: #667eea;
margin: 0 0 0.5rem 0;
font-size: 1.5rem;
}
.doc-meta {
color: rgba(255, 255, 255, 0.6);
font-size: 0.9rem;
margin: 0;
}
/* PDF pages */
.pdf-page {
margin-bottom: 2rem;
padding: 1.5rem;
background: rgba(255, 255, 255, 0.02);
border-radius: 8px;
border: 1px solid rgba(255, 255, 255, 0.05);
}
.page-number {
font-size: 0.85rem;
color: rgba(255, 255, 255, 0.5);
font-weight: 600;
margin-bottom: 1rem;
padding-bottom: 0.5rem;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
text-align: center;
}
/* Paragraphs */
.paragraph {
margin-bottom: 1rem;
padding: 0.75rem;
border-radius: 6px;
transition: all 0.3s ease;
cursor: default;
}
.paragraph:hover {
background: rgba(255, 255, 255, 0.03);
}
/* Highlighted citations */
.highlighted-citation {
background: rgba(102, 126, 234, 0.2) !important;
border-left: 4px solid #667eea !important;
padding-left: 1rem !important;
box-shadow: 0 2px 12px rgba(102, 126, 234, 0.3);
}
@keyframes highlight-flash {
0%, 100% {
background: rgba(102, 126, 234, 0.2);
}
50% {
background: rgba(102, 126, 234, 0.4);
}
}
/* Empty state */
.document-viewer-empty {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
padding: 2rem;
color: rgba(255, 255, 255, 0.6);
}
.empty-state-icon {
font-size: 4rem;
margin-bottom: 1rem;
opacity: 0.5;
}
.document-viewer-empty h3 {
color: rgba(255, 255, 255, 0.7);
margin: 0 0 0.5rem 0;
}
.document-viewer-empty p {
color: rgba(255, 255, 255, 0.5);
margin: 0;
}
/* ========== Citation Styles ========== */
.sources-container {
margin-top: 1rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.02);
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
}
.sources-title {
color: #667eea;
margin: 0 0 1rem 0;
font-size: 1.1rem;
font-weight: 600;
}
.citation-card {
background: var(--glass-bg);
border-left: 3px solid #667eea;
padding: 0.75rem 1rem;
margin: 0.75rem 0;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s ease;
animation: slideIn 0.3s ease;
}
.citation-card:hover {
background: rgba(102, 126, 234, 0.15);
border-left-color: #764ba2;
transform: translateX(5px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}
.citation-header {
color: #667eea;
font-size: 0.95rem;
margin-bottom: 0.5rem;
}
.citation-snippet {
color: rgba(255, 255, 255, 0.7);
font-size: 0.85rem;
font-style: italic;
line-height: 1.4;
}
/* Document selector */
.document-selector {
margin-bottom: 1rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.05);
border-radius: 8px;
}
.document-selector label {
display: block;
color: rgba(255, 255, 255, 0.8);
font-weight: 600;
margin-bottom: 0.5rem;
font-size: 0.9rem;
}
.doc-select-dropdown {
width: 100%;
padding: 0.75rem;
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 8px;
color: white;
font-size: 0.95rem;
cursor: pointer;
transition: all 0.3s ease;
}
.doc-select-dropdown:hover {
background: rgba(255, 255, 255, 0.15);
border-color: #667eea;
}
.doc-select-dropdown:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}
.doc-select-dropdown option {
background: #1a1a2e;
color: white;
}
.no-docs-message {
color: rgba(255, 255, 255, 0.5);
text-align: center;
padding: 1rem;
font-style: italic;
}
/* Scrollbar styling for document viewer */
.document-viewer-content::-webkit-scrollbar {
width: 8px;
}
.document-viewer-content::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.05);
border-radius: 4px;
}
.document-viewer-content::-webkit-scrollbar-thumb {
background: rgba(102, 126, 234, 0.5);
border-radius: 4px;
}
.document-viewer-content::-webkit-scrollbar-thumb:hover {
background: rgba(102, 126, 234, 0.7);
}
"""
def format_sources(sources: list) -> str:
"""
Format source citations for display
Args:
sources: List of source metadata
Returns:
Formatted HTML string
"""
if not sources:
return ""
html = "<div style='margin-top: 1rem; padding-top: 1rem; border-top: 1px solid rgba(255,255,255,0.1);'>"
for i, source in enumerate(sources, 1):
filename = source.get('filename', 'Unknown')
chunk_idx = source.get('chunk_index', 0)
preview = source.get('chunk_text', '')[:150]
html += f"""
<div class='source-citation'>
<strong>#{i} {filename}</strong> (Chunk {chunk_idx})
<br><span style='color: rgba(255,255,255,0.7); font-size: 0.85rem;'>{preview}...</span>
</div>
"""
html += "</div>"
return html
def format_file_size(size_bytes: int) -> str:
"""Format file size in human-readable format"""
for unit in ['B', 'KB', 'MB', 'GB']:
if size_bytes < 1024.0:
return f"{size_bytes:.1f} {unit}"
size_bytes /= 1024.0
return f"{size_bytes:.1f} TB"
def create_document_card(doc_info: dict) -> str:
"""
Create HTML card for document display
Args:
doc_info: Document information dictionary
Returns:
HTML string
"""
filename = doc_info.get('filename', 'Unknown')
num_chunks = doc_info.get('num_chunks', 0)
html = f"""
<div class='doc-card'>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<div>
<h4 style='margin: 0; color: #667eea;'>📄 {filename}</h4>
<p style='margin: 0.25rem 0 0 0; color: rgba(255,255,255,0.7); font-size: 0.9rem;'>
{num_chunks} chunks
</p>
</div>
</div>
</div>
"""
return html
def create_status_message(message: str, status_type: str = "info") -> str:
"""
Create styled status message
Args:
message: Status message text
status_type: Type of status (success, error, info, warning)
Returns:
HTML string
"""
icons = {
"success": "✓",
"error": "✗",
"info": "ℹ",
"warning": "⚠"
}
colors = {
"success": "#38ef7d",
"error": "#ff6b6b",
"info": "#667eea",
"warning": "#ffd93d"
}
icon = icons.get(status_type, "ℹ")
color = colors.get(status_type, "#667eea")
html = f"""
<div style='padding: 1rem; border-radius: 8px; background: rgba(255,255,255,0.05);
border-left: 4px solid {color}; margin: 1rem 0;'>
<span style='color: {color}; font-weight: 600; font-size: 1.1rem;'>{icon} {message}</span>
</div>
"""
return html