File size: 33,960 Bytes
5e5e890
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#!/usr/bin/env python3
"""
LinkedIn Profile Enhancer - Gradio Interface (app2.py)
A beautiful web interface for the LinkedIn Profile Enhancer using Gradio
"""

import sys
import os
import time
import json
from typing import Dict, Any, Tuple, Optional
import gradio as gr
from PIL import Image
import requests
from io import BytesIO

# Add project root to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from agents.orchestrator import ProfileOrchestrator
from agents.scraper_agent import ScraperAgent
from agents.analyzer_agent import AnalyzerAgent
from agents.content_agent import ContentAgent

class LinkedInEnhancerGradio:
    """Gradio Interface for LinkedIn Profile Enhancer"""
    
    def __init__(self):
        self.orchestrator = ProfileOrchestrator()
        self.current_profile_data = None
        self.current_analysis = None
        self.current_suggestions = None
    
    def test_api_connections(self) -> Tuple[str, str]:
        """Test API connections and return status"""
        apify_status = "❌ Failed"
        openai_status = "❌ Failed"
        
        try:
            scraper = ScraperAgent()
            if scraper.test_apify_connection():
                apify_status = "βœ… Connected"
        except Exception as e:
            apify_status = f"❌ Error: {str(e)[:50]}..."
        
        try:
            content_agent = ContentAgent()
            if content_agent.test_openai_connection():
                openai_status = "βœ… Connected"
        except Exception as e:
            openai_status = f"❌ Error: {str(e)[:50]}..."
        
        return apify_status, openai_status
    
    def load_profile_image(self, image_url: str) -> Optional[Image.Image]:
        """Load profile image from URL"""
        try:
            if image_url:
                response = requests.get(image_url, timeout=10)
                if response.status_code == 200:
                    return Image.open(BytesIO(response.content))
        except Exception as e:
            print(f"Error loading image: {e}")
        return None
    
    def enhance_linkedin_profile(self, linkedin_url: str, job_description: str = "") -> Tuple[str, str, str, str, str, str, str, str, Optional[Image.Image]]:
        """Complete LinkedIn profile enhancement with extraction, analysis, and suggestions"""
        if not linkedin_url.strip():
            return "❌ Error", "Please enter a LinkedIn profile URL", "", "", "", "", "", "", None
        
        if not any(pattern in linkedin_url.lower() for pattern in ['linkedin.com/in/', 'www.linkedin.com/in/']):
            return "❌ Error", "Please enter a valid LinkedIn profile URL", "", "", "", "", "", "", None
        
        try:
            # Step 1: Extract profile data
            self.orchestrator.memory.session_data.clear()
            profile_data = self.orchestrator.scraper.extract_profile_data(linkedin_url)
            self.current_profile_data = profile_data
            
            # Format basic info
            basic_info = f"""
**Name:** {profile_data.get('name', 'N/A')}
**Headline:** {profile_data.get('headline', 'N/A')}
**Location:** {profile_data.get('location', 'N/A')}
**Connections:** {profile_data.get('connections', 'N/A')}
**Followers:** {profile_data.get('followers', 'N/A')}
**Email:** {profile_data.get('email', 'N/A')}
**Current Job:** {profile_data.get('job_title', 'N/A')} at {profile_data.get('company_name', 'N/A')}
            """
            
            # Format about section
            about_section = profile_data.get('about', 'No about section available')
            
            # Format experience
            experience_text = ""
            for i, exp in enumerate(profile_data.get('experience', [])[:5], 1):
                experience_text += f"""
**{i}. {exp.get('title', 'Position')}**
- Company: {exp.get('company', 'N/A')}
- Duration: {exp.get('duration', 'N/A')}
- Location: {exp.get('location', 'N/A')}
- Current: {'Yes' if exp.get('is_current') else 'No'}
"""
                if exp.get('description'):
                    experience_text += f"- Description: {exp.get('description')[:200]}...\n"
                experience_text += "\n"
            
            # Format education and skills
            education_text = ""
            for i, edu in enumerate(profile_data.get('education', []), 1):
                education_text += f"""
**{i}. {edu.get('school', 'School')}**
- Degree: {edu.get('degree', 'N/A')}
- Field: {edu.get('field', 'N/A')}
- Year: {edu.get('year', 'N/A')}
- Grade: {edu.get('grade', 'N/A')}

"""
            
            skills_text = ", ".join(profile_data.get('skills', [])[:20])
            if len(profile_data.get('skills', [])) > 20:
                skills_text += f" ... and {len(profile_data.get('skills', [])) - 20} more"
            
            details_text = f"""
## πŸŽ“ Education
{education_text if education_text else "No education information available"}

## πŸ› οΈ Skills
{skills_text if skills_text else "No skills information available"}

## πŸ† Certifications
{len(profile_data.get('certifications', []))} certifications found

## πŸ“Š Additional Data
- Projects: {len(profile_data.get('projects', []))}
- Publications: {len(profile_data.get('publications', []))}
- Recommendations: {len(profile_data.get('recommendations', []))}
            """
            
            # Load profile image
            profile_image = self.load_profile_image(profile_data.get('profile_image_hq') or profile_data.get('profile_image'))
            
            # Step 2: Analyze profile automatically
            try:
                analysis = self.orchestrator.analyzer.analyze_profile(
                    self.current_profile_data, 
                    job_description
                )
                self.current_analysis = analysis
                
                # Format analysis results
                analysis_text = f"""
## πŸ“Š Analysis Results

**Overall Rating:** {analysis.get('overall_rating', 'Unknown')}
**Completeness Score:** {analysis.get('completeness_score', 0):.1f}%
**Job Match Score:** {analysis.get('job_match_score', 0):.1f}%

### 🌟 Strengths
"""
                for strength in analysis.get('strengths', []):
                    analysis_text += f"- {strength}\n"
                
                analysis_text += "\n### ⚠️ Areas for Improvement\n"
                for weakness in analysis.get('weaknesses', []):
                    analysis_text += f"- {weakness}\n"
                
                # Keyword analysis
                keyword_analysis = analysis.get('keyword_analysis', {})
                keywords_text = ""
                if keyword_analysis:
                    found_keywords = keyword_analysis.get('found_keywords', [])
                    missing_keywords = keyword_analysis.get('missing_keywords', [])
                    
                    keywords_text = f"""
## πŸ” Keyword Analysis

**Found Keywords:** {', '.join(found_keywords[:10])}
{"..." if len(found_keywords) > 10 else ""}

**Missing Keywords:** {', '.join(missing_keywords[:5])}
{"..." if len(missing_keywords) > 5 else ""}
                    """
            except Exception as e:
                analysis_text = f"⚠️ Analysis failed: {str(e)}"
                keywords_text = ""
            
            # Step 3: Generate suggestions automatically
            try:
                suggestions = self.orchestrator.content_generator.generate_suggestions(
                    self.current_analysis, 
                    job_description
                )
                self.current_suggestions = suggestions
                
                suggestions_text = ""
                
                for category, items in suggestions.items():
                    if category == 'ai_generated_content':
                        ai_content = items if isinstance(items, dict) else {}
                        
                        # AI Headlines
                        if 'ai_headlines' in ai_content and ai_content['ai_headlines']:
                            suggestions_text += "## ✨ Professional Headlines\n\n"
                            for i, headline in enumerate(ai_content['ai_headlines'], 1):
                                cleaned_headline = headline.strip('"').replace('\\"', '"')
                                if cleaned_headline.startswith(('1.', '2.', '3.', '4.', '5.')):
                                    cleaned_headline = cleaned_headline[2:].strip()
                                suggestions_text += f"{i}. {cleaned_headline}\n\n"
                        
                        # AI About Section
                        if 'ai_about_section' in ai_content and ai_content['ai_about_section']:
                            suggestions_text += "## πŸ“„ Enhanced About Section\n\n"
                            suggestions_text += f"```\n{ai_content['ai_about_section']}\n```\n\n"
                        
                        # AI Experience Descriptions
                        if 'ai_experience_descriptions' in ai_content and ai_content['ai_experience_descriptions']:
                            suggestions_text += "## πŸ’Ό Experience Description Ideas\n\n"
                            for desc in ai_content['ai_experience_descriptions']:
                                suggestions_text += f"- {desc}\n"
                            suggestions_text += "\n"
                    else:
                        # Standard categories
                        category_name = category.replace('_', ' ').title()
                        suggestions_text += f"## πŸ“‹ {category_name}\n\n"
                        if isinstance(items, list):
                            for item in items:
                                suggestions_text += f"- {item}\n"
                        else:
                            suggestions_text += f"- {items}\n"
                        suggestions_text += "\n"
            except Exception as e:
                suggestions_text = f"⚠️ Suggestions generation failed: {str(e)}"
            
            return "βœ… Profile Enhanced Successfully", basic_info, about_section, experience_text, details_text, analysis_text, keywords_text, suggestions_text, profile_image
            
        except Exception as e:
            return "❌ Error", f"Failed to enhance profile: {str(e)}", "", "", "", "", "", "", None
    
    def analyze_profile(self, job_description: str = "") -> Tuple[str, str, str]:
        """Analyze the extracted profile data"""
        if not self.current_profile_data:
            return "❌ Error", "Please extract profile data first", ""
        
        try:
            # Analyze profile
            analysis = self.orchestrator.analyzer.analyze_profile(
                self.current_profile_data, 
                job_description
            )
            self.current_analysis = analysis
            
            # Format analysis results
            analysis_text = f"""
## πŸ“Š Analysis Results

**Overall Rating:** {analysis.get('overall_rating', 'Unknown')}
**Completeness Score:** {analysis.get('completeness_score', 0):.1f}%
**Job Match Score:** {analysis.get('job_match_score', 0):.1f}%

### 🌟 Strengths
"""
            for strength in analysis.get('strengths', []):
                analysis_text += f"- {strength}\n"
            
            analysis_text += "\n### οΏ½ Areas for Improvement\n"
            for weakness in analysis.get('weaknesses', []):
                analysis_text += f"- {weakness}\n"
            
            # Keyword analysis
            keyword_analysis = analysis.get('keyword_analysis', {})
            keywords_text = ""
            if keyword_analysis:
                found_keywords = keyword_analysis.get('found_keywords', [])
                missing_keywords = keyword_analysis.get('missing_keywords', [])
                
                keywords_text = f"""
## πŸ” Keyword Analysis

**Found Keywords:** {', '.join(found_keywords[:10])}
{"..." if len(found_keywords) > 10 else ""}

**Missing Keywords:** {', '.join(missing_keywords[:5])}
{"..." if len(missing_keywords) > 5 else ""}
                """
            
            return "βœ… Success", analysis_text, keywords_text
            
        except Exception as e:
            return "❌ Error", f"Failed to analyze profile: {str(e)}", ""
    
    def generate_suggestions(self, job_description: str = "") -> Tuple[str, str]:
        """Generate enhancement suggestions"""
        if not self.current_analysis:
            return "❌ Error", "Please analyze profile first"
        
        try:
            # Generate suggestions
            suggestions = self.orchestrator.content_generator.generate_suggestions(
                self.current_analysis, 
                job_description
            )
            self.current_suggestions = suggestions
            
            suggestions_text = ""
            ai_content_text = ""
            
            for category, items in suggestions.items():
                if category == 'ai_generated_content':
                    ai_content = items if isinstance(items, dict) else {}
                    
                    # AI Headlines
                    if 'ai_headlines' in ai_content and ai_content['ai_headlines']:
                        ai_content_text += "## ✨ Professional Headlines\n\n"
                        for i, headline in enumerate(ai_content['ai_headlines'], 1):
                            cleaned_headline = headline.strip('"').replace('\\"', '"')
                            if cleaned_headline.startswith(('1.', '2.', '3.', '4.', '5.')):
                                cleaned_headline = cleaned_headline[2:].strip()
                            ai_content_text += f"{i}. {cleaned_headline}\n\n"
                    
                    # AI About Section
                    if 'ai_about_section' in ai_content and ai_content['ai_about_section']:
                        ai_content_text += "## οΏ½ Enhanced About Section\n\n"
                        ai_content_text += f"```\n{ai_content['ai_about_section']}\n```\n\n"
                    
                    # AI Experience Descriptions
                    if 'ai_experience_descriptions' in ai_content and ai_content['ai_experience_descriptions']:
                        ai_content_text += "## πŸ’Ό Experience Description Ideas\n\n"
                        for desc in ai_content['ai_experience_descriptions']:
                            ai_content_text += f"- {desc}\n"
                        ai_content_text += "\n"
                else:
                    # Standard categories
                    category_name = category.replace('_', ' ').title()
                    suggestions_text += f"## πŸ“‹ {category_name}\n\n"
                    if isinstance(items, list):
                        for item in items:
                            suggestions_text += f"- {item}\n"
                    else:
                        suggestions_text += f"- {items}\n"
                    suggestions_text += "\n"
            
            return "βœ… Success", suggestions_text + ai_content_text
            
        except Exception as e:
            return "❌ Error", f"Failed to generate suggestions: {str(e)}"
    
    def export_results(self, linkedin_url: str) -> str:
        """Export all results to a comprehensive downloadable file"""
        if not self.current_profile_data:
            return "❌ No data to export"
        
        try:
            # Create filename with timestamp
            profile_name = linkedin_url.split('/in/')[-1].split('/')[0] if linkedin_url else 'profile'
            timestamp = time.strftime('%Y%m%d_%H%M%S')
            filename = f"LinkedIn_Profile_Enhancement_{profile_name}_{timestamp}.md"
            
            # Compile comprehensive report
            content = f"""# πŸš€ LinkedIn Profile Enhancement Report

**Generated:** {time.strftime('%B %d, %Y at %I:%M %p')}  
**Profile URL:** [{linkedin_url}]({linkedin_url})  
**Enhancement Date:** {time.strftime('%Y-%m-%d')}

---

## πŸ“Š Executive Summary

This comprehensive report provides a detailed analysis of your LinkedIn profile along with AI-powered enhancement suggestions to improve your professional visibility and job match potential.

---

## πŸ‘€ Basic Profile Information

| Field | Current Value |
|-------|---------------|
| **Name** | {self.current_profile_data.get('name', 'N/A')} |
| **Professional Headline** | {self.current_profile_data.get('headline', 'N/A')} |
| **Location** | {self.current_profile_data.get('location', 'N/A')} |
| **Connections** | {self.current_profile_data.get('connections', 'N/A')} |
| **Followers** | {self.current_profile_data.get('followers', 'N/A')} |
| **Email** | {self.current_profile_data.get('email', 'N/A')} |
| **Current Position** | {self.current_profile_data.get('job_title', 'N/A')} at {self.current_profile_data.get('company_name', 'N/A')} |

---

## πŸ“ Current About Section

```
{self.current_profile_data.get('about', 'No about section available')}
```

---

## πŸ’Ό Professional Experience

"""
            # Add experience details
            for i, exp in enumerate(self.current_profile_data.get('experience', []), 1):
                content += f"""
### {i}. {exp.get('title', 'Position')} 
**Company:** {exp.get('company', 'N/A')}  
**Duration:** {exp.get('duration', 'N/A')}  
**Location:** {exp.get('location', 'N/A')}  
**Current Role:** {'Yes' if exp.get('is_current') else 'No'}

"""
                if exp.get('description'):
                    content += f"**Description:**\n```\n{exp.get('description')}\n```\n\n"
            
            # Add education
            content += "---\n\n## πŸŽ“ Education\n\n"
            for i, edu in enumerate(self.current_profile_data.get('education', []), 1):
                content += f"""
### {i}. {edu.get('school', 'School')}
- **Degree:** {edu.get('degree', 'N/A')}
- **Field of Study:** {edu.get('field', 'N/A')}
- **Year:** {edu.get('year', 'N/A')}
- **Grade:** {edu.get('grade', 'N/A')}

"""
            
            # Add skills
            skills = self.current_profile_data.get('skills', [])
            content += f"""---

## πŸ› οΈ Skills & Expertise

**Total Skills Listed:** {len(skills)}

"""
            if skills:
                # Group skills for better readability
                skills_per_line = 5
                for i in range(0, len(skills), skills_per_line):
                    skill_group = skills[i:i+skills_per_line]
                    content += f"- {' β€’ '.join(skill_group)}\n"
            
            # Add certifications and additional data
            content += f"""
---

## πŸ† Additional Profile Data

| Category | Count |
|----------|-------|
| **Certifications** | {len(self.current_profile_data.get('certifications', []))} |
| **Projects** | {len(self.current_profile_data.get('projects', []))} |
| **Publications** | {len(self.current_profile_data.get('publications', []))} |
| **Recommendations** | {len(self.current_profile_data.get('recommendations', []))} |

"""
            
            # Add analysis results if available
            if self.current_analysis:
                content += f"""---

## πŸ“ˆ AI Analysis Results

### Overall Assessment
- **Overall Rating:** {self.current_analysis.get('overall_rating', 'Unknown')}
- **Profile Completeness:** {self.current_analysis.get('completeness_score', 0):.1f}%
- **Job Match Score:** {self.current_analysis.get('job_match_score', 0):.1f}%

### 🌟 Identified Strengths
"""
                for strength in self.current_analysis.get('strengths', []):
                    content += f"- {strength}\n"
                
                content += "\n### ⚠️ Areas for Improvement\n"
                for weakness in self.current_analysis.get('weaknesses', []):
                    content += f"- {weakness}\n"
                
                # Add keyword analysis
                keyword_analysis = self.current_analysis.get('keyword_analysis', {})
                if keyword_analysis:
                    found_keywords = keyword_analysis.get('found_keywords', [])
                    missing_keywords = keyword_analysis.get('missing_keywords', [])
                    
                    content += f"""
### πŸ” Keyword Analysis

**Found Keywords ({len(found_keywords)}):** {', '.join(found_keywords[:15])}
{"..." if len(found_keywords) > 15 else ""}

**Missing Keywords ({len(missing_keywords)}):** {', '.join(missing_keywords[:10])}
{"..." if len(missing_keywords) > 10 else ""}
"""
            
            # Add enhancement suggestions if available
            if self.current_suggestions:
                content += "\n---\n\n## πŸ’‘ AI-Powered Enhancement Suggestions\n\n"
                
                for category, items in self.current_suggestions.items():
                    if category == 'ai_generated_content':
                        ai_content = items if isinstance(items, dict) else {}
                        
                        # AI Headlines
                        if 'ai_headlines' in ai_content and ai_content['ai_headlines']:
                            content += "### ✨ Professional Headlines (Choose Your Favorite)\n\n"
                            for i, headline in enumerate(ai_content['ai_headlines'], 1):
                                cleaned_headline = headline.strip('"').replace('\\"', '"')
                                if cleaned_headline.startswith(('1.', '2.', '3.', '4.', '5.')):
                                    cleaned_headline = cleaned_headline[2:].strip()
                                content += f"{i}. {cleaned_headline}\n\n"
                        
                        # AI About Section
                        if 'ai_about_section' in ai_content and ai_content['ai_about_section']:
                            content += "### πŸ“„ Enhanced About Section\n\n"
                            content += f"```\n{ai_content['ai_about_section']}\n```\n\n"
                        
                        # AI Experience Descriptions
                        if 'ai_experience_descriptions' in ai_content and ai_content['ai_experience_descriptions']:
                            content += "### πŸ’Ό Experience Description Enhancements\n\n"
                            for j, desc in enumerate(ai_content['ai_experience_descriptions'], 1):
                                content += f"{j}. {desc}\n\n"
                    else:
                        # Standard categories
                        category_name = category.replace('_', ' ').title()
                        content += f"### πŸ“‹ {category_name}\n\n"
                        if isinstance(items, list):
                            for item in items:
                                content += f"- {item}\n"
                        else:
                            content += f"- {items}\n"
                        content += "\n"
            
            # Add action items and next steps
            content += """---

## 🎯 Recommended Action Items

### Immediate Actions (This Week)
1. **Update Headline:** Choose one of the AI-generated headlines that best reflects your goals
2. **Enhance About Section:** Implement the suggested about section improvements
3. **Add Missing Keywords:** Incorporate relevant missing keywords naturally into your content
4. **Complete Profile Sections:** Fill in any incomplete sections identified in the analysis

### Medium-term Goals (This Month)
1. **Experience Descriptions:** Update job descriptions using the AI-generated suggestions
2. **Skills Optimization:** Add relevant skills identified in the keyword analysis
3. **Network Growth:** Aim to increase connections in your industry
4. **Content Strategy:** Start sharing relevant professional content

### Long-term Strategy (Next 3 Months)
1. **Regular Updates:** Keep your profile current with new achievements and skills
2. **Engagement:** Actively engage with your network's content
3. **Personal Branding:** Develop a consistent professional brand across all sections
4. **Performance Monitoring:** Track profile views and connection requests

---

## πŸ“ž Additional Resources

- **LinkedIn Profile Optimization Guide:** [LinkedIn Help Center](https://www.linkedin.com/help/linkedin)
- **Professional Photography:** Consider professional headshots for profile picture
- **Skill Assessments:** Take LinkedIn skill assessments to verify your expertise
- **Industry Groups:** Join relevant professional groups in your field



*This is an automated analysis. Results may vary based on individual goals and industry standards.*
"""
            
            # Save to file (this will be downloaded by the browser)
            with open(filename, 'w', encoding='utf-8') as f:
                f.write(content)
            
            return f"βœ… Report exported as {filename} - File saved for download"
            
        except Exception as e:
            return f"❌ Export failed: {str(e)}"

def create_gradio_interface():
    """Create and return the Gradio interface"""
    
    app = LinkedInEnhancerGradio()
    
    # Custom CSS for beautiful styling
    custom_css = """
    .gradio-container {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        max-width: 1200px;
        margin: 0 auto;
    }
    
    .header-text {
        text-align: center;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 2rem;
        border-radius: 10px;
        margin-bottom: 2rem;
    }
    
    .status-box {
        padding: 1rem;
        border-radius: 8px;
        margin: 0.5rem 0;
    }
    
    .success {
        background-color: #d4edda;
        border: 1px solid #c3e6cb;
        color: #155724;
    }
    
    .error {
        background-color: #f8d7da;
        border: 1px solid #f5c6cb;
        color: #721c24;
    }
    
    .info {
        background-color: #e7f3ff;
        border: 1px solid #b3d7ff;
        color: #0c5460;
    }
    """
    
    with gr.Blocks(css=custom_css, title="πŸš€ LinkedIn Profile Enhancer", theme=gr.themes.Soft()) as demo:
        
        # Header
        gr.HTML("""
        <div class="header-text">
            <h1>πŸš€ LinkedIn Profile Enhancer</h1>
            <p style="font-size: 1.2em; margin: 1rem 0;">AI-powered LinkedIn profile analysis and enhancement suggestions</p>
            <div style="display: flex; justify-content: center; gap: 2rem; margin-top: 1rem;">
                <div style="text-align: center;">
                    <div style="font-size: 2em;">πŸ”</div>
                    <div>Real Scraping</div>
                </div>
                <div style="text-align: center;">
                    <div style="font-size: 2em;">πŸ€–</div>
                    <div>AI Analysis</div>
                </div>
                <div style="text-align: center;">
                    <div style="font-size: 2em;">🎯</div>
                    <div>Smart Suggestions</div>
                </div>
                <div style="text-align: center;">
                    <div style="font-size: 2em;">πŸ“Š</div>
                    <div>Rich Data</div>
                </div>
            </div>
        </div>
        """)
        
        # API Status Section
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("## πŸ”Œ API Status")
                with gr.Row():
                    apify_status = gr.Textbox(label="πŸ“‘ Apify API", interactive=False, value="Testing...")
                    openai_status = gr.Textbox(label="πŸ€– OpenAI API", interactive=False, value="Testing...")
                test_btn = gr.Button("πŸ”„ Test Connections", variant="secondary")
        
        # Main Input Section
        with gr.Row():
            with gr.Column(scale=2):
                linkedin_url = gr.Textbox(
                    label="πŸ”— LinkedIn Profile URL",
                    placeholder="https://www.linkedin.com/in/your-profile",
                    lines=1
                )
                job_description = gr.Textbox(
                    label="🎯 Target Job Description (Optional)",
                    placeholder="Paste the job description here for tailored suggestions...",
                    lines=5
                )
            
            with gr.Column(scale=1):
                profile_image = gr.Image(
                    label="πŸ“Έ Profile Picture",
                    height=200,
                    width=200
                )
        
        # Action Buttons - Single Enhanced Button
        with gr.Row():
            enhance_btn = gr.Button("οΏ½ Enhance LinkedIn Profile", variant="primary", size="lg")
            export_btn = gr.Button("πŸ“ Export Results", variant="secondary")
        
        # Results Section with Tabs
        with gr.Tabs():
            with gr.TabItem("πŸ“Š Basic Information"):
                enhance_status = gr.Textbox(label="Status", interactive=False)
                basic_info = gr.Markdown(label="Basic Information")
            
            with gr.TabItem("πŸ“ About Section"):
                about_section = gr.Markdown(label="About Section")
            
            with gr.TabItem("πŸ’Ό Experience"):
                experience_info = gr.Markdown(label="Work Experience")
            
            with gr.TabItem("πŸŽ“ Education & Skills"):
                education_skills = gr.Markdown(label="Education & Skills")
            
            with gr.TabItem("πŸ“ˆ Analysis Results"):
                analysis_results = gr.Markdown(label="Analysis Results")
                keyword_analysis = gr.Markdown(label="Keyword Analysis")
            
            with gr.TabItem("πŸ’‘ Enhancement Suggestions"):
                suggestions_content = gr.Markdown(label="Enhancement Suggestions")
            
            with gr.TabItem("πŸ“ Export & Download"):
                export_status = gr.Textbox(label="Download Status", interactive=False)
                gr.Markdown("""
                ### πŸ“ Comprehensive Report Download
                
                Click the **Export Results** button to download a complete markdown report containing:
                
                #### πŸ“Š **Complete Profile Analysis**
                - Basic profile information and current content
                - Detailed experience and education sections
                - Skills analysis and completeness scoring
                
                #### πŸ€– **AI Enhancement Suggestions**
                - Professional headline options
                - Enhanced about section recommendations
                - Experience description improvements
                - Keyword optimization suggestions
                
                #### 🎯 **Action Plan**
                - Immediate action items (this week)
                - Medium-term goals (this month) 
                - Long-term strategy (next 3 months)
                - Additional resources and tips
                
                **File Format:** Markdown (.md) - Compatible with GitHub, Notion, and most text editors
                """)
        
        # Event Handlers
        def on_test_connections():
            apify, openai = app.test_api_connections()
            return apify, openai
        
        def on_enhance_profile(url, job_desc):
            status, basic, about, exp, details, analysis, keywords, suggestions, image = app.enhance_linkedin_profile(url, job_desc)
            return status, basic, about, exp, details, analysis, keywords, suggestions, image
        
        def on_export_results(url):
            return app.export_results(url)
        
        # Connect events
        test_btn.click(
            fn=on_test_connections,
            outputs=[apify_status, openai_status]
        )
        
        enhance_btn.click(
            fn=on_enhance_profile,
            inputs=[linkedin_url, job_description],
            outputs=[enhance_status, basic_info, about_section, experience_info, education_skills, analysis_results, keyword_analysis, suggestions_content, profile_image]
        )
        
        export_btn.click(
            fn=on_export_results,
            inputs=[linkedin_url],
            outputs=[export_status]
        )
        
        # Auto-test connections on load
        demo.load(
            fn=on_test_connections,
            outputs=[apify_status, openai_status]
        )
        
        # Footer
        gr.HTML("""
        <div style="text-align: center; margin-top: 2rem; padding: 1rem; border-top: 1px solid #eee;">
            <p>πŸš€ <strong>LinkedIn Profile Enhancer</strong> | Powered by AI | Built with ❀️ using Gradio</p>
            <p>Data scraped with respect to LinkedIn's ToS | Uses OpenAI GPT-4o-mini and Apify</p>
        </div>
        """)
    
    return demo

def main():
    """Main function"""
    
    # Check if running with command line arguments (for backward compatibility)
    if len(sys.argv) > 1:
        if sys.argv[1] == '--help':
            print("""
LinkedIn Profile Enhancer - Gradio Interface

Usage:
    python app2.py                      # Launch Gradio web interface
    python app2.py --help               # Show this help
    
Web Interface Features:
    - Beautiful modern UI
    - Real-time profile extraction
    - AI-powered analysis
    - Enhancement suggestions
    - Export functionality
    - Profile image display
            """)
            return
        else:
            print("❌ Unknown argument. Use --help for usage information.")
            return
    
    # Launch Gradio interface
    print("πŸš€ Starting LinkedIn Profile Enhancer...")
    print("πŸ“± Launching Gradio interface...")
    
    demo = create_gradio_interface()
    demo.launch(
        server_name="localhost",
        server_port=7860,
        share=True,  # Creates a public link
        show_error=True
    )

if __name__ == "__main__":
    main()