austinmyc commited on
Commit
00b07c5
·
verified ·
1 Parent(s): 3b085b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py CHANGED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import pickle
4
+ from fuzzywuzzy import process
5
+
6
+ # Load your data
7
+ def load_lawyers(filename):
8
+ return pd.read_csv(filename)
9
+
10
+ def load_history(filename):
11
+ with open(filename, "rb") as f:
12
+ return pickle.load(f)
13
+
14
+ lawyers_data = load_lawyers('data/lawyers.csv')
15
+ history_data = load_history('data/history.pkl')
16
+
17
+ # Search function
18
+ def search_solicitor(name):
19
+ if not name or not isinstance(name, str):
20
+ return "Invalid input"
21
+
22
+ names_list = lawyers_data['name'].tolist()
23
+ matches = process.extract(name, names_list, limit=None)
24
+ threshold = 70
25
+ matched_names = [(names_list.index(match[0])+1, match[0]) for match in matches if match[1] >= threshold]
26
+
27
+ if matched_names:
28
+ return pd.DataFrame(matched_names, columns=['idx', 'name'])
29
+ else:
30
+ return "No solicitors found."
31
+
32
+ # Display history function
33
+ def display_history(selected_index):
34
+ if selected_index is None or not (0 < selected_index <= len(lawyers_data)):
35
+ return "Invalid selection."
36
+
37
+ solicitor = lawyers_data.iloc[selected_index - 1]
38
+ name = solicitor['name']
39
+ working_history_df = history_data[name][0] # Get the DataFrame for the solicitor
40
+
41
+ if working_history_df.columns.dtype == 'int64':
42
+ first_row_values = working_history_df.iloc[0]
43
+ working_history_df.columns = first_row_values
44
+ working_history_df = working_history_df.drop(index=0)
45
+
46
+ # Clean the DataFrame: drop unwanted columns and replace NaN values
47
+ working_history_df = working_history_df.drop(columns=[col for col in working_history_df.columns if 'Unnamed' in str(col)], errors='ignore')
48
+ working_history_df = working_history_df.fillna('') # Replace NaN with empty strings
49
+ nan_columns = [col for col in working_history_df.columns if pd.isna(col)] # Identify columns with NaN as their name
50
+ working_history_df = working_history_df.drop(columns=nan_columns, errors='ignore')
51
+
52
+ return working_history_df
53
+
54
+ # Gradio interface
55
+ with gr.Blocks() as iface:
56
+ gr.Markdown("# Solicitor Search and History Display")
57
+
58
+ with gr.Row():
59
+ name_input = gr.Textbox(label="Enter Solicitor's Name")
60
+ search_button = gr.Button("Search Solicitor")
61
+
62
+ search_results = gr.Dataframe(label="Search Results")
63
+
64
+ with gr.Row():
65
+ index_input = gr.Number(label="Select Solicitor by Number", precision=0)
66
+ history_button = gr.Button("Display Working History")
67
+
68
+ history_results = gr.Dataframe(label="Working History")
69
+
70
+ # Define button actions
71
+ search_button.click(search_solicitor, inputs=name_input, outputs=search_results)
72
+ history_button.click(display_history, inputs=index_input, outputs=history_results)
73
+
74
+ if __name__ == "__main__":
75
+ iface.launch()