File size: 2,698 Bytes
823ded0
 
 
 
 
 
 
 
 
 
 
 
 
 
03a4c5e
a78f83f
f13a3ca
 
a78f83f
f13a3ca
9e57aa8
a78f83f
 
 
 
 
 
 
 
9e57aa8
bf33cf7
e403126
68b29f4
 
 
 
a78f83f
 
 
 
 
 
 
e403126
03a4c5e
e403126
a78f83f
 
 
 
 
 
 
 
 
 
 
9e57aa8
80e0122
e403126
 
a78f83f
 
 
e403126
 
 
 
 
 
 
 
 
 
 
a78f83f
 
 
 
68b29f4
a78f83f
 
 
 
68b29f4
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Install necessary libraries
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("scikit-learn")
install("gradio")

import os
import pandas as pd
import gradio as gr
from transformers import AutoModel, AutoTokenizer

# Load the pre-trained model and tokenizer
def load_model_and_tokenizer():
    try:
        model = AutoModel.from_pretrained("Alibaba-NLP/gte-multilingual-base", trust_remote_code=True)
        tokenizer = AutoTokenizer.from_pretrained("Alibaba-NLP/gte-multilingual-base", trust_remote_code=True)
        return model, tokenizer
    except Exception as e:
        print(f"Error loading model or tokenizer: {e}")
        return None, None

# Function to load the dataset
def load_dataset():
    file_path = "Valid-part-2.xlsx"
    if not os.path.exists(file_path):
        raise FileNotFoundError(f"Dataset not found. Please ensure that '{file_path}' exists.")
    
    try:
        df = pd.read_excel(file_path)
        print("Columns in the dataset:", df.columns.tolist())
        return df
    except Exception as e:
        print(f"Error loading dataset: {e}")
        return None

# Function to search by name and return the PEC number
def search_by_name(name, df):
    if df is None:
        return "Error: Dataset not loaded."
    
    try:
        name_matches = df[df['name'].str.contains(name, case=False, na=False)]
        if not name_matches.empty:
            return f"Your PEC number: {name_matches['PEC number'].values[0]}"
        else:
            return "No matches found for your name."
    except Exception as e:
        return f"Error during search: {e}"

# Gradio interface
def build_interface():
    df = load_dataset()  # Load your dataset
    if df is None:
        return None
    
    iface = gr.Interface(
        fn=lambda name: search_by_name(name, df),
        inputs=gr.Textbox(label="Please write your Name"),
        outputs=gr.Textbox(label="Your PEC number"),
        title="PEC Number Lookup",
        description="Enter your name to find your PEC number."
    )
    return iface

# Main function to run the Gradio app
if __name__ == "__main__":
    model, tokenizer = load_model_and_tokenizer()
    if model is None or tokenizer is None:
        print("Failed to load model or tokenizer. Exiting.")
    else:
        iface = build_interface()
        if iface is not None:
            iface.launch()
        else:
            print("Failed to build interface due to dataset issues.")