Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
last_hidden_state = outputs.last_hidden_state[:, 0, :]
|
| 15 |
-
# Return a response based on the output
|
| 16 |
-
return {"response": last_hidden_state.detach().numpy().tolist()}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
+
def mask(text):
|
| 5 |
+
mask_model = pipeline("fill-mask", model="google-bert/bert-base-uncased")
|
| 6 |
+
output = mask_model(text)
|
| 7 |
+
return output[0]['sequence']
|
| 8 |
|
| 9 |
+
with gr.Blocks() as demo:
|
| 10 |
+
gr.Markdown("BERT Test - Zircon.tech")
|
| 11 |
+
with gr.Column(scale=1):
|
| 12 |
+
textbox_mask = gr.Textbox(label="Give me a sentance and use the word [MASK] to predict what will go there")
|
| 13 |
+
mask_button = gr.Button("Predict")
|
| 14 |
+
textbox_mask_output = gr.Textbox()
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
mask_button.click(fn=mask,
|
| 17 |
+
inputs = textbox_mask,
|
| 18 |
+
outputs = textbox_mask_output)
|
| 19 |
+
demo.launch(debug=True)
|
| 20 |
+
|
| 21 |
+
# # Load the model and tokenizer
|
| 22 |
+
# tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
|
| 23 |
+
# model = BertModel.from_pretrained("google-bert/bert-base-uncased")
|
| 24 |
|
| 25 |
+
# def predict(input_text):
|
| 26 |
+
# # Preprocess the input text
|
| 27 |
+
# inputs = tokenizer(input_text, return_tensors="pt")
|
| 28 |
+
# # Get the model's output
|
| 29 |
+
# outputs = model(**inputs)
|
| 30 |
+
# # Extract the last hidden state
|
| 31 |
+
# last_hidden_state = outputs.last_hidden_state[:, 0, :]
|
| 32 |
+
# # Return a response based on the output
|
| 33 |
+
# return {"response": last_hidden_state.detach().numpy().tolist()}
|
| 34 |
+
|
| 35 |
+
# # Create a Gradio interface
|
| 36 |
+
# iface = gr.Interface(
|
| 37 |
+
# fn=predict,
|
| 38 |
+
# inputs="text",
|
| 39 |
+
# outputs="text",
|
| 40 |
+
# title="BERT-based Text Analyzer",
|
| 41 |
+
# description="Enter some text to analyze with BERT!"
|
| 42 |
+
# )
|
| 43 |
+
|
| 44 |
+
# # Launch the interface
|
| 45 |
+
# iface.launch()
|