Spaces:
Sleeping
Sleeping
import pandas as pd | |
import gradio as gr | |
import hashlib | |
from datetime import datetime | |
# Function to create a vCard entry with UID and REV fields | |
def create_vcf(row): | |
# Generate a unique UID based on the email (using a hash for consistency) | |
email = row['Email'] | |
uid = hashlib.md5(email.encode()).hexdigest() | |
rev = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # Current timestamp for revision | |
vcf_template = """BEGIN:VCARD | |
VERSION:3.0 | |
PRODID:-//Apple Inc.//iPhone OS 17.2.1//EN | |
N:{name} | |
FN:{name} | |
ORG:BAT Bangladesh;{function} | |
TITLE:{designation} | |
TEL;TYPE=CELL;TYPE=VOICE;TYPE=pref:{number} | |
EMAIL:{email} | |
UID:{uid} | |
REV:{rev} | |
END:VCARD""" | |
return vcf_template.format( | |
name=row['Name'], | |
function=row['Function'], | |
designation=row['Designation'], | |
number=row['Number'], | |
email=email, | |
uid=uid, | |
rev=rev | |
) | |
# Function to generate the VCF file | |
def generate_vcf(file): | |
# Load the Excel file | |
df = pd.read_excel(file) | |
df['Number'] = df['Number'].astype(str) # Ensure phone numbers are strings | |
# Generate the VCF data | |
vcf_data = df.apply(create_vcf, axis=1).str.cat(sep='\n\n') | |
# Write the VCF data to a file | |
vcf_file_name = '/tmp/BAT_New_Contacts.vcf' | |
with open(vcf_file_name, 'w') as vcf_file: | |
vcf_file.write(vcf_data) | |
return vcf_file_name | |
# Custom CSS and HTML | |
css = """ | |
.gradio-container { | |
background: rgb(14, 43, 99); | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
color: #fff; | |
padding: 20px; | |
} | |
.gradio-input, .gradio-output { | |
background: rgb(28, 56, 113); | |
color: #fff; | |
} | |
footer { | |
display: none !important; | |
} | |
""" | |
html_content = """ | |
<div style="text-align: center; margin-bottom: 20px;"> | |
<img src="https://i.ibb.co/RbQRzcy/APMEA-CENTRAL-White.png" border="0" alt='BAT Bangladesh Logo' style='max-width: 300px;'> | |
</div> | |
<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> | |
""" | |
# Gradio Interface | |
with gr.Blocks(css=css) as demo: | |
gr.HTML(html_content) | |
gr.Interface( | |
fn=generate_vcf, | |
inputs=gr.File(label="Upload Excel File"), | |
outputs=gr.File(label="Download VCF File"), | |
) | |
demo.launch() | |