Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,6 @@ import io
|
|
| 7 |
from urllib.parse import urlparse, urljoin
|
| 8 |
import time
|
| 9 |
import random
|
| 10 |
-
import pandas as pd
|
| 11 |
-
import openpyxl
|
| 12 |
-
from io import BytesIO
|
| 13 |
|
| 14 |
# Mock contacts database (same as your API)
|
| 15 |
CONTACTS_DB = [
|
|
@@ -301,53 +298,6 @@ def parse_csv_file(file_content):
|
|
| 301 |
print(f"Error parsing CSV: {e}")
|
| 302 |
return []
|
| 303 |
|
| 304 |
-
def parse_excel_file(file_path):
|
| 305 |
-
"""Parse Excel file and extract website URLs from column H, fill contact info in specific columns"""
|
| 306 |
-
try:
|
| 307 |
-
# Read Excel file
|
| 308 |
-
workbook = openpyxl.load_workbook(file_path)
|
| 309 |
-
sheet = workbook.active
|
| 310 |
-
|
| 311 |
-
# Process each row
|
| 312 |
-
for row_num in range(2, sheet.max_row + 1): # Start from row 2 to skip header
|
| 313 |
-
website_cell = sheet[f'H{row_num}']
|
| 314 |
-
website_url = website_cell.value
|
| 315 |
-
|
| 316 |
-
if website_url and str(website_url).strip():
|
| 317 |
-
website_url = str(website_url).strip()
|
| 318 |
-
print(f"Processing website: {website_url}")
|
| 319 |
-
|
| 320 |
-
# Find contacts for this website
|
| 321 |
-
contacts = simulate_website_scraping(website_url)
|
| 322 |
-
|
| 323 |
-
if contacts:
|
| 324 |
-
# Fill first contact info
|
| 325 |
-
first_contact = contacts[0]
|
| 326 |
-
sheet[f'I{row_num}'] = first_contact['first_name'] # Contact First Name
|
| 327 |
-
sheet[f'J{row_num}'] = first_contact['last_name'] # Contact Last Name
|
| 328 |
-
sheet[f'K{row_num}'] = first_contact['job_title'] # Job Title
|
| 329 |
-
sheet[f'L{row_num}'] = first_contact['phone'] # Phone
|
| 330 |
-
sheet[f'M{row_num}'] = first_contact['email'] # Email
|
| 331 |
-
|
| 332 |
-
# Fill second contact info if available
|
| 333 |
-
if len(contacts) > 1:
|
| 334 |
-
second_contact = contacts[1]
|
| 335 |
-
sheet[f'S{row_num}'] = second_contact['first_name'] # Second Contact First Name
|
| 336 |
-
sheet[f'T{row_num}'] = second_contact['last_name'] # Second Contact Last Name
|
| 337 |
-
sheet[f'U{row_num}'] = second_contact['job_title'] # Second Contact Job Title
|
| 338 |
-
sheet[f'V{row_num}'] = second_contact['phone'] # Second Contact Phone
|
| 339 |
-
sheet[f'W{row_num}'] = second_contact['email'] # Second Contact Email
|
| 340 |
-
|
| 341 |
-
# Save the modified Excel file
|
| 342 |
-
output_path = file_path.replace('.xlsx', '_with_contacts.xlsx')
|
| 343 |
-
workbook.save(output_path)
|
| 344 |
-
|
| 345 |
-
return output_path, len([row for row in range(2, sheet.max_row + 1) if sheet[f'H{row}'].value])
|
| 346 |
-
|
| 347 |
-
except Exception as e:
|
| 348 |
-
print(f"Error processing Excel file: {e}")
|
| 349 |
-
return None, 0
|
| 350 |
-
|
| 351 |
def search_csv_websites(csv_file, max_results=10):
|
| 352 |
"""Search for contacts from websites listed in CSV file"""
|
| 353 |
if csv_file is None:
|
|
@@ -416,46 +366,6 @@ def search_csv_websites(csv_file, max_results=10):
|
|
| 416 |
except Exception as e:
|
| 417 |
return f"Error processing CSV file: {str(e)}", ""
|
| 418 |
|
| 419 |
-
def search_excel_websites(excel_file, max_results=10):
|
| 420 |
-
"""Search for contacts from websites listed in Excel file column H and fill contact info"""
|
| 421 |
-
if excel_file is None:
|
| 422 |
-
return "Please upload an Excel file", ""
|
| 423 |
-
|
| 424 |
-
try:
|
| 425 |
-
# Process Excel file
|
| 426 |
-
output_path, total_websites = parse_excel_file(excel_file.name)
|
| 427 |
-
|
| 428 |
-
if not output_path:
|
| 429 |
-
return "Error processing Excel file. Please ensure your Excel file has websites in column H.", ""
|
| 430 |
-
|
| 431 |
-
# Format results
|
| 432 |
-
results_text = f"EXCEL CONTACT DISCOVERY REPORT\n"
|
| 433 |
-
results_text += f"Total Websites Processed: {total_websites}\n"
|
| 434 |
-
results_text += f"Modified Excel File: {output_path}\n"
|
| 435 |
-
results_text += f"{'='*60}\n\n"
|
| 436 |
-
results_text += f"Contact information has been filled in the following columns:\n"
|
| 437 |
-
results_text += f"• Column I: Contact First Name\n"
|
| 438 |
-
results_text += f"• Column J: Contact Last Name\n"
|
| 439 |
-
results_text += f"• Column K: Job Title\n"
|
| 440 |
-
results_text += f"• Column L: Phone\n"
|
| 441 |
-
results_text += f"• Column M: Email\n\n"
|
| 442 |
-
results_text += f"Second contact information (if available):\n"
|
| 443 |
-
results_text += f"• Column S: Second Contact First Name\n"
|
| 444 |
-
results_text += f"• Column T: Second Contact Last Name\n"
|
| 445 |
-
results_text += f"• Column U: Second Contact Job Title\n"
|
| 446 |
-
results_text += f"• Column V: Second Contact Phone\n"
|
| 447 |
-
results_text += f"• Column W: Second Contact Email\n\n"
|
| 448 |
-
results_text += f"The modified Excel file has been saved as: {output_path}\n"
|
| 449 |
-
results_text += f"You can download it from the file system."
|
| 450 |
-
|
| 451 |
-
# Create a simple export message
|
| 452 |
-
export_output = f"Excel file processed successfully.\nModified file saved as: {output_path}\nTotal websites processed: {total_websites}"
|
| 453 |
-
|
| 454 |
-
return results_text, export_output
|
| 455 |
-
|
| 456 |
-
except Exception as e:
|
| 457 |
-
return f"Error processing Excel file: {str(e)}", ""
|
| 458 |
-
|
| 459 |
def search_website_contacts(website_url, max_results=10):
|
| 460 |
"""Main function to search for contacts on a website"""
|
| 461 |
if not website_url:
|
|
@@ -688,7 +598,7 @@ with gr.Blocks(css=custom_css, title="Contact Discovery Platform", theme=gr.them
|
|
| 688 |
with gr.TabItem("CSV Bulk Search"):
|
| 689 |
with gr.Row():
|
| 690 |
with gr.Column(scale=2):
|
| 691 |
-
gr.HTML('<div class="section-header">
|
| 692 |
|
| 693 |
csv_file = gr.File(
|
| 694 |
label="Upload CSV File",
|
|
@@ -696,22 +606,12 @@ with gr.Blocks(css=custom_css, title="Contact Discovery Platform", theme=gr.them
|
|
| 696 |
elem_classes=["custom-input"]
|
| 697 |
)
|
| 698 |
|
| 699 |
-
excel_file = gr.File(
|
| 700 |
-
label="Upload Excel File",
|
| 701 |
-
file_types=[".xlsx", ".xls"],
|
| 702 |
-
elem_classes=["custom-input"]
|
| 703 |
-
)
|
| 704 |
-
|
| 705 |
gr.HTML("""
|
| 706 |
<div style="background: #f8fafc; padding: 15px; border-radius: 8px; border-left: 4px solid #1e40af; margin: 10px 0;">
|
| 707 |
<strong>CSV Format Requirements:</strong><br>
|
| 708 |
• Include a column named 'website', 'url', or 'domain'<br>
|
| 709 |
• One website per row<br>
|
| 710 |
-
• Example: techflowsolutions.com, greenleafconsult.com
|
| 711 |
-
<strong>Excel Format Requirements:</strong><br>
|
| 712 |
-
• Websites should be in column H<br>
|
| 713 |
-
• Contact info will be filled in columns I-M (first contact) and S-W (second contact)<br>
|
| 714 |
-
• The modified file will be saved with '_with_contacts' suffix
|
| 715 |
</div>
|
| 716 |
""")
|
| 717 |
|
|
@@ -721,7 +621,7 @@ with gr.Blocks(css=custom_css, title="Contact Discovery Platform", theme=gr.them
|
|
| 721 |
maximum=50,
|
| 722 |
value=20,
|
| 723 |
step=1,
|
| 724 |
-
label="Maximum Results
|
| 725 |
elem_classes=["custom-input"]
|
| 726 |
)
|
| 727 |
|
|
@@ -731,19 +631,12 @@ with gr.Blocks(css=custom_css, title="Contact Discovery Platform", theme=gr.them
|
|
| 731 |
size="lg",
|
| 732 |
elem_classes=["primary-btn"]
|
| 733 |
)
|
| 734 |
-
|
| 735 |
-
excel_search_btn = gr.Button(
|
| 736 |
-
"Process Excel",
|
| 737 |
-
variant="primary",
|
| 738 |
-
size="lg",
|
| 739 |
-
elem_classes=["primary-btn"]
|
| 740 |
-
)
|
| 741 |
|
| 742 |
-
gr.HTML('<div class="section-header">
|
| 743 |
|
| 744 |
with gr.Row():
|
| 745 |
csv_results_display = gr.Textbox(
|
| 746 |
-
label="
|
| 747 |
lines=18,
|
| 748 |
max_lines=35,
|
| 749 |
show_copy_button=True,
|
|
@@ -751,7 +644,7 @@ with gr.Blocks(css=custom_css, title="Contact Discovery Platform", theme=gr.them
|
|
| 751 |
)
|
| 752 |
|
| 753 |
csv_export_output = gr.Textbox(
|
| 754 |
-
label="Export Data
|
| 755 |
lines=18,
|
| 756 |
max_lines=35,
|
| 757 |
show_copy_button=True,
|
|
@@ -760,4 +653,74 @@ with gr.Blocks(css=custom_css, title="Contact Discovery Platform", theme=gr.them
|
|
| 760 |
|
| 761 |
# Sample websites section
|
| 762 |
with gr.Accordion("Sample Websites Database", open=False):
|
| 763 |
-
gr.HTML('<div style="background: #f8fafc; padding: 15px; border-radius: 8px; border-left: 4px solid #1e40af
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from urllib.parse import urlparse, urljoin
|
| 8 |
import time
|
| 9 |
import random
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Mock contacts database (same as your API)
|
| 12 |
CONTACTS_DB = [
|
|
|
|
| 298 |
print(f"Error parsing CSV: {e}")
|
| 299 |
return []
|
| 300 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
def search_csv_websites(csv_file, max_results=10):
|
| 302 |
"""Search for contacts from websites listed in CSV file"""
|
| 303 |
if csv_file is None:
|
|
|
|
| 366 |
except Exception as e:
|
| 367 |
return f"Error processing CSV file: {str(e)}", ""
|
| 368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
def search_website_contacts(website_url, max_results=10):
|
| 370 |
"""Main function to search for contacts on a website"""
|
| 371 |
if not website_url:
|
|
|
|
| 598 |
with gr.TabItem("CSV Bulk Search"):
|
| 599 |
with gr.Row():
|
| 600 |
with gr.Column(scale=2):
|
| 601 |
+
gr.HTML('<div class="section-header">CSV Upload</div>')
|
| 602 |
|
| 603 |
csv_file = gr.File(
|
| 604 |
label="Upload CSV File",
|
|
|
|
| 606 |
elem_classes=["custom-input"]
|
| 607 |
)
|
| 608 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 609 |
gr.HTML("""
|
| 610 |
<div style="background: #f8fafc; padding: 15px; border-radius: 8px; border-left: 4px solid #1e40af; margin: 10px 0;">
|
| 611 |
<strong>CSV Format Requirements:</strong><br>
|
| 612 |
• Include a column named 'website', 'url', or 'domain'<br>
|
| 613 |
• One website per row<br>
|
| 614 |
+
• Example: techflowsolutions.com, greenleafconsult.com
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
</div>
|
| 616 |
""")
|
| 617 |
|
|
|
|
| 621 |
maximum=50,
|
| 622 |
value=20,
|
| 623 |
step=1,
|
| 624 |
+
label="Maximum Results",
|
| 625 |
elem_classes=["custom-input"]
|
| 626 |
)
|
| 627 |
|
|
|
|
| 631 |
size="lg",
|
| 632 |
elem_classes=["primary-btn"]
|
| 633 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
|
| 635 |
+
gr.HTML('<div class="section-header">CSV Results</div>')
|
| 636 |
|
| 637 |
with gr.Row():
|
| 638 |
csv_results_display = gr.Textbox(
|
| 639 |
+
label="CSV Processing Report",
|
| 640 |
lines=18,
|
| 641 |
max_lines=35,
|
| 642 |
show_copy_button=True,
|
|
|
|
| 644 |
)
|
| 645 |
|
| 646 |
csv_export_output = gr.Textbox(
|
| 647 |
+
label="Export Data (CSV Format)",
|
| 648 |
lines=18,
|
| 649 |
max_lines=35,
|
| 650 |
show_copy_button=True,
|
|
|
|
| 653 |
|
| 654 |
# Sample websites section
|
| 655 |
with gr.Accordion("Sample Websites Database", open=False):
|
| 656 |
+
gr.HTML('<div style="background: #f8fafc; padding: 15px; border-radius: 8px; border-left: 4px solid #1e40af;">')
|
| 657 |
+
sample_websites = gr.Textbox(
|
| 658 |
+
label="Available Websites in Database",
|
| 659 |
+
value=get_all_available_websites(),
|
| 660 |
+
lines=8,
|
| 661 |
+
interactive=False,
|
| 662 |
+
elem_classes=["custom-input"]
|
| 663 |
+
)
|
| 664 |
+
gr.HTML('</div>')
|
| 665 |
+
|
| 666 |
+
# Quick search buttons
|
| 667 |
+
gr.HTML('<div class="section-header">Quick Access Sample Websites</div>')
|
| 668 |
+
|
| 669 |
+
with gr.Row():
|
| 670 |
+
quick_btn1 = gr.Button("TechFlow Solutions", size="sm", elem_classes=["secondary-btn"])
|
| 671 |
+
quick_btn2 = gr.Button("GreenLeaf Consulting", size="sm", elem_classes=["secondary-btn"])
|
| 672 |
+
quick_btn3 = gr.Button("BlueSky Marketing", size="sm", elem_classes=["secondary-btn"])
|
| 673 |
+
quick_btn4 = gr.Button("Quantum Dynamics", size="sm", elem_classes=["secondary-btn"])
|
| 674 |
+
|
| 675 |
+
with gr.Row():
|
| 676 |
+
quick_btn5 = gr.Button("Stellar Logistics", size="sm", elem_classes=["secondary-btn"])
|
| 677 |
+
quick_btn6 = gr.Button("Nexus Financial", size="sm", elem_classes=["secondary-btn"])
|
| 678 |
+
quick_btn7 = gr.Button("Horizon Health", size="sm", elem_classes=["secondary-btn"])
|
| 679 |
+
quick_btn8 = gr.Button("Phoenix Manufacturing", size="sm", elem_classes=["secondary-btn"])
|
| 680 |
+
|
| 681 |
+
# Event handlers
|
| 682 |
+
search_btn.click(
|
| 683 |
+
fn=search_website_contacts,
|
| 684 |
+
inputs=[website_input, max_results],
|
| 685 |
+
outputs=[results_display, csv_output]
|
| 686 |
+
)
|
| 687 |
+
|
| 688 |
+
csv_search_btn.click(
|
| 689 |
+
fn=search_csv_websites,
|
| 690 |
+
inputs=[csv_file, csv_max_results],
|
| 691 |
+
outputs=[csv_results_display, csv_export_output]
|
| 692 |
+
)
|
| 693 |
+
|
| 694 |
+
# Quick search button handlers
|
| 695 |
+
quick_btn1.click(lambda: "techflowsolutions.com", outputs=website_input)
|
| 696 |
+
quick_btn2.click(lambda: "greenleafconsult.com", outputs=website_input)
|
| 697 |
+
quick_btn3.click(lambda: "blueskymarketing.net", outputs=website_input)
|
| 698 |
+
quick_btn4.click(lambda: "quantumdynamics.org", outputs=website_input)
|
| 699 |
+
quick_btn5.click(lambda: "stellarlogistics.biz", outputs=website_input)
|
| 700 |
+
quick_btn6.click(lambda: "nexusfinancial.pro", outputs=website_input)
|
| 701 |
+
quick_btn7.click(lambda: "horizonhealth.care", outputs=website_input)
|
| 702 |
+
quick_btn8.click(lambda: "phoenixmfg.com", outputs=website_input)
|
| 703 |
+
|
| 704 |
+
# Examples
|
| 705 |
+
gr.Examples(
|
| 706 |
+
examples=[
|
| 707 |
+
["techflowsolutions.com", 5],
|
| 708 |
+
["greenleafconsult.com", 3],
|
| 709 |
+
["blueskymarketing.net", 4],
|
| 710 |
+
["quantumdynamics.org", 6]
|
| 711 |
+
],
|
| 712 |
+
inputs=[website_input, max_results],
|
| 713 |
+
label="Sample Searches"
|
| 714 |
+
)
|
| 715 |
+
|
| 716 |
+
# Footer
|
| 717 |
+
gr.HTML("""
|
| 718 |
+
<div style="text-align: center; padding: 30px 20px; background: linear-gradient(135deg, #64748b 0%, #475569 100%); color: white; border-radius: 15px; margin-top: 30px;">
|
| 719 |
+
<h3 style="margin: 0 0 10px 0;">Contact Intelligence Platform</h3>
|
| 720 |
+
<p style="margin: 0; opacity: 0.9;">Professional-grade contact discovery and lead generation technology</p>
|
| 721 |
+
<p style="margin: 10px 0 0 0; font-size: 0.9em; opacity: 0.7;">Powered by advanced web intelligence algorithms</p>
|
| 722 |
+
</div>
|
| 723 |
+
""")
|
| 724 |
+
|
| 725 |
+
if __name__ == "__main__":
|
| 726 |
+
app.launch()
|