File size: 2,222 Bytes
e3a80d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import subprocess

# Function to install a package if it is not already installed
def install(package):
    subprocess.check_call([os.sys.executable, "-m", "pip", "install", package])

# Ensure the necessary packages are installed
install("pandas")
install("gradio")
install("openpyxl")

import pandas as pd
import gradio as gr

# Function to load the dataset from an uploaded file
def load_dataset(file_path):
    try:
        df = pd.read_excel(file_path)
        print("File loaded successfully.")
        print(df.head())  # Print first few rows for debugging
        return df
    except Exception as e:
        print(f"Error loading file: {e}")
        return None

# Function to get details based on the name
def get_details(name, df):
    # Clean the 'Name' column for consistent searching
    df['Name'] = df['Name'].astype(str).str.strip().str.lower()
    name = name.strip().lower()

    print(f"Searching for Name: {name}")  # Debugging output
    results = df[df['Name'].str.contains(name, case=False, na=False)]

    if not results.empty:
        print(f"Found Details: {results.to_dict('records')}")  # Debugging output
        return results.to_dict('records')
    else:
        print("Name not found.")  # Debugging output
        return "Name not found."

# Combine the functions to create a prediction
def predict(name, file):
    try:
        # Load the dataset from the uploaded file
        df = load_dataset(file.name)

        # Get details for the provided name
        if df is not None:
            details = get_details(name, df)
            return details  # Return details as JSON-like output
        else:
            return "Error loading the file."
    except Exception as e:
        print(f"An error occurred: {e}")
        return f"Error: {e}"

# Build the Gradio interface with file upload option
iface = gr.Interface(
    fn=predict,
    inputs=[gr.Textbox(lines=1, label="**Enter Name**"), gr.File(label="Upload cleaned_data.xlsx")],
    outputs="json",
    title="Name Details Lookup",
    description="Upload the 'cleaned_data.xlsx' file and enter a name to find all details associated with it."
)

# Run the Gradio interface
if __name__ == "__main__":
    iface.launch()