SharmaAmit1818 commited on
Commit
6f062c5
·
verified ·
1 Parent(s): 072f63a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -10,14 +10,14 @@ model = BertForSequenceClassification.from_pretrained('huawei-noah/TinyBERT_Gene
10
  # Function to process the CSV file and generate predictions
11
  def process_csv(file):
12
  try:
13
- # Read the CSV file
14
- df = pd.read_csv(file.name) # Use file.name to get the file path
15
 
16
- # Ensure the CSV has a 'text' column
17
  if 'text' not in df.columns:
18
  return "Error: The CSV file must contain a 'text' column."
19
 
20
- # Tokenize the input text
21
  inputs = tokenizer(df['text'].tolist(), return_tensors='pt', padding=True, truncation=True)
22
 
23
  # Perform inference
@@ -27,18 +27,22 @@ def process_csv(file):
27
  # Get predicted classes
28
  _, predicted_classes = torch.max(outputs.logits, dim=1)
29
 
30
- # Add predicted classes to the DataFrame
31
  df['predicted_class'] = predicted_classes.numpy()
32
 
33
- # Return the processed DataFrame as a CSV string
34
- output_csv = df.to_csv(index=False)
35
-
36
- return output_csv
37
 
 
 
 
 
 
 
38
  except Exception as e:
39
- return f"Error: {str(e)}"
40
 
41
- # Create the Gradio interface
42
  input_csv = gr.File(label="Upload CSV File")
43
  output_csv = gr.File(label="Download Processed CSV")
44
 
@@ -50,5 +54,5 @@ demo = gr.Interface(
50
  description="Upload a CSV file with a 'text' column, and the model will process the data and provide predictions."
51
  )
52
 
53
- # Launch the Gradio interface
54
  demo.launch()
 
10
  # Function to process the CSV file and generate predictions
11
  def process_csv(file):
12
  try:
13
+ # Read the CSV file using Pandas
14
+ df = pd.read_csv(file.name) # Ensure correct file path
15
 
16
+ # Check for 'text' column
17
  if 'text' not in df.columns:
18
  return "Error: The CSV file must contain a 'text' column."
19
 
20
+ # Tokenize input text
21
  inputs = tokenizer(df['text'].tolist(), return_tensors='pt', padding=True, truncation=True)
22
 
23
  # Perform inference
 
27
  # Get predicted classes
28
  _, predicted_classes = torch.max(outputs.logits, dim=1)
29
 
30
+ # Add predictions to DataFrame
31
  df['predicted_class'] = predicted_classes.numpy()
32
 
33
+ # Return processed DataFrame as CSV string
34
+ return df.to_csv(index=False)
 
 
35
 
36
+ except FileNotFoundError:
37
+ return "Error: The specified file was not found. Please check your upload."
38
+ except pd.errors.EmptyDataError:
39
+ return "Error: The uploaded file is empty."
40
+ except pd.errors.ParserError:
41
+ return "Error: There was an issue parsing the CSV file."
42
  except Exception as e:
43
+ return f"An unexpected error occurred: {str(e)}"
44
 
45
+ # Create Gradio interface
46
  input_csv = gr.File(label="Upload CSV File")
47
  output_csv = gr.File(label="Download Processed CSV")
48
 
 
54
  description="Upload a CSV file with a 'text' column, and the model will process the data and provide predictions."
55
  )
56
 
57
+ # Launch Gradio interface
58
  demo.launch()