Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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) #
|
15 |
|
16 |
-
#
|
17 |
if 'text' not in df.columns:
|
18 |
return "Error: The CSV file must contain a 'text' column."
|
19 |
|
20 |
-
# Tokenize
|
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
|
31 |
df['predicted_class'] = predicted_classes.numpy()
|
32 |
|
33 |
-
# Return
|
34 |
-
|
35 |
-
|
36 |
-
return output_csv
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
except Exception as e:
|
39 |
-
return f"
|
40 |
|
41 |
-
# Create
|
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
|
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()
|