SarowarSaurav commited on
Commit
6989a37
1 Parent(s): 363eb22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -53
app.py CHANGED
@@ -1,61 +1,76 @@
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()
 
 
1
  import pandas as pd
2
+ import gradio as gr
3
+
4
+ # Function to create a vCard entry from a DataFrame row
5
+ def create_vcf(row):
6
+ vcf_template = """BEGIN:VCARD
7
+ VERSION:3.0
8
+ PRODID:-//Apple Inc.//iPhone OS 17.2.1//EN
9
+ N:{name}
10
+ FN:{name}
11
+ ORG:BAT Bangladesh;{function}
12
+ TITLE:{designation}
13
+ TEL;TYPE=CELL;TYPE=VOICE;TYPE=pref:{number}
14
+ EMAIL:{email}
15
+ END:VCARD"""
16
+ # Formatting the row data into vCard format
17
+ return vcf_template.format(
18
+ name=row['Name'],
19
+ function=row['Function'],
20
+ designation=row['Designation'],
21
+ number=row['Number'],
22
+ email=row['Email']
23
+ )
24
 
25
+ # Function to generate the VCF file from the uploaded Excel data
26
+ def generate_vcf(file):
27
  # Load Excel file
28
+ df = pd.read_excel(file)
29
+ df['Number'] = df['Number'].astype(str) # Convert phone numbers to string to avoid formatting issues
30
 
31
+ # Combine each vCard entry into a single VCF file content
32
+ vcf_data = df.apply(create_vcf, axis=1).str.cat(sep='\n\n')
33
+
34
+ # Write the VCF data to a file
35
+ vcf_file_name = '/tmp/BAT_New_Contacts.vcf'
36
+ with open(vcf_file_name, 'w') as vcf_file:
37
+ vcf_file.write(vcf_data)
38
 
39
+ return vcf_file_name
40
+
41
+ # Custom CSS and HTML for a refined UI
42
+ css = """
43
+ .gradio-container {
44
+ background: rgb(14, 43, 99);
45
+ display: flex;
46
+ flex-direction: column;
47
+ align-items: center;
48
+ color: #fff;
49
+ padding: 20px;
50
+ }
51
+ .gradio-input, .gradio-output {
52
+ background: rgb(28, 56, 113);
53
+ color: #fff;
54
+ }
55
+ footer {
56
+ display: none !important;
57
+ }
58
+ """
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ html_content = """
61
+ <div style="text-align: center; margin-bottom: 20px;">
62
+ <img src="https://i.ibb.co/RbQRzcy/APMEA-CENTRAL-White.png" border="0" alt='BAT Bangladesh Logo' style='max-width: 300px;'>
63
+ </div>
64
+ <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>
65
+ """
66
 
67
+ # Gradio Interface
68
+ with gr.Blocks(css=css) as demo:
69
+ gr.HTML(html_content)
70
+ gr.Interface(
71
+ fn=generate_vcf,
72
+ inputs=gr.File(label="Upload Excel File"),
73
+ outputs=gr.File(label="Download VCF File"),
74
+ )
75
 
76
+ demo.launch()