File size: 30,325 Bytes
d5eb838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from pymongo import MongoClient
from datetime import datetime
import requests
from openai import OpenAI
from bs4 import BeautifulSoup
import asyncio
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time

# Initialize OpenAI client
@st.cache_resource
def init_openai():
    client = OpenAI(api_key=('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'))
    return client

# Initialize MongoDB connection
@st.cache_resource
def init_mongodb():
    client = MongoClient("mongodb://linkedin_user:P4XnKOjkOaTg@18.235.17.44:27017/?authMechanism=DEFAULT")
    return client['linkedin_db']

# Get detailed address using Selenium and Google Maps
@st.cache_data(ttl=3600)
def get_location_info(company_name):
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    
    result = {
        "status": "failed",
        "address": None,
        "error": None
    }
    
    try:
        driver = webdriver.Chrome(options=options)
        driver.implicitly_wait(10)
        
        # Open Google Maps
        driver.get("https://www.google.com/maps")
        
        # Accept cookies if prompted (common in some regions)
        try:
            cookie_button = WebDriverWait(driver, 3).until(
                EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Accept all')]"))
            )
            cookie_button.click()
        except:
            pass  # No cookie prompt or different format
        
        # Find and use the search box
        search_box = driver.find_element(By.NAME, "q")
        search_box.clear()
        search_box.send_keys(company_name)
        search_box.send_keys(Keys.RETURN)
        
        # Wait for results and get the address
        wait = WebDriverWait(driver, 10)
        
        # First attempt with Io6YTe class
        try:
            address_element = wait.until(
                EC.presence_of_element_located((By.CLASS_NAME, "Io6YTe"))
            )
            address = address_element.text
            result["status"] = "success"
            result["address"] = address
        except:
            # Fallback to alternative selectors
            try:
                # Try looking for the address in a different format
                address_container = wait.until(
                    EC.presence_of_element_located((By.CSS_SELECTOR, "[data-section-id='addr']"))
                )
                address = address_container.text.replace("Address: ", "")
                result["status"] = "success"
                result["address"] = address
            except:
                result["error"] = "Could not find address element"
    
    except TimeoutException:
        result["error"] = "Timeout waiting for Google Maps to load"
    except NoSuchElementException:
        result["error"] = "Could not find the required elements on the page"
    except Exception as e:
        result["error"] = f"An error occurred: {str(e)}"
    finally:
        # Always close the browser
        if 'driver' in locals():
            driver.quit()
    
    return result


def search_profiles(db, search_terms, location=None, limit=100):
    # Your existing function code here
    query = {
        '$and': [
            {'search_query': {'$regex': search_terms, '$options': 'i'}},
        ]
    }
    
    if location:
        query['$and'].append({'location': {'$regex': location, '$options': 'i'}})
    
    profiles = list(db.LInkedinProfiles.find(query).limit(limit))
    
    # Get all sent emails for this campaign
    sent_emails = list(db.sent_emails.find({}, {
        'recipient_first_name': 1,
        'recipient_last_name': 1,
        'recipient_company': 1,
        'sent_date': 1
    }))
    
    # Create a lookup dictionary for sent emails
    sent_email_lookup = {
        f"{email['recipient_first_name']}_{email['recipient_last_name']}_{email['recipient_company']}": email['sent_date']
        for email in sent_emails
    }
    
    # Add email status to each profile
    for profile in profiles:
        key = f"{profile['first_name']}_{profile['last_name']}_{profile['company']}"
        if key in sent_email_lookup:
            profile['email_status'] = 'Sent'
            profile['sent_date'] = sent_email_lookup[key]
        else:
            profile['email_status'] = 'Not Sent'
            profile['sent_date'] = None
    
    return profiles


async def get_coffee_shops(company_address):
    if not company_address:
        return []
        
    url = f"https://www.google.com/search?q=coffee%20shops%20near%20{company_address}&sca_esv=2621f7b39c394d4e&tbm=lcl"
    
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
    }
    
    try:
        response = requests.get(url, headers=headers)
        soup = BeautifulSoup(response.text, "html.parser")
        shops = soup.find_all("div", class_="VkpGBb")
        
        rated_shops = []
        
        for shop in shops:
            name = shop.find("div", class_="dbg0pd").text if shop.find("div", class_="dbg0pd") else "N/A"
            rating_elem = shop.find("span", class_="yi40Hd YrbPuc")
            rating = rating_elem.text if rating_elem else "0"
            address_divs = shop.find_all("div")
            address = address_divs[-4].text.strip() if len(address_divs) > 2 else "N/A"
            
            try:
                rating_value = float(rating.replace("/5", ""))
                rated_shops.append({
                    "name": name,
                    "rating": rating,
                    "rating_value": rating_value,
                    "address": address
                })
            except ValueError:
                continue
        
        top_shops = sorted(rated_shops, key=lambda x: x["rating_value"], reverse=True)[:3]
        
        for shop in top_shops:
            del shop["rating_value"]
            
        return top_shops
    except Exception as e:
        print(f"Error fetching shops for {company_address}: {str(e)}")
        return []

def get_email_for_profile(profile_url):
    try:
        response = requests.get(
            "http://127.0.0.1:8000/get_emails",
            params={"profile_url": profile_url},
            timeout=10
        )
        
        if response.status_code == 200:
            data = response.json()
            return data.get('email')
        else:
            st.error(f"API Error: Status {response.status_code}")
            return None
            
    except requests.exceptions.Timeout:
        st.error("Request timed out. Please try again.")
        return None
    except requests.exceptions.ConnectionError:
        st.error("Could not connect to the email service. Please check if the API server is running.")
        return None
    except Exception as e:
        st.error(f"Error fetching email: {str(e)}")
        return None

def update_template_with_coffee_shop(template, shop_name, shop_address):
    paragraphs = template.split('\n\n')
    
    meeting_loc_idx = -1
    for i, para in enumerate(paragraphs):
        if any(keyword in para.lower() for keyword in ['meet', 'coffee', 'discuss']):
            meeting_loc_idx = i
            break
    
    meeting_text = f"I would love to meet you at {shop_name} ({shop_address}) to discuss this further."
    
    if meeting_loc_idx >= 0:
        paragraphs[meeting_loc_idx] = meeting_text
    else:
        paragraphs.insert(-1, meeting_text)
    
    return '\n\n'.join(paragraphs)

def generate_email_template(openai_client, profile_data, coffee_shops):
    try:
        shops_text = ""
        if coffee_shops:
            shops_text = "Nearby recommended meeting spots:\n"
            for i, shop in enumerate(coffee_shops, 1):
                shops_text += f"{i}. {shop['name']} (Rating: {shop['rating']}) - {shop['address']}\n"

        first_name = profile_data.get('first_name', '')
        company = profile_data.get('company', '')
        location = profile_data.get('location', '')
        description = profile_data.get('description', '')
        company_address = profile_data.get('company_address', '')

        # First, detect the actual company name
        company_detection_prompt = f"""
        I need to identify the most likely company name from the following LinkedIn profile data. The company name might be in any of these fields:
        
        Company field: "{company}"
        Description field: "{description}"
        Location field: "{location}"
        
        Analyze all three fields and identify the most likely company name. Return ONLY the company name, nothing else.
        """
        
        company_detection_response = openai_client.chat.completions.create(
            model="gpt-4-turbo-preview",
            messages=[
                {"role": "system", "content": "You extract the most likely company name from LinkedIn profile data."},
                {"role": "user", "content": company_detection_prompt}
            ],
            temperature=0.3
        )
        
        detected_company = company_detection_response.choices[0].message.content.strip()
        
        # Print the detected company name and address
        print(f"Original company field: {company}")
        print(f"Detected company name: {detected_company}")
        print(f"Company address: {company_address}")
        
        system_message = """
        You are a professional email writer crafting meeting request templates.
        IMPORTANT: 
        1. Always use the recipient's actual first name in the greeting (e.g., "Dear John," not "Dear [Name]")
        2. Always specifically mention their company name in the first paragraph
        3. Always sign the email with "Best regards,\nAdil"
        4. Never use placeholders like [Name] or [CEO's Name]
        """

        prompt = f"""
        Based on the following professional's information and nearby coffee shops, write a formal email template requesting a meeting:
        
        First Name: {first_name}
        Company: {detected_company}
        Location: {location}
        Company Address: {company_address}
        Description: {description}
        
        {shops_text}
        
        The email should:
        1. Begin with "Dear {first_name},"
        2. Be professional and formal
        3. Reference their current role at {detected_company} specifically in the first paragraph
        4. Suggest meeting at one of the nearby coffee shops (if available)
        5. Be concise but personal
        6. Include a clear call to action for a meeting
        7. End with "Best regards,\nAdil"
        
        Write only the email body without additional subject line or formatting.
        """
        
        response = openai_client.chat.completions.create(
            model="gpt-4-turbo-preview",
            messages=[
                {"role": "system", "content": system_message},
                {"role": "user", "content": prompt}
            ],
            temperature=0.7
        )
        
        template = response.choices[0].message.content.strip()
        
        if "Dear " + first_name not in template:
            template = f"Dear {first_name},\n\n" + template.split('\n', 1)[1] if '\n' in template else template
            
        if "Best regards,\nAdil" not in template:
            template = template.rsplit('\n', 2)[0] + "\n\nBest regards,\nAdil"
        
        # Add the detected company name and address at the top of the email for your reference    
        template = f"[Detected Company: {detected_company}]\n[Company Address: {company_address}]\n\n" + template
            
        return template
    except Exception as e:
        st.error(f"Error generating email template: {str(e)}")
        return None


def save_email_record(db, profile_data, template):
    try:
        email_record = {
            'recipient_first_name': profile_data['First Name'],
            'recipient_last_name': profile_data['Last Name'],
            'recipient_company': profile_data['Company'],
            'email_template': template,
            'sent_date': datetime.now(),
            'status': 'sent'
        }
        
        result = db.sent_emails.insert_one(email_record)
        return result.inserted_id
    except Exception as e:
        st.error(f"Error saving email record: {str(e)}")
        return None

def send_email(template, db, profile_data):
    try:
        sender_email = "adilinbox4@gmail.com"
        sender_password = "pulv dnov zzfg etcg"
        recipient_email = "adilinbox4@gmail.com"
        
        msg = MIMEMultipart()
        msg['From'] = sender_email
        msg['To'] = recipient_email
        msg['Subject'] = "Meeting Request"

        msg.attach(MIMEText(template, 'plain'))

        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(sender_email, sender_password)
        text = msg.as_string()
        server.sendmail(sender_email, recipient_email, text)
        server.quit()

        # Save record to database after successful send
        record_id = save_email_record(db, profile_data, template)
        
        if record_id:
            # Update profile status in session state
            for profile in st.session_state.search_results:
                if (profile['first_name'] == profile_data['First Name'] and 
                    profile['last_name'] == profile_data['Last Name'] and 
                    profile['company'] == profile_data['Company']):
                    profile['email_status'] = 'Sent'
                    profile['sent_date'] = datetime.now()
            return True
            
        return False
    except Exception as e:
        st.error(f"Failed to send email: {str(e)}")
        return False

def main():
    st.set_page_config(page_title="LinkedIn Profile Explorer", layout="wide")
    
    # Initialize session state variables
    if 'search_results' not in st.session_state:
        st.session_state.search_results = None
    if 'edited_df' not in st.session_state:
        st.session_state.edited_df = None
    if 'email_results' not in st.session_state:
        st.session_state.email_results = []
    if 'selected_templates' not in st.session_state:
        st.session_state.selected_templates = {}
    if 'templates_generated' not in st.session_state:
        st.session_state.templates_generated = False
    if 'edited_templates' not in st.session_state:
        st.session_state.edited_templates = {}
    if 'deleted_templates' not in st.session_state:
        st.session_state.deleted_templates = set()
    if 'selected_coffee_shops' not in st.session_state:
        st.session_state.selected_coffee_shops = {}
    if 'show_templates' not in st.session_state:
        st.session_state.show_templates = False
    if 'company_addresses' not in st.session_state:
        st.session_state.company_addresses = {}
    
    # Initialize OpenAI and MongoDB clients
    openai_client = init_openai()
    db = init_mongodb()
    
    if 'coffee_shop_selections' not in st.session_state:
        st.session_state.coffee_shop_selections = {}
        
    # Add callback function for radio button changes
    def on_coffee_shop_change(template_key, selected_shop, result):
        st.session_state.coffee_shop_selections[template_key] = selected_shop
        current_template = st.session_state.edited_templates.get(template_key, result['Email Template'])
        
        if selected_shop != "No specific coffee shop":
            shop_name = selected_shop.split(" (Rating")[0]
            shop_address = selected_shop.split(" - ")[-1]
            current_template = update_template_with_coffee_shop(current_template, shop_name, shop_address)
        st.session_state.edited_templates[template_key] = current_template

    # Sidebar
    st.sidebar.title("LinkedIn Profile Explorer")
    
    # Search Interface
    st.sidebar.header("Search Profiles")
    
    search_terms = st.sidebar.text_input("Search Keywords (e.g., Healthcare CEO)")
    location = st.sidebar.text_input("Location (Optional)")
    limit = st.sidebar.slider("Number of results", 10, 500, 100)
    
    if st.sidebar.button("Search"):
        with st.spinner("Searching profiles..."):
            profiles = search_profiles(db, search_terms, location, limit)
            if profiles:
                st.session_state.search_results = profiles
                st.session_state.email_results = []
                st.session_state.selected_templates = {}
                st.session_state.templates_generated = False
                st.session_state.edited_templates = {}
                st.session_state.deleted_templates = set()
                st.session_state.show_templates = False
                st.session_state.company_addresses = {}
            else:
                st.session_state.search_results = None
                st.warning("No profiles found matching your search criteria.")
    
    if st.session_state.search_results:
        st.title("Search Results")
        st.success(f"Found {len(st.session_state.search_results)} matching profiles")
        
        df = pd.DataFrame(st.session_state.search_results)
        
        if not df.empty:
            display_cols = {
                'first_name': 'First Name',
                'last_name': 'Last Name',
                'description': 'Description',
                'company': 'Company',
                'location': 'Location',
                'url': 'Profile URL',
                'email_status': 'Email Status',
                'sent_date': 'Sent Date'
            }
            
            df_display = df[display_cols.keys()].rename(columns=display_cols)
            df_display['Description'] = df_display['Description'].apply(
                lambda x: x[:100] + '...' if isinstance(x, str) and len(x) > 100 else x
            )
            
            def format_date(x):
                try:
                    return x.strftime("%Y-%m-%d %H:%M:%S") if pd.notnull(x) and hasattr(x, 'strftime') else ''
                except:
                    return ''

            df_display['Sent Date'] = df_display['Sent Date'].apply(format_date)
            
            # Add Select column and set initial values
            df_display.insert(0, 'Select', False)
            mask = df_display['Email Status'].fillna('').astype(str) == 'Sent'
            df_display.loc[mask, 'Select'] = False

            # Apply conditional styling
            def highlight_sent_rows(row):
                if row['Email Status'] == 'Sent':
                    return ['background-color: #4CAF50; color: black'] * len(row)
                else:
                    return [''] * len(row)

            styled_df = df_display.style.apply(highlight_sent_rows, axis=1)

            st.session_state.edited_df = st.data_editor(
                styled_df,
                hide_index=True,
                disabled=list(display_cols.values()),
                column_config={
                    "Select": st.column_config.CheckboxColumn(
                        "Select",
                        help="Select profiles to fetch emails",
                        default=False,
                    ),
                    "Email Status": st.column_config.Column(
                        "Email Status",
                        help="Shows if an email has been sent to this profile",
                        width="medium"
                    ),
                    "Sent Date": st.column_config.Column(
                        "Sent Date",
                        help="When the email was sent",
                        width="medium"
                    )
                }
            )

        # Generate templates button
        if st.button("Get Emails and Generate Templates"):
            selected_profiles = st.session_state.edited_df[st.session_state.edited_df['Select'] == True]
            
            if selected_profiles.empty:
                st.warning("Please select at least one profile")
            else:
                st.session_state.show_templates = True
                progress_placeholder = st.empty()
                email_results = []
                
                total_profiles = len(selected_profiles)
                
                for idx, (i, row) in enumerate(selected_profiles.iterrows()):
                    progress = min(idx / (total_profiles - 1) if total_profiles > 1 else 1.0, 1.0)
                    progress_placeholder.progress(progress)
                    
                    # First, use GPT to detect the company name
                    company_detection_prompt = f"""
                    I need to identify the most likely company name from the following LinkedIn profile data:
                    
                    Company field: "{row['Company']}"
                    Description field: "{row['Description']}"
                    Location field: "{row['Location']}"
                    
                    Analyze all fields and identify the most likely company name. Return ONLY the company name, nothing else.
                    """
                    
                    company_detection_response = openai_client.chat.completions.create(
                        model="gpt-4-turbo-preview",
                        messages=[
                            {"role": "system", "content": "You extract the most likely company name from LinkedIn profile data."},
                            {"role": "user", "content": company_detection_prompt}
                        ],
                        temperature=0.3
                    )
                    
                    detected_company = company_detection_response.choices[0].message.content.strip()
                    
                    # Use Selenium to get the company's detailed address
                    with st.spinner(f"Looking up address for {detected_company}..."):
                        location_info = get_location_info(detected_company)
                        company_address = location_info.get("address", "")
                        
                        # Store in session state for reuse
                        key = f"{row['First Name']}_{row['Last Name']}_{row['Company']}"
                        st.session_state.company_addresses[key] = company_address
                    
                    # Get coffee shops near the company address
                    try:
                        with st.spinner(f"Finding coffee shops near {detected_company}..."):
                            coffee_shops = asyncio.run(get_coffee_shops(company_address))
                    except Exception as e:
                        coffee_shops = []
                        st.warning(f"Could not fetch coffee shops: {str(e)}")
                    
                    # Generate email template
                    template = generate_email_template(
                        openai_client,
                        {
                            'first_name': row['First Name'],
                            'company': row['Company'],
                            'location': row['Location'],
                            'description': row['Description'],
                            'company_address': company_address
                        },
                        coffee_shops
                    )
                    
                    result = {
                        'First Name': row['First Name'],
                        'Last Name': row['Last Name'],
                        'Company': row['Company'],
                        'Detected Company': detected_company,
                        'Company Address': company_address,
                        'Email Template': template if template else 'Template generation failed',
                        'Nearby Coffee Shops': coffee_shops
                    }
                    
                    email_results.append(result)
                
                progress_placeholder.empty()
                st.session_state.email_results = email_results
                st.session_state.templates_generated = True
                
                # Initialize edited templates with generated content
                for idx, result in enumerate(email_results):
                    template_key = f"template_{idx}"
                    if template_key not in st.session_state.edited_templates:
                        st.session_state.edited_templates[template_key] = result['Email Template']
                    
        # Display templates section
        if st.session_state.show_templates and st.session_state.email_results:
            st.write("### Select Templates to Send")
            
            templates_to_display = [
                (idx, result) for idx, result in enumerate(st.session_state.email_results)
                if f"template_{idx}" not in st.session_state.deleted_templates
            ]

            for idx, result in templates_to_display:
                template_key = f"template_{idx}"
                
                with st.expander(f"📧 {result['First Name']} {result['Last Name']} - {result['Company']}", expanded=True):
                    col1, col2, col3 = st.columns([0.2, 1.6, 0.2])
                    
                    with col1:
                        st.session_state.selected_templates[template_key] = True
                        
                        if result.get('Company Address'):
                            st.info(f"**Company Address:** {result['Company Address']}")

                    with col2:
                        if result.get('Nearby Coffee Shops'):
                            st.write("**Select Coffee Shop for Meeting:**")
                            
                            coffee_shops = result['Nearby Coffee Shops']
                            coffee_shop_options = [
                                f"{shop['name']} (Rating: {shop['rating']}) - {shop['address']}"
                                for shop in coffee_shops
                            ]
                            coffee_shop_options.insert(0, "No specific coffee shop")
                            
                            if template_key not in st.session_state.coffee_shop_selections and coffee_shop_options:
                                st.session_state.coffee_shop_selections[template_key] = coffee_shop_options[1] if len(coffee_shop_options) > 1 else coffee_shop_options[0]
                            
                            if coffee_shop_options:
                                selected_shop = st.radio(
                                    "Choose a coffee shop:",
                                    options=coffee_shop_options,
                                    key=f"coffee_shop_{template_key}",
                                    index=coffee_shop_options.index(st.session_state.coffee_shop_selections.get(template_key, coffee_shop_options[0]))
                                )
                                
                                if selected_shop != st.session_state.coffee_shop_selections.get(template_key):
                                    on_coffee_shop_change(template_key, selected_shop, result)
                        
                        st.write("**Generated Email Template:**")
                        current_template = st.session_state.edited_templates.get(template_key, result['Email Template'])
                        edited_template = st.text_area(
                            "",
                            value=current_template,
                            height=300,
                            key=f"edit_{template_key}"
                        )
                        st.session_state.edited_templates[template_key] = edited_template

                    with col3:
                        if st.button("🗑️", key=f"delete_{template_key}"):
                            st.session_state.deleted_templates.add(template_key)
                            st.rerun()


            if templates_to_display and st.button("Send Selected Templates"):
                success_count = 0
                send_progress = st.progress(0)
                status_text = st.empty()
                
                total_selected = len(templates_to_display)
                
                for i, (idx, result) in enumerate(templates_to_display):
                    template_key = f"template_{idx}"
                    template = st.session_state.edited_templates[template_key]
                    profile_data = st.session_state.email_results[idx]
                    
                    if send_email(template, db, profile_data):
                        success_count += 1
                        st.session_state.deleted_templates.add(template_key)
                    
                    progress = (i + 1) / total_selected
                    send_progress.progress(progress)
                    status_text.text(f"Sending emails: {i + 1}/{total_selected}")
                
                send_progress.empty()
                status_text.empty()
                
                if success_count > 0:
                    st.success(f"Successfully sent {success_count} out of {total_selected} templates!")
                    st.rerun()
                if success_count < total_selected:
                    st.warning(f"Failed to send {total_selected - success_count} templates. Please check the errors above.")

        st.sidebar.markdown("---")
        st.sidebar.markdown("### About")
        st.sidebar.info(
            "This application allows you to search LinkedIn profiles and generate meeting request templates."
        )


    
if __name__ == "__main__":
    main()