Tarbelian / app.py
atifsial123's picture
Create app.py
e3a80d7 verified
raw
history blame
2.22 kB
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()