atifsial123 commited on
Commit
e3a80d7
·
verified ·
1 Parent(s): ae92da7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+
4
+ # Function to install a package if it is not already installed
5
+ def install(package):
6
+ subprocess.check_call([os.sys.executable, "-m", "pip", "install", package])
7
+
8
+ # Ensure the necessary packages are installed
9
+ install("pandas")
10
+ install("gradio")
11
+ install("openpyxl")
12
+
13
+ import pandas as pd
14
+ import gradio as gr
15
+
16
+ # Function to load the dataset from an uploaded file
17
+ def load_dataset(file_path):
18
+ try:
19
+ df = pd.read_excel(file_path)
20
+ print("File loaded successfully.")
21
+ print(df.head()) # Print first few rows for debugging
22
+ return df
23
+ except Exception as e:
24
+ print(f"Error loading file: {e}")
25
+ return None
26
+
27
+ # Function to get details based on the name
28
+ def get_details(name, df):
29
+ # Clean the 'Name' column for consistent searching
30
+ df['Name'] = df['Name'].astype(str).str.strip().str.lower()
31
+ name = name.strip().lower()
32
+
33
+ print(f"Searching for Name: {name}") # Debugging output
34
+ results = df[df['Name'].str.contains(name, case=False, na=False)]
35
+
36
+ if not results.empty:
37
+ print(f"Found Details: {results.to_dict('records')}") # Debugging output
38
+ return results.to_dict('records')
39
+ else:
40
+ print("Name not found.") # Debugging output
41
+ return "Name not found."
42
+
43
+ # Combine the functions to create a prediction
44
+ def predict(name, file):
45
+ try:
46
+ # Load the dataset from the uploaded file
47
+ df = load_dataset(file.name)
48
+
49
+ # Get details for the provided name
50
+ if df is not None:
51
+ details = get_details(name, df)
52
+ return details # Return details as JSON-like output
53
+ else:
54
+ return "Error loading the file."
55
+ except Exception as e:
56
+ print(f"An error occurred: {e}")
57
+ return f"Error: {e}"
58
+
59
+ # Build the Gradio interface with file upload option
60
+ iface = gr.Interface(
61
+ fn=predict,
62
+ inputs=[gr.Textbox(lines=1, label="**Enter Name**"), gr.File(label="Upload cleaned_data.xlsx")],
63
+ outputs="json",
64
+ title="Name Details Lookup",
65
+ description="Upload the 'cleaned_data.xlsx' file and enter a name to find all details associated with it."
66
+ )
67
+
68
+ # Run the Gradio interface
69
+ if __name__ == "__main__":
70
+ iface.launch()