Malek-AI commited on
Commit
cb7384e
·
verified ·
1 Parent(s): 291909d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -23
app.py CHANGED
@@ -1,28 +1,45 @@
1
  import gradio as gr
2
- from transformers import BertTokenizer, BertModel
3
 
4
- # Load the model and tokenizer
5
- tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
6
- model = BertModel.from_pretrained("google-bert/bert-base-uncased")
 
7
 
8
- def predict(input_text):
9
- # Preprocess the input text
10
- inputs = tokenizer(input_text, return_tensors="pt")
11
- # Get the model's output
12
- outputs = model(**inputs)
13
- # Extract the last hidden state
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
- # Create a Gradio interface
19
- iface = gr.Interface(
20
- fn=predict,
21
- inputs="text",
22
- outputs="text",
23
- title="BERT-based Text Analyzer",
24
- description="Enter some text to analyze with BERT!"
25
- )
26
 
27
- # Launch the interface
28
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()