Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,33 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import BertTokenizer, BertModel
|
| 4 |
|
| 5 |
+
tokenizer = BertTokenizer.from_pretrained('google/bert-base-uncased')
|
| 6 |
+
model = BertModel.from_pretrained('google/bert-base-uncased')
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def predict(input_text):
|
| 10 |
+
# Tokenize the input text
|
| 11 |
+
inputs = tokenizer.encode_plus(
|
| 12 |
+
input_text,
|
| 13 |
+
add_special_tokens=True,
|
| 14 |
+
max_length=512,
|
| 15 |
+
return_attention_mask=True,
|
| 16 |
+
return_tensors='pt'
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Run the input through the BERT model
|
| 20 |
+
outputs = model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
|
| 21 |
+
last_hidden_state = outputs.last_hidden_state[:, 0, :]
|
| 22 |
+
|
| 23 |
+
# Return the output (e.g., the pooled output)
|
| 24 |
+
return last_hidden_state.detach().numpy()
|
| 25 |
+
|
| 26 |
+
# Create the Gradio app
|
| 27 |
+
app = gr.Interface(
|
| 28 |
+
fn=predict,
|
| 29 |
+
inputs="text",
|
| 30 |
+
outputs="numpy",
|
| 31 |
+
title="BERT App",
|
| 32 |
+
description="Enter some text to see the BERT output"
|
| 33 |
+
)
|