SarowarSaurav commited on
Commit
1a4514d
1 Parent(s): 539accd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -60
app.py CHANGED
@@ -1,67 +1,61 @@
1
- import pandas as pd
2
  import gradio as gr
 
 
 
3
 
4
- def create_vcf(row):
5
- vcf_template = """BEGIN:VCARD
6
- VERSION:3.0
7
- PRODID:-//Apple Inc.//iPhone OS 17.2.1//EN
8
- N:{name}
9
- ORG:BAT Bangladesh;{function}
10
- TITLE:{designation}
11
- TEL;type=CELL;type=VOICE;type=pref:{number}
12
- EMAIL:{email}
13
- END:VCARD"""
14
- return vcf_template.format(
15
- name=row['Name'],
16
- function=row['Function'],
17
- designation=row['Designation'],
18
- number=row['Number'],
19
- email=row['Email']
20
- )
21
-
22
- def generate_vcf(file):
23
- df = pd.read_excel(file)
24
- df['Number'] = df['Number'].astype(str) # Ensure phone numbers are strings
25
- vcf_data = df.apply(create_vcf, axis=1).str.cat(sep='\n\n')
26
 
27
- vcf_file_name = '/tmp/BAT New Contacts.vcf'
28
- with open(vcf_file_name, 'w') as vcf_file:
29
- vcf_file.write(vcf_data)
30
-
31
- return vcf_file_name
32
 
33
- # Define custom CSS and HTML
34
- css = """
35
- .gradio-container {
36
- background: rgb(14, 43, 99);
37
- display: flex;
38
- flex-direction: column;
39
- align-items: center;
40
- color: #fff; /* Text color for better readability */
41
- padding: 20px; /* Padding for the container */
42
- }
43
- .gradio-input, .gradio-output {
44
- background: rgb(28, 56, 113);
45
- color: #fff; /* Text color for input and output fields */
46
- }
47
- footer {
48
- display: none !important;
49
- }
50
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- html_content = """
53
- <div style="text-align: center; margin-bottom: 20px;">
54
- <img src="https://i.ibb.co/RbQRzcy/APMEA-CENTRAL-White.png" border="0" alt='BAT Bangladesh Logo' style='max-width: 300px;'>
55
- </div>
56
- <p style="text-align: center; color: #fff;">Upload an Excel file containing contact information in the following format: Name, Designation, Function, GRADE, Email, Number. The output will be a VCF file containing the contact information.</p>
57
- """
58
 
59
- with gr.Blocks(css=css) as demo:
60
- gr.HTML(html_content)
61
- gr.Interface(
62
- fn=generate_vcf,
63
- inputs=gr.File(label="Upload Excel File"),
64
- outputs=gr.File(label="Download VCF File"),
65
- )
 
66
 
67
- demo.launch()
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ import vobject
4
+ from io import BytesIO
5
 
6
+ def excel_to_vcf(file):
7
+ # Load Excel file
8
+ df = pd.read_excel(file.name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ # Prepare a dictionary to check for existing emails
11
+ contact_dict = {}
 
 
 
12
 
13
+ # Create a vCard file in memory
14
+ vcf_output = BytesIO()
15
+
16
+ # Loop through each row in the DataFrame
17
+ for _, row in df.iterrows():
18
+ name = row['Name']
19
+ designation = row['Designation']
20
+ function = row['Function']
21
+ grade = row['GRADE']
22
+ email = row['Email']
23
+ number = str(row['Number'])
24
+
25
+ # If email already exists, replace the contact details
26
+ if email in contact_dict:
27
+ contact_dict[email]['tel'].value = number
28
+ else:
29
+ # Create a new vCard entry
30
+ contact = vobject.vCard()
31
+ contact.add('fn').value = name
32
+ contact.add('title').value = designation
33
+ contact.add('org').value = function
34
+ contact.add('note').value = f"GRADE: {grade}"
35
+
36
+ email_entry = contact.add('email')
37
+ email_entry.value = email
38
+ email_entry.type_param = 'INTERNET'
39
+
40
+ tel_entry = contact.add('tel')
41
+ tel_entry.value = number
42
+ tel_entry.type_param = 'CELL'
43
+
44
+ # Add contact to dictionary and write to VCF
45
+ contact_dict[email] = contact
46
+ vcf_output.write(contact.serialize().encode('utf-8'))
47
 
48
+ # Return the generated VCF file
49
+ vcf_output.seek(0)
50
+ return vcf_output, "contacts.vcf"
 
 
 
51
 
52
+ # Set up Gradio interface
53
+ iface = gr.Interface(
54
+ fn=excel_to_vcf,
55
+ inputs=gr.inputs.File(label="Upload Excel File (Format: Name, Designation, Function, GRADE, Email, Number)"),
56
+ outputs=gr.outputs.File(label="Download VCF File"),
57
+ title="Excel to VCF Converter",
58
+ description="Upload an Excel file with contact information to generate a VCF file. Contacts with matching email IDs will have their phone numbers updated."
59
+ )
60
 
61
+ iface.launch()