Spaces:
Sleeping
Sleeping
atifsial123
commited on
Commit
•
295ef96
1
Parent(s):
6258046
Update app.py
Browse files
app.py
CHANGED
@@ -27,26 +27,39 @@ def load_dataset():
|
|
27 |
print(f"Error loading file: {e}")
|
28 |
return None
|
29 |
|
30 |
-
# Function to get details based on the name
|
31 |
-
def get_details(name, df):
|
32 |
# Clean the 'Name' column for consistent searching
|
33 |
df['Name'] = df['Name'].astype(str).str.strip().str.lower()
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
if not results.empty:
|
40 |
print(f"Found Details: {results.to_dict('records')}") # Debugging output
|
41 |
return results.to_dict('records')[0] # Return the first match for simplicity
|
42 |
else:
|
43 |
-
print("
|
44 |
return None
|
45 |
|
46 |
# Function to format the output in HTML
|
47 |
def format_output(details):
|
48 |
if details is None:
|
49 |
-
return "
|
50 |
|
51 |
# Build HTML table for a cleaner presentation
|
52 |
html = "<table style='border-collapse: collapse; width: 100%;'>"
|
@@ -66,17 +79,20 @@ def add_or_update_details(name, phone_number, current_occupation, pass_out_year)
|
|
66 |
if df is None:
|
67 |
return "Error loading data."
|
68 |
|
69 |
-
# Find the entry for the name
|
70 |
-
details = get_details(name, df)
|
71 |
|
72 |
if details:
|
73 |
# Update the phone number, current occupation, and pass out year
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
77 |
message = "Phone number, occupation, and pass out year updated successfully."
|
78 |
else:
|
79 |
-
# Add a new entry if the name was not found
|
80 |
new_entry = {
|
81 |
'Name': name,
|
82 |
'Contact Number (Whatsapp)': phone_number,
|
@@ -103,8 +119,8 @@ def predict_and_update(name, phone_number=None, current_occupation=None, pass_ou
|
|
103 |
if df is None:
|
104 |
return "Error loading the file."
|
105 |
|
106 |
-
# Get details for the provided name
|
107 |
-
details = get_details(name, df)
|
108 |
details_html = format_output(details)
|
109 |
|
110 |
# If a phone number, current occupation, or pass out year is provided, update or add the details
|
@@ -121,16 +137,19 @@ def predict_and_update(name, phone_number=None, current_occupation=None, pass_ou
|
|
121 |
iface = gr.Interface(
|
122 |
fn=predict_and_update,
|
123 |
inputs=[
|
124 |
-
gr.Textbox(lines=1, label="**Enter Name**"),
|
125 |
-
gr.Textbox(lines=1, label="**Phone Number** (Optional
|
126 |
gr.Textbox(lines=1, label="**Current Occupation** (Optional - to add/update)"),
|
127 |
gr.Textbox(lines=1, label="**Pass Out Year** (Optional - to add/update)")
|
128 |
],
|
129 |
outputs="html",
|
130 |
-
title="Name
|
131 |
-
description="Enter a name to find
|
132 |
)
|
133 |
|
134 |
# Run the Gradio interface
|
135 |
if __name__ == "__main__":
|
136 |
iface.launch()
|
|
|
|
|
|
|
|
27 |
print(f"Error loading file: {e}")
|
28 |
return None
|
29 |
|
30 |
+
# Function to get details based on the name or phone number
|
31 |
+
def get_details(name, phone_number, df):
|
32 |
# Clean the 'Name' column for consistent searching
|
33 |
df['Name'] = df['Name'].astype(str).str.strip().str.lower()
|
34 |
+
df['Contact Number (Whatsapp)'] = df['Contact Number (Whatsapp)'].astype(str).str.strip()
|
35 |
|
36 |
+
name = name.strip().lower() if name else None
|
37 |
+
phone_number = phone_number.strip() if phone_number else None
|
38 |
+
|
39 |
+
print(f"Searching for Name: {name}, Phone Number: {phone_number}") # Debugging output
|
40 |
+
|
41 |
+
# Filter based on the provided inputs
|
42 |
+
if name and phone_number:
|
43 |
+
results = df[(df['Name'].str.contains(name, case=False, na=False)) |
|
44 |
+
(df['Contact Number (Whatsapp)'] == phone_number)]
|
45 |
+
elif name:
|
46 |
+
results = df[df['Name'].str.contains(name, case=False, na=False)]
|
47 |
+
elif phone_number:
|
48 |
+
results = df[df['Contact Number (Whatsapp)'] == phone_number]
|
49 |
+
else:
|
50 |
+
return None # If neither is provided, return None
|
51 |
|
52 |
if not results.empty:
|
53 |
print(f"Found Details: {results.to_dict('records')}") # Debugging output
|
54 |
return results.to_dict('records')[0] # Return the first match for simplicity
|
55 |
else:
|
56 |
+
print("No matching records found.") # Debugging output
|
57 |
return None
|
58 |
|
59 |
# Function to format the output in HTML
|
60 |
def format_output(details):
|
61 |
if details is None:
|
62 |
+
return "No matching records found."
|
63 |
|
64 |
# Build HTML table for a cleaner presentation
|
65 |
html = "<table style='border-collapse: collapse; width: 100%;'>"
|
|
|
79 |
if df is None:
|
80 |
return "Error loading data."
|
81 |
|
82 |
+
# Find the entry for the name or phone number
|
83 |
+
details = get_details(name, phone_number, df)
|
84 |
|
85 |
if details:
|
86 |
# Update the phone number, current occupation, and pass out year
|
87 |
+
if phone_number:
|
88 |
+
df.loc[df['Name'].str.lower() == name.strip().lower(), 'Contact Number (Whatsapp)'] = phone_number
|
89 |
+
if current_occupation:
|
90 |
+
df.loc[df['Name'].str.lower() == name.strip().lower(), 'Current Occupation'] = current_occupation
|
91 |
+
if pass_out_year:
|
92 |
+
df.loc[df['Name'].str.lower() == name.strip().lower(), 'Pass Out Year'] = pass_out_year
|
93 |
message = "Phone number, occupation, and pass out year updated successfully."
|
94 |
else:
|
95 |
+
# Add a new entry if the name or phone number was not found
|
96 |
new_entry = {
|
97 |
'Name': name,
|
98 |
'Contact Number (Whatsapp)': phone_number,
|
|
|
119 |
if df is None:
|
120 |
return "Error loading the file."
|
121 |
|
122 |
+
# Get details for the provided name or phone number
|
123 |
+
details = get_details(name, phone_number, df)
|
124 |
details_html = format_output(details)
|
125 |
|
126 |
# If a phone number, current occupation, or pass out year is provided, update or add the details
|
|
|
137 |
iface = gr.Interface(
|
138 |
fn=predict_and_update,
|
139 |
inputs=[
|
140 |
+
gr.Textbox(lines=1, label="**Enter Name** (Optional)"),
|
141 |
+
gr.Textbox(lines=1, label="**Phone Number** (Optional)"),
|
142 |
gr.Textbox(lines=1, label="**Current Occupation** (Optional - to add/update)"),
|
143 |
gr.Textbox(lines=1, label="**Pass Out Year** (Optional - to add/update)")
|
144 |
],
|
145 |
outputs="html",
|
146 |
+
title="Name and Phone Number Lookup and Update",
|
147 |
+
description="Enter a name or phone number to find details associated with it from the 'cleaned_data.xlsx' file. Optionally, add or update the phone number, current occupation, and pass out year."
|
148 |
)
|
149 |
|
150 |
# Run the Gradio interface
|
151 |
if __name__ == "__main__":
|
152 |
iface.launch()
|
153 |
+
|
154 |
+
|
155 |
+
|