Spaces:
Sleeping
Sleeping
SarowarSaurav
commited on
Commit
•
6989a37
1
Parent(s):
363eb22
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,76 @@
|
|
1 |
-
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
import
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
|
|
7 |
# Load Excel file
|
8 |
-
df = pd.read_excel(file
|
|
|
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 |
-
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 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
)
|
60 |
|
61 |
-
|
|
|
|
|
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()
|