atifsial123 commited on
Commit
a78f83f
·
verified ·
1 Parent(s): 68b29f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -13,41 +13,55 @@ install("pandas")
13
  install("scikit-learn")
14
  install("gradio")
15
 
16
- # Import the necessary libraries
17
- from transformers import AutoModel, AutoTokenizer
18
- import torch
19
- from torch.utils.data import DataLoader, Dataset
20
- from sklearn.model_selection import train_test_split
21
  import pandas as pd
22
  import gradio as gr
23
- import os
24
 
25
  # Load the pre-trained model and tokenizer
26
- model = AutoModel.from_pretrained("Alibaba-NLP/gte-multilingual-base", trust_remote_code=True)
27
- tokenizer = AutoTokenizer.from_pretrained("Alibaba-NLP/gte-multilingual-base", trust_remote_code=True)
 
 
 
 
 
 
28
 
29
  # Function to load the dataset
30
  def load_dataset():
31
- # Use the uploaded file path
32
  file_path = "Valid-part-2.xlsx"
33
  if not os.path.exists(file_path):
34
  raise FileNotFoundError(f"Dataset not found. Please ensure that '{file_path}' exists.")
35
 
36
- df = pd.read_excel(file_path) # Load the Excel file
37
- print("Columns in the dataset:", df.columns.tolist())
38
- return df
 
 
 
 
39
 
40
  # Function to search by name and return the PEC number
41
  def search_by_name(name, df):
42
- name_matches = df[df['name'].str.contains(name, case=False, na=False)]
43
- if not name_matches.empty:
44
- return f"Your PEC number: {name_matches['PEC number'].values[0]}"
45
- else:
46
- return "No matches found for your name."
 
 
 
 
 
 
47
 
48
  # Gradio interface
49
  def build_interface():
50
  df = load_dataset() # Load your dataset
 
 
 
51
  iface = gr.Interface(
52
  fn=lambda name: search_by_name(name, df),
53
  inputs=gr.Textbox(label="Please write your Name"),
@@ -59,9 +73,13 @@ def build_interface():
59
 
60
  # Main function to run the Gradio app
61
  if __name__ == "__main__":
62
- try:
 
 
 
63
  iface = build_interface()
64
- iface.launch()
65
- except FileNotFoundError as e:
66
- print(str(e))
 
67
 
 
13
  install("scikit-learn")
14
  install("gradio")
15
 
16
+ import os
 
 
 
 
17
  import pandas as pd
18
  import gradio as gr
19
+ from transformers import AutoModel, AutoTokenizer
20
 
21
  # Load the pre-trained model and tokenizer
22
+ def load_model_and_tokenizer():
23
+ try:
24
+ model = AutoModel.from_pretrained("Alibaba-NLP/gte-multilingual-base", trust_remote_code=True)
25
+ tokenizer = AutoTokenizer.from_pretrained("Alibaba-NLP/gte-multilingual-base", trust_remote_code=True)
26
+ return model, tokenizer
27
+ except Exception as e:
28
+ print(f"Error loading model or tokenizer: {e}")
29
+ return None, None
30
 
31
  # Function to load the dataset
32
  def load_dataset():
 
33
  file_path = "Valid-part-2.xlsx"
34
  if not os.path.exists(file_path):
35
  raise FileNotFoundError(f"Dataset not found. Please ensure that '{file_path}' exists.")
36
 
37
+ try:
38
+ df = pd.read_excel(file_path)
39
+ print("Columns in the dataset:", df.columns.tolist())
40
+ return df
41
+ except Exception as e:
42
+ print(f"Error loading dataset: {e}")
43
+ return None
44
 
45
  # Function to search by name and return the PEC number
46
  def search_by_name(name, df):
47
+ if df is None:
48
+ return "Error: Dataset not loaded."
49
+
50
+ try:
51
+ name_matches = df[df['name'].str.contains(name, case=False, na=False)]
52
+ if not name_matches.empty:
53
+ return f"Your PEC number: {name_matches['PEC number'].values[0]}"
54
+ else:
55
+ return "No matches found for your name."
56
+ except Exception as e:
57
+ return f"Error during search: {e}"
58
 
59
  # Gradio interface
60
  def build_interface():
61
  df = load_dataset() # Load your dataset
62
+ if df is None:
63
+ return None
64
+
65
  iface = gr.Interface(
66
  fn=lambda name: search_by_name(name, df),
67
  inputs=gr.Textbox(label="Please write your Name"),
 
73
 
74
  # Main function to run the Gradio app
75
  if __name__ == "__main__":
76
+ model, tokenizer = load_model_and_tokenizer()
77
+ if model is None or tokenizer is None:
78
+ print("Failed to load model or tokenizer. Exiting.")
79
+ else:
80
  iface = build_interface()
81
+ if iface is not None:
82
+ iface.launch()
83
+ else:
84
+ print("Failed to build interface due to dataset issues.")
85