-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
648 lines (532 loc) · 26.5 KB
/
streamlit_app.py
File metadata and controls
648 lines (532 loc) · 26.5 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
"""
LinkedIn Sourcing Agent - Streamlit Web Application
Professional web interface for candidate sourcing and outreach generation
"""
import streamlit as st
import pandas as pd
import json
import asyncio
from datetime import datetime
import plotly.express as px
import plotly.graph_objects as go
from typing import Dict, List, Any
import io
import base64
# Import your LinkedIn Sourcing Agent with robust fallback
import sys
import os
import subprocess
# For Streamlit Cloud deployment, try to use real model first, fallback to demo
DEMO_MODE = False # Set to True to force demo mode, False to try real model first
# Try importing the package only if not in demo mode
LinkedInSourcingAgent = None
OutreachGenerator = None
setup_logging = None
import_success = False
if not DEMO_MODE:
# Try to import the actual package (for local development and cloud deployment)
try:
from linkedin_sourcing_agent.core.agent import LinkedInSourcingAgent
from linkedin_sourcing_agent.generators.outreach_generator import OutreachGenerator
from linkedin_sourcing_agent.utils.logging_config import setup_logging
import_success = True
st.success("✅ LinkedIn Sourcing Agent package loaded successfully!")
except ImportError as e:
st.warning(f"⚠️ Could not import LinkedIn Sourcing Agent package: {str(e)}")
st.info("🔄 Falling back to Demo Mode with realistic sample data")
DEMO_MODE = True
except Exception as e:
st.error(f"❌ Error loading LinkedIn Sourcing Agent package: {str(e)}")
st.info("🔄 Falling back to Demo Mode with realistic sample data")
DEMO_MODE = True
if DEMO_MODE or not import_success:
# Demo mode - create mock classes
st.info("🚀 Running in Demo Mode with realistic sample data!")
import random
class MockLinkedInSourcingAgent:
def __init__(self):
pass
async def search_candidates(self, query, location=None, limit=10):
# Return demo candidates with diverse, realistic data
demo_candidates = []
companies = ['Google', 'Microsoft', 'Apple', 'Amazon', 'Meta', 'Netflix', 'Tesla', 'Uber', 'Airbnb', 'Spotify', 'Stripe', 'Square', 'Dropbox', 'Slack', 'Zoom']
default_locations = ['San Francisco, CA', 'New York, NY', 'Seattle, WA', 'Austin, TX', 'Boston, MA', 'Chicago, IL', 'Los Angeles, CA', 'Denver, CO', 'Remote']
skills_pool = ['Python', 'JavaScript', 'React', 'Node.js', 'AWS', 'Docker', 'Kubernetes', 'Machine Learning', 'Data Science', 'SQL', 'MongoDB', 'Redis', 'GraphQL', 'TypeScript', 'Go', 'Rust', 'PostgreSQL', 'TensorFlow', 'PyTorch']
# Diverse names for realistic demo data
first_names = ['Sarah', 'Michael', 'Emily', 'David', 'Jessica', 'Christopher', 'Amanda', 'Daniel', 'Ashley', 'Matthew', 'Jennifer', 'Andrew', 'Emma', 'Joshua', 'Madison', 'Ryan', 'Olivia', 'James', 'Sophia', 'William']
last_names = ['Chen', 'Rodriguez', 'Kim', 'Johnson', 'Williams', 'Brown', 'Davis', 'Miller', 'Wilson', 'Moore', 'Taylor', 'Anderson', 'Thomas', 'Jackson', 'White', 'Harris', 'Martin', 'Thompson', 'Garcia', 'Martinez']
# Job titles based on query
job_titles = [
f'Senior {query.split()[0] if query else "Software"} Engineer',
f'{query.split()[0] if query else "Software"} Engineer',
f'Lead {query.split()[0] if query else "Software"} Developer',
f'Principal {query.split()[0] if query else "Software"} Engineer',
f'Staff {query.split()[0] if query else "Software"} Engineer'
]
education_options = [
'BS Computer Science - Stanford University',
'MS Software Engineering - MIT',
'BS Information Technology - UC Berkeley',
'PhD Computer Science - Carnegie Mellon',
'MS Computer Science - Georgia Tech',
'BS Software Engineering - University of Washington',
'MS Data Science - Columbia University',
'BS Computer Engineering - Caltech'
]
for i in range(min(limit, 20)): # Allow up to 20 candidates
# Generate unique combinations
first_name = random.choice(first_names)
last_name = random.choice(last_names)
full_name = f'{first_name} {last_name}'
# Use provided location or pick from defaults
candidate_location = location or random.choice(default_locations)
demo_candidates.append({
'name': full_name,
'headline': random.choice(job_titles),
'current_company': random.choice(companies),
'location': candidate_location,
'linkedin_url': f'https://linkedin.com/in/{first_name.lower()}-{last_name.lower()}-{random.randint(100, 999)}',
'skills': random.sample(skills_pool, random.randint(5, 10)),
'experience_years': random.randint(2, 15),
'education': random.choice(education_options)
})
return demo_candidates
async def score_candidate(self, candidate, job_description):
# Generate realistic fit scores
base_score = random.uniform(6.5, 9.8)
# Add logic based on candidate data
if 'Senior' in candidate.get('headline', ''):
base_score += 0.3
if candidate.get('experience_years', 0) > 7:
base_score += 0.2
candidate['fit_score'] = round(min(base_score, 10.0), 1)
candidate['score_breakdown'] = {
'skills_match': round(random.uniform(7.0, 9.5), 1),
'experience_level': round(random.uniform(7.5, 9.8), 1),
'location_preference': round(random.uniform(8.0, 10.0), 1),
'culture_fit': round(random.uniform(7.0, 9.2), 1)
}
return candidate
class MockOutreachGenerator:
def __init__(self, use_ai=True):
self.use_ai = use_ai
async def generate_message(self, candidate, job_description):
name = candidate.get('name', 'there')
company = candidate.get('current_company', 'your current company')
headline = candidate.get('headline', 'professional background')
skills = candidate.get('skills', [])
top_skills = ', '.join(skills[:3]) if skills else 'your technical skills'
templates = [
f"""Hi {name},
I hope this message finds you well! I came across your profile and was impressed by your experience as a {headline} at {company}.
Your expertise in {top_skills} aligns perfectly with what we're looking for in our team. We're currently seeking talented professionals for an exciting opportunity that could be a great fit for your career growth.
Would you be open to a brief conversation to learn more about this opportunity?
Best regards,
Sarah Chen
Senior Technical Recruiter""",
f"""Hello {name},
Your background as a {headline} at {company} caught my attention, and I'd love to connect about a role that might interest you.
We're building an innovative team and looking for someone with your skill set, particularly your experience with {top_skills}. The position offers excellent growth opportunities and the chance to work on cutting-edge projects.
Are you currently open to exploring new opportunities? I'd be happy to share more details.
Best,
Michael Rodriguez
Talent Acquisition Manager""",
f"""Hi {name},
I hope you're doing well! I noticed your impressive work as a {headline} at {company} and wanted to reach out about an opportunity that might align with your career goals.
Given your strong background in {top_skills}, I think you'd be an excellent fit for a role we're currently filling. The company culture and technical challenges would be right up your alley.
Would you be interested in hearing more details? I'd love to set up a quick call.
Best regards,
Jennifer Liu
Technical Recruiter"""
]
message = random.choice(templates)
return {
'message': message,
'confidence': random.choice(['High', 'Very High']),
'personalization_score': round(random.uniform(8.5, 9.8), 1),
'template_used': f'Professional Template {random.randint(1, 3)}',
'estimated_response_rate': f"{random.randint(25, 45)}%"
}
# Use mock classes
LinkedInSourcingAgent = MockLinkedInSourcingAgent
OutreachGenerator = MockOutreachGenerator
def setup_logging():
pass
# Setup logging
setup_logging()
# Page configuration
st.set_page_config(
page_title="LinkedIn Sourcing Agent",
page_icon="🎯",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS
st.markdown("""
<style>
.main-header {
font-size: 3rem;
font-weight: bold;
color: #0066cc;
text-align: center;
margin-bottom: 2rem;
}
.metric-card {
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
padding: 1rem;
border-radius: 10px;
color: white;
text-align: center;
margin: 0.5rem 0;
}
.candidate-card {
border: 1px solid #e0e0e0;
border-radius: 10px;
padding: 1rem;
margin: 1rem 0;
background: #f9f9f9;
}
.score-bar {
height: 20px;
border-radius: 10px;
background: linear-gradient(90deg, #ff6b6b 0%, #ffd93d 50%, #6bcf7f 100%);
}
</style>
""", unsafe_allow_html=True)
# Initialize session state
if 'agent' not in st.session_state:
st.session_state.agent = None
if 'candidates' not in st.session_state:
st.session_state.candidates = []
if 'search_history' not in st.session_state:
st.session_state.search_history = []
if 'agent_initialized' not in st.session_state:
st.session_state.agent_initialized = False
# Auto-initialize agent on first load
if not st.session_state.agent_initialized:
try:
with st.spinner("Initializing LinkedIn Sourcing Agent..."):
st.session_state.agent = LinkedInSourcingAgent()
st.session_state.agent_initialized = True
if import_success:
st.success("✅ Agent initialized successfully with full functionality!")
else:
st.success("✅ Agent initialized in demo mode!")
st.info("💡 Demo mode provides realistic sample data for testing the interface.")
except Exception as e:
st.error(f"Failed to auto-initialize agent: {str(e)}")
st.info("Please manually initialize the agent using the sidebar button.")
# Header
st.markdown('<h1 class="main-header">🎯 LinkedIn Sourcing Agent</h1>', unsafe_allow_html=True)
st.markdown("**Professional candidate sourcing and outreach automation powered by AI**")
# Sidebar
with st.sidebar:
st.header("🔧 Configuration")
# API Keys
st.subheader("API Keys")
gemini_key = st.text_input("Google Gemini API Key", type="password", help="Optional: For AI-powered outreach")
openai_key = st.text_input("OpenAI API Key", type="password", help="Optional: Alternative AI provider")
linkedin_key = st.text_input("LinkedIn API Key", type="password", help="Optional: Will use demo data if not provided")
# Search Configuration
st.subheader("Search Settings")
max_candidates = st.slider("Max Candidates", 5, 50, 10)
include_outreach = st.checkbox("Generate Outreach Messages", True)
export_excel = st.checkbox("Auto-export to Excel", True)
# Initialize Agent
if st.button("🚀 Initialize Agent", type="primary"):
try:
with st.spinner("Initializing LinkedIn Sourcing Agent..."):
st.session_state.agent = LinkedInSourcingAgent()
st.success("Agent initialized successfully!")
except Exception as e:
st.error(f"Failed to initialize agent: {str(e)}")
# Main content
tab1, tab2, tab3, tab4 = st.tabs(["🔍 Search", "📊 Results", "💌 Outreach", "📈 Analytics"])
with tab1:
st.header("🔍 Candidate Search")
col1, col2 = st.columns([2, 1])
with col1:
job_description = st.text_area(
"Job Description",
placeholder="Enter the complete job description here...",
height=200,
help="Provide detailed job requirements for better candidate matching"
)
search_query = st.text_input(
"Search Keywords",
placeholder="e.g., Python Developer, Machine Learning Engineer",
help="Key skills and job titles to search for"
)
with col2:
location = st.text_input(
"Location",
placeholder="e.g., San Francisco, Remote",
help="Geographic preference for candidates"
)
experience_level = st.selectbox(
"Experience Level",
["Any", "Entry Level", "Mid Level", "Senior", "Lead/Principal", "Executive"]
)
industry = st.selectbox(
"Industry",
["Any", "Technology", "Healthcare", "Finance", "Startup", "Enterprise"]
)
# Search button
if st.button("🔍 Search Candidates", type="primary", disabled=not st.session_state.agent):
if not job_description and not search_query:
st.warning("Please provide either a job description or search keywords.")
else:
try:
with st.spinner("Searching for candidates... This may take a few moments."):
# Simulate async call
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
query = search_query if search_query else job_description[:100]
candidates = loop.run_until_complete(
st.session_state.agent.search_candidates(
query=query,
location=location,
limit=max_candidates
)
)
# Score candidates
scored_candidates = []
for candidate in candidates:
scored_candidate = loop.run_until_complete(
st.session_state.agent.score_candidate(candidate, job_description)
)
scored_candidates.append(scored_candidate)
# Sort by fit score
scored_candidates.sort(key=lambda x: x.get('fit_score', 0), reverse=True)
st.session_state.candidates = scored_candidates
st.session_state.search_history.append({
'timestamp': datetime.now(),
'query': query,
'location': location,
'results': len(scored_candidates)
})
loop.close()
st.success(f"Found {len(st.session_state.candidates)} candidates!")
st.rerun()
except Exception as e:
st.error(f"Search failed: {str(e)}")
with tab2:
st.header("📊 Search Results")
if st.session_state.candidates:
# Summary metrics
col1, col2, col3, col4 = st.columns(4)
avg_score = sum(c.get('fit_score', 0) for c in st.session_state.candidates) / len(st.session_state.candidates)
high_score_count = sum(1 for c in st.session_state.candidates if c.get('fit_score', 0) >= 8.0)
with col1:
st.metric("Total Candidates", len(st.session_state.candidates))
with col2:
st.metric("Average Fit Score", f"{avg_score:.1f}")
with col3:
st.metric("High Score (8.0+)", high_score_count)
with col4:
st.metric("Top Candidate Score", f"{max(c.get('fit_score', 0) for c in st.session_state.candidates):.1f}")
# Score distribution chart
scores = [c.get('fit_score', 0) for c in st.session_state.candidates]
fig = px.histogram(
x=scores,
nbins=10,
title="Candidate Score Distribution",
labels={'x': 'Fit Score', 'y': 'Number of Candidates'}
)
st.plotly_chart(fig, use_container_width=True, key="score_distribution_chart")
# Candidate list
st.subheader("Candidate Details")
for i, candidate in enumerate(st.session_state.candidates[:10]): # Show top 10
with st.expander(f"#{i+1} {candidate.get('name', 'Unknown')} - Score: {candidate.get('fit_score', 0):.1f}"):
col1, col2 = st.columns([2, 1])
with col1:
st.write(f"**Company:** {candidate.get('current_company', 'N/A')}")
st.write(f"**Title:** {candidate.get('headline', 'N/A')}")
st.write(f"**Location:** {candidate.get('location', 'N/A')}")
if candidate.get('linkedin_url'):
st.markdown(f"[LinkedIn Profile]({candidate['linkedin_url']})")
# Skills
if candidate.get('skills'):
st.write("**Skills:**")
skills_str = ", ".join(candidate['skills'][:8]) # Show first 8 skills
st.write(skills_str)
with col2:
# Score visualization
score = candidate.get('fit_score', 0)
fig = go.Figure(go.Indicator(
mode = "gauge+number",
value = score,
domain = {'x': [0, 1], 'y': [0, 1]},
title = {'text': "Fit Score"},
gauge = {
'axis': {'range': [None, 10]},
'bar': {'color': "darkblue"},
'steps': [
{'range': [0, 5], 'color': "lightgray"},
{'range': [5, 8], 'color': "yellow"},
{'range': [8, 10], 'color': "green"}
],
'threshold': {
'line': {'color': "red", 'width': 4},
'thickness': 0.75,
'value': 9
}
}
))
fig.update_layout(height=200)
st.plotly_chart(fig, use_container_width=True, key=f"candidate_gauge_{i}")
# Export options
st.subheader("📥 Export Options")
col1, col2, col3 = st.columns(3)
with col1:
if st.button("📊 Download Excel"):
try:
# Export to Excel (simplified version)
df = pd.DataFrame(st.session_state.candidates)
excel_buffer = io.BytesIO()
df.to_excel(excel_buffer, index=False)
excel_buffer.seek(0)
st.download_button(
label="💾 Download Excel File",
data=excel_buffer,
file_name=f"candidates_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
except Exception as e:
st.error(f"Export failed: {str(e)}")
with col2:
if st.button("📄 Download JSON"):
json_str = json.dumps(st.session_state.candidates, indent=2)
st.download_button(
label="💾 Download JSON File",
data=json_str,
file_name=f"candidates_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
mime="application/json"
)
with col3:
if st.button("📋 Copy to Clipboard"):
# Create summary text
summary = f"LinkedIn Sourcing Results - {len(st.session_state.candidates)} candidates\n\n"
for i, c in enumerate(st.session_state.candidates[:5]): # Top 5
summary += f"{i+1}. {c.get('name', 'Unknown')} - {c.get('current_company', 'N/A')} - Score: {c.get('fit_score', 0):.1f}\n"
st.text_area("Summary (copy this)", summary, height=200)
else:
st.info("No candidates found. Please run a search first.")
with tab3:
st.header("💌 Outreach Messages")
if st.session_state.candidates:
# Outreach generation
st.subheader("Generate Personalized Messages")
selected_candidate = st.selectbox(
"Select Candidate",
options=range(len(st.session_state.candidates)),
format_func=lambda x: f"{st.session_state.candidates[x].get('name', 'Unknown')} - {st.session_state.candidates[x].get('current_company', 'N/A')}"
)
message_type = st.selectbox(
"Message Type",
["Professional Introduction", "Job Opportunity", "Networking", "Custom"]
)
custom_notes = st.text_area(
"Additional Notes",
placeholder="Any specific points to mention in the outreach...",
help="These will be incorporated into the personalized message"
)
if st.button("✨ Generate Outreach Message"):
try:
candidate = st.session_state.candidates[selected_candidate]
with st.spinner("Generating personalized outreach message..."):
# Simulate outreach generation
outreach_generator = OutreachGenerator(use_ai=True)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
job_desc = f"We're looking for talented professionals like you. {custom_notes}"
message_result = loop.run_until_complete(
outreach_generator.generate_message(candidate, job_desc)
)
loop.close()
st.success("Message generated successfully!")
# Display the message
st.subheader("Generated Message")
message = message_result.get('message', 'Message generation failed')
st.text_area("Outreach Message", message, height=200)
# Message analytics
col1, col2 = st.columns(2)
with col1:
st.metric("Confidence Level", message_result.get('confidence', 'Medium'))
with col2:
st.metric("Message Length", f"{len(message)} chars")
# Copy button
if st.button("📋 Copy Message"):
st.code(message)
st.success("Message ready to copy!")
except Exception as e:
st.error(f"Failed to generate message: {str(e)}")
else:
st.info("No candidates available. Please run a search first.")
with tab4:
st.header("📈 Analytics & Insights")
if st.session_state.search_history:
# Search history
st.subheader("Search History")
history_df = pd.DataFrame(st.session_state.search_history)
st.dataframe(history_df, use_container_width=True)
# Search trends
if len(st.session_state.search_history) > 1:
fig = px.line(
history_df,
x='timestamp',
y='results',
title="Search Results Over Time"
)
st.plotly_chart(fig, use_container_width=True, key="search_trends_chart")
if st.session_state.candidates:
# Candidate insights
st.subheader("Candidate Insights")
# Company distribution
companies = [c.get('current_company', 'Unknown') for c in st.session_state.candidates]
company_counts = pd.Series(companies).value_counts().head(10)
fig = px.bar(
x=company_counts.values,
y=company_counts.index,
orientation='h',
title="Top Companies",
labels={'x': 'Number of Candidates', 'y': 'Company'}
)
st.plotly_chart(fig, use_container_width=True, key="company_distribution_chart")
# Location distribution
locations = [c.get('location', 'Unknown').split(',')[0] for c in st.session_state.candidates]
location_counts = pd.Series(locations).value_counts().head(10)
fig = px.pie(
values=location_counts.values,
names=location_counts.index,
title="Candidate Locations"
)
st.plotly_chart(fig, use_container_width=True, key="location_distribution_chart")
# Performance metrics
st.subheader("System Performance")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Searches", len(st.session_state.search_history))
with col2:
total_candidates = sum(h['results'] for h in st.session_state.search_history)
st.metric("Total Candidates Found", total_candidates)
with col3:
if st.session_state.search_history:
avg_results = total_candidates / len(st.session_state.search_history)
st.metric("Avg Results per Search", f"{avg_results:.1f}")
# Footer
st.markdown("---")
st.markdown("**🎯 LinkedIn Sourcing Agent** | Built for Synapse AI Hackathon | Powered by AI")
# Sidebar info
with st.sidebar:
st.markdown("---")
st.subheader("ℹ️ About")
st.write("This is a professional LinkedIn candidate sourcing and outreach automation system.")
st.subheader("🚀 Features")
st.write("• AI-powered candidate scoring")
st.write("• Personalized outreach generation")
st.write("• Multi-format export options")
st.write("• Real-time analytics")
if st.button("🔄 Reset Session"):
st.session_state.clear()
st.rerun()