Teddy Xinyuan Chen
2024-05-19T13-51-15Z
146153c unverified
import gradio as gr
# Mapping of letters to digits as per a standard phone keypad
keypad_mapping = {
"A": "2",
"B": "2",
"C": "2",
"D": "3",
"E": "3",
"F": "3",
"G": "4",
"H": "4",
"I": "4",
"J": "5",
"K": "5",
"L": "5",
"M": "6",
"N": "6",
"O": "6",
"P": "7",
"Q": "7",
"R": "7",
"S": "7",
"T": "8",
"U": "8",
"V": "8",
"W": "9",
"X": "9",
"Y": "9",
"Z": "9",
}
# Function to convert the phone number
def convert_phone_number(phone_number, trim_to_10):
numerical_phone_number = ""
digit_count = 0
ignore_first_digit = phone_number.startswith("1")
for char in phone_number:
if char.isalpha():
numerical_phone_number += keypad_mapping[char.upper()]
if not ignore_first_digit:
digit_count += 1
ignore_first_digit = False
else:
numerical_phone_number += char
if char.isdigit() and not (ignore_first_digit and digit_count == 0):
digit_count += 1
ignore_first_digit = False
if trim_to_10 and digit_count >= 10:
break
return numerical_phone_number
# Create Gradio interface
iface = gr.Interface(
fn=convert_phone_number,
inputs=[
gr.Textbox(label="Enter Phone Number with Letters"),
gr.Checkbox(label="Trim to 10 digits"),
],
outputs=gr.Textbox(label="Numerical Phone Number"),
examples=[
["1-800-TEDDYSC", True],
["1-800-MY-APPLE", False],
["1-800-Fidelity", True],
],
title="Phone Number Converter | Made by <a href='https://teddysc.me'>Teddy</a>",
description="Convert a phone number with letters to its numerical equivalent. For example, '1-800-TEDDYSC' becomes '1-800-8333972'.",
)
# Launch the app
iface.launch(
share=True,
)