Engineer / app.py
atifsial123's picture
Update app.py
562e8c4 verified
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("transformers")
install("torch")
install("pandas")
install("gradio")
install("openpyxl") # Added installation for openpyxl
import pandas as pd
import gradio as gr
from transformers import AutoModel, AutoTokenizer
import torch
# Load the model and tokenizer from Hugging Face
tokenizer = AutoTokenizer.from_pretrained("Alibaba-NLP/gte-multilingual-base", trust_remote_code=True)
model = AutoModel.from_pretrained("Alibaba-NLP/gte-multilingual-base", trust_remote_code=True)
# Load the dataset containing PEC numbers and names
def load_dataset(file_path='PEC_Numbers_and_Names.xlsx'):
if os.path.exists(file_path):
df = pd.read_excel(file_path)
print("File loaded successfully.")
print(df.head()) # Print first few rows for debugging
else:
raise FileNotFoundError(f"File not found: {file_path}")
return df
# Function to get the name based on the PEC number
def get_name(pec_number, df):
df['PEC No.'] = df['PEC No.'].str.strip().str.upper()
pec_number = pec_number.strip().upper()
print(f"Searching for PEC Number: {pec_number}") # Debugging output
result = df[df['PEC No.'] == pec_number]
if not result.empty:
print(f"Found Name: {result.iloc[0]['Name']}") # Debugging output
return result.iloc[0]['Name']
else:
print("PEC Number not is found.") # Debugging output
return "PEC Number not is found."
# Function to check if the PEC number is attached
def check_pec_number(pec_number, df):
df['PEC No.'] = df['PEC No.'].str.strip().str.upper()
pec_number = pec_number.strip().upper()
if pec_number in df['PEC No.'].values:
return "Your PEC Number is NOT Attached."
else:
return "Your PEC Number is Not Attached."
# Combine the functions to create a prediction
def predict(pec_number):
try:
# Load the dataset from the root directory
df = load_dataset()
name = get_name(pec_number, df)
pec_status = check_pec_number(pec_number, df)
return f"Your Name Is: {name}\n{pec_status}" # Return name and PEC status
except Exception as e:
print(f"An error occurred: {e}")
return f"Error: {e}"
# Build the Gradio interface without the file upload option
iface = gr.Interface(
fn=predict,
inputs=gr.Textbox(lines=1, label="**PEC Number**"), # Bold label for PEC Number
outputs=gr.Textbox(label="Your Name Is:"), # Custom label for the output
title="PEC Number to Name Lookup",
description="Enter a PEC number , Your PEC number is attached with Firm or not"
)
# Run the Gradio interface
if __name__ == "__main__":
iface.launch()