Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load pre-trained TinyBERT model and tokenizer
|
| 7 |
+
tokenizer = BertTokenizer.from_pretrained('huawei-noah/TinyBERT_General_4L_312D')
|
| 8 |
+
model = BertForSequenceClassification.from_pretrained('huawei-noah/TinyBERT_General_4L_312D')
|
| 9 |
+
|
| 10 |
+
# Function to process the CSV file and generate predictions
|
| 11 |
+
def process_csv(file):
|
| 12 |
+
# Read the CSV file
|
| 13 |
+
df = pd.read_csv(file)
|
| 14 |
+
|
| 15 |
+
# Ensure the CSV has a 'text' column
|
| 16 |
+
if 'text' not in df.columns:
|
| 17 |
+
return "Error: The CSV file must contain a 'text' column."
|
| 18 |
+
|
| 19 |
+
# Tokenize the input text
|
| 20 |
+
inputs = tokenizer(df['text'].tolist(), return_tensors='pt', padding=True, truncation=True)
|
| 21 |
+
|
| 22 |
+
# Perform inference
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
|
| 26 |
+
# Get predicted classes
|
| 27 |
+
_, predicted_classes = torch.max(outputs.logits, dim=1)
|
| 28 |
+
df['predicted_class'] = predicted_classes.numpy()
|
| 29 |
+
|
| 30 |
+
# Return the processed DataFrame as a CSV string
|
| 31 |
+
return df.to_csv(index=False)
|
| 32 |
+
|
| 33 |
+
# Create the Gradio interface
|
| 34 |
+
input_csv = gr.File(label="Upload CSV File")
|
| 35 |
+
output_csv = gr.File(label="Download Processed CSV")
|
| 36 |
+
|
| 37 |
+
demo = gr.Interface(
|
| 38 |
+
fn=process_csv,
|
| 39 |
+
inputs=input_csv,
|
| 40 |
+
outputs=output_csv,
|
| 41 |
+
title="CSV Data Processing with TinyBERT",
|
| 42 |
+
description="Upload a CSV file with a 'text' column, and the model will process the data and provide predictions."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Launch the Gradio interface
|
| 46 |
+
demo.launch()
|