File size: 11,678 Bytes
96a706d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""
HRHUB - Bilateral HR Matching System
Main Streamlit Application

A professional HR matching system that connects candidates with companies
using NLP embeddings and cosine similarity matching.
"""

import streamlit as st
import sys
from pathlib import Path

# Add parent directory to path for imports
sys.path.append(str(Path(__file__).parent))

from config import *
from data.data_loader import (
    load_embeddings,
    find_top_matches
)
from hrhub_project.utils.display_v2 import (
    display_candidate_profile,
    display_company_card,
    display_match_table,
    display_stats_overview
)
from utils.visualization import create_network_graph
import streamlit.components.v1 as components


def configure_page():
    """Configure Streamlit page settings and custom CSS."""
    
    st.set_page_config(
        page_title="HRHUB - HR Matching",
        page_icon="🏒",
        layout="wide",
        initial_sidebar_state="expanded"
    )
    
    # Custom CSS for better styling
    st.markdown("""
        <style>
        /* Main title styling */
        .main-title {
            font-size: 3rem;
            font-weight: bold;
            text-align: center;
            color: #0066CC;
            margin-bottom: 0;
        }
        
        .sub-title {
            font-size: 1.2rem;
            text-align: center;
            color: #666;
            margin-top: 0;
            margin-bottom: 2rem;
        }
        
        /* Section headers */
        .section-header {
            background: linear-gradient(90deg, #0066CC 0%, #00BFFF 100%);
            color: white;
            padding: 15px;
            border-radius: 10px;
            margin: 20px 0;
            font-size: 1.5rem;
            font-weight: bold;
        }
        
        /* Info boxes */
        .info-box {
            background-color: #E7F3FF;
            border-left: 5px solid #0066CC;
            padding: 15px;
            border-radius: 5px;
            margin: 10px 0;
        }
        
        /* Metric cards */
        div[data-testid="metric-container"] {
            background-color: #F8F9FA;
            border: 2px solid #E0E0E0;
            padding: 15px;
            border-radius: 10px;
        }
        
        /* Expander styling */
        .streamlit-expanderHeader {
            background-color: #F0F2F6;
            border-radius: 5px;
        }
        
        /* Hide Streamlit branding */
        #MainMenu {visibility: hidden;}
        footer {visibility: hidden;}
        
        /* Custom scrollbar */
        ::-webkit-scrollbar {
            width: 10px;
            height: 10px;
        }
        
        ::-webkit-scrollbar-track {
            background: #f1f1f1;
        }
        
        ::-webkit-scrollbar-thumb {
            background: #888;
            border-radius: 5px;
        }
        
        ::-webkit-scrollbar-thumb:hover {
            background: #555;
        }
        </style>
    """, unsafe_allow_html=True)


def render_header():
    """Render application header."""
    
    st.markdown(f'<h1 class="main-title">{APP_TITLE}</h1>', unsafe_allow_html=True)
    st.markdown(f'<p class="sub-title">{APP_SUBTITLE}</p>', unsafe_allow_html=True)


def render_sidebar():
    """Render sidebar with controls and information."""
    
    with st.sidebar:
        st.image("https://via.placeholder.com/250x80/0066CC/FFFFFF?text=HRHUB", width=250)
        
        st.markdown("---")
        
        st.markdown("### βš™οΈ Settings")
        
        # Number of matches
        top_k = st.slider(
            "Number of Matches",
            min_value=5,
            max_value=20,
            value=DEFAULT_TOP_K,
            step=5,
            help="Select how many top companies to display"
        )
        
        # Minimum score threshold
        min_score = st.slider(
            "Minimum Match Score",
            min_value=0.0,
            max_value=1.0,
            value=MIN_SIMILARITY_SCORE,
            step=0.05,
            help="Filter companies below this similarity score"
        )
        
        st.markdown("---")
        
        # View mode selection
        st.markdown("### πŸ‘€ View Mode")
        view_mode = st.radio(
            "Select view:",
            ["πŸ“Š Overview", "πŸ” Detailed Cards", "πŸ“ˆ Table View"],
            help="Choose how to display company matches"
        )
        
        st.markdown("---")
        
        # Information section
        with st.expander("ℹ️ About HRHUB", expanded=False):
            st.markdown("""
                **HRHUB** is a bilateral HR matching system that uses:
                
                - πŸ€– **NLP Embeddings**: Sentence transformers (384 dimensions)
                - πŸ“ **Cosine Similarity**: Scale-invariant matching
                - πŸŒ‰ **Job Postings Bridge**: Aligns candidate and company language
                
                **Key Innovation:**
                Companies enriched with job posting data speak the same 
                "skills language" as candidates!
            """)
        
        with st.expander("πŸ“š How to Use", expanded=False):
            st.markdown("""
                1. **View Candidate Profile**: See the candidate's skills and background
                2. **Explore Matches**: Review top company matches with scores
                3. **Network Graph**: Visualize connections interactively
                4. **Company Details**: Click to see full company information
            """)
        
        st.markdown("---")
        
        # Version info
        st.caption(f"Version: {VERSION}")
        st.caption("Β© 2024 HRHUB Team")
        
        return top_k, min_score, view_mode


def get_network_graph_data(candidate_id, matches):
    """Generate network graph data from matches."""
    nodes = []
    edges = []
    
    # Add candidate node
    nodes.append({
        'id': f'C{candidate_id}',
        'label': f'Candidate #{candidate_id}',
        'color': '#4ade80',
        'shape': 'dot',
        'size': 30
    })
    
    # Add company nodes and edges
    for comp_id, score, comp_data in matches:
        nodes.append({
            'id': f'COMP{comp_id}',
            'label': comp_data.get('name', f'Company {comp_id}')[:30],
            'color': '#ff6b6b',
            'shape': 'box',
            'size': 20
        })
        
        edges.append({
            'from': f'C{candidate_id}',
            'to': f'COMP{comp_id}',
            'value': float(score) * 10,
            'title': f'{score:.3f}'
        })
    
    return {'nodes': nodes, 'edges': edges}


def render_network_section(candidate_id: int, matches):
    """Render interactive network visualization section."""
    
    st.markdown('<div class="section-header">πŸ•ΈοΈ Network Visualization</div>', unsafe_allow_html=True)
    
    with st.spinner("Generating interactive network graph..."):
        # Get graph data
        graph_data = get_network_graph_data(candidate_id, matches)
        
        # Create HTML graph
        html_content = create_network_graph(
            nodes=graph_data['nodes'],
            edges=graph_data['edges'],
            height="600px"
        )
        
        # Display in Streamlit
        components.html(html_content, height=620, scrolling=False)
    
    # Graph instructions
    with st.expander("πŸ“– Graph Controls", expanded=False):
        st.markdown("""
            **How to interact with the graph:**
            
            - πŸ–±οΈ **Drag nodes**: Click and drag to reposition
            - πŸ” **Zoom**: Scroll to zoom in/out
            - πŸ‘† **Pan**: Click background and drag to pan
            - 🎯 **Hover**: Hover over nodes and edges for details
            
            **Legend:**
            - 🟒 **Green circles**: Candidates
            - πŸ”΄ **Red squares**: Companies
            - **Line thickness**: Match strength (thicker = better match)
        """)


def render_matches_section(matches, view_mode: str):
    """Render company matches section with different view modes."""
    
    st.markdown('<div class="section-header">🎯 Company Matches</div>', unsafe_allow_html=True)
    
    if view_mode == "πŸ“Š Overview":
        # Table view
        display_match_table(matches)
        
    elif view_mode == "πŸ” Detailed Cards":
        # Card view - detailed
        for rank, (comp_id, score, comp_data) in enumerate(matches, 1):
            display_company_card(comp_data, score, rank)
            
    elif view_mode == "πŸ“ˆ Table View":
        # Compact table
        display_match_table(matches)


def main():
    """Main application entry point."""
    
    # Configure page
    configure_page()
    
    # Render header
    render_header()
    
    # Render sidebar and get settings
    top_k, min_score, view_mode = render_sidebar()
    
    # Main content area
    st.markdown("---")
    
    # Load embeddings (cache in session state)
    if 'embeddings_loaded' not in st.session_state:
        with st.spinner("πŸ”„ Loading embeddings and data..."):
            cand_emb, comp_emb, cand_df, comp_df = load_embeddings()
            st.session_state.embeddings_loaded = True
            st.session_state.candidate_embeddings = cand_emb
            st.session_state.company_embeddings = comp_emb
            st.session_state.candidates_df = cand_df
            st.session_state.companies_df = comp_df
            st.success("βœ… Data loaded successfully!")
    
    # Load candidate data
    candidate_id = DEMO_CANDIDATE_ID
    candidate = st.session_state.candidates_df.iloc[candidate_id]
    
    # Load company matches
    matches_list = find_top_matches(
        candidate_id,
        st.session_state.candidate_embeddings,
        st.session_state.company_embeddings,
        st.session_state.companies_df,
        top_k
    )
    
    # Format matches for display
    matches = [
        (m['company_id'], m['score'], st.session_state.companies_df.iloc[m['company_id']])
        for m in matches_list
    ]
    
    # Filter by minimum score
    matches = [(cid, score, cdata) for cid, score, cdata in matches if score >= min_score]
    
    if not matches:
        st.warning(f"No matches found above {min_score:.0%} threshold. Try lowering the minimum score.")
        return
    
    # Display statistics overview
    display_stats_overview(candidate, matches)
    
    # Create two columns for layout
    col1, col2 = st.columns([1, 2])
    
    with col1:
        # Candidate profile section
        st.markdown('<div class="section-header">πŸ‘€ Candidate Profile</div>', unsafe_allow_html=True)
        display_candidate_profile(candidate)
    
    with col2:
        # Matches section
        render_matches_section(matches, view_mode)
    
    st.markdown("---")
    
    # Network visualization (full width)
    render_network_section(candidate_id, matches)
    
    st.markdown("---")
    
    # Technical info expander
    with st.expander("πŸ”§ Technical Details", expanded=False):
        st.markdown(f"""
            **Current Configuration:**
            - Embedding Dimension: {EMBEDDING_DIMENSION}
            - Similarity Metric: Cosine Similarity
            - Top K Matches: {top_k}
            - Minimum Score: {min_score:.0%}
            - Candidates Loaded: {len(st.session_state.candidates_df):,}
            - Companies Loaded: {len(st.session_state.companies_df):,}
            
            **Algorithm:**
            1. Load pre-computed embeddings (.npy files)
            2. Calculate cosine similarity
            3. Rank companies by similarity score
            4. Return top-K matches
        """)


if __name__ == "__main__":
    main()