Spaces:
Runtime error
Runtime error
File size: 1,814 Bytes
3f49524 1158018 072f63a 1158018 072f63a 610bb32 1158018 072f63a 3f49524 1158018 3f49524 072f63a 3f49524 610bb32 3f49524 |
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 |
import gradio as gr
import pandas as pd
from transformers import BertTokenizer, BertForSequenceClassification
import torch
# Load pre-trained TinyBERT model and tokenizer
tokenizer = BertTokenizer.from_pretrained('huawei-noah/TinyBERT_General_4L_312D')
model = BertForSequenceClassification.from_pretrained('huawei-noah/TinyBERT_General_4L_312D')
# Function to process the CSV file and generate predictions
def process_csv(file):
try:
# Read the CSV file
df = pd.read_csv(file.name) # Use file.name to get the file path
# Ensure the CSV has a 'text' column
if 'text' not in df.columns:
return "Error: The CSV file must contain a 'text' column."
# Tokenize the input text
inputs = tokenizer(df['text'].tolist(), return_tensors='pt', padding=True, truncation=True)
# Perform inference
with torch.no_grad():
outputs = model(**inputs)
# Get predicted classes
_, predicted_classes = torch.max(outputs.logits, dim=1)
# Add predicted classes to the DataFrame
df['predicted_class'] = predicted_classes.numpy()
# Return the processed DataFrame as a CSV string
output_csv = df.to_csv(index=False)
return output_csv
except Exception as e:
return f"Error: {str(e)}"
# Create the Gradio interface
input_csv = gr.File(label="Upload CSV File")
output_csv = gr.File(label="Download Processed CSV")
demo = gr.Interface(
fn=process_csv,
inputs=input_csv,
outputs=output_csv,
title="CSV Data Processing with TinyBERT",
description="Upload a CSV file with a 'text' column, and the model will process the data and provide predictions."
)
# Launch the Gradio interface
demo.launch() |