Spaces:
Runtime error
Runtime error
Commit
•
e5e07ff
1
Parent(s):
9663a1e
Add application file
Browse files- app.py +49 -0
- config.json +25 -0
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from transformers import TFAutoModelForSequenceClassification, DistilBertTokenizer
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
# Define the repository name and model ID
|
8 |
+
repository_name = "MariamKili/my_bert_model"
|
9 |
+
model_id = "tf_model"
|
10 |
+
|
11 |
+
# Load the tokenizer
|
12 |
+
tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")
|
13 |
+
|
14 |
+
# Load the model directly from Hugging Face Hub
|
15 |
+
model = TFAutoModelForSequenceClassification.from_pretrained(repository_name)
|
16 |
+
|
17 |
+
# Your prediction function would remain the same
|
18 |
+
def predict_sentiment(text):
|
19 |
+
# Tokenize and encode the input text
|
20 |
+
encoded_input = tokenizer.encode_plus(
|
21 |
+
text,
|
22 |
+
add_special_tokens=True,
|
23 |
+
max_length=512,
|
24 |
+
padding="max_length",
|
25 |
+
return_attention_mask=True,
|
26 |
+
truncation=True,
|
27 |
+
return_tensors="tf"
|
28 |
+
)
|
29 |
+
|
30 |
+
# Make predictions
|
31 |
+
output = model(encoded_input)
|
32 |
+
probabilities = tf.nn.softmax(output.logits, axis=1).numpy()[0]
|
33 |
+
predicted_label = np.argmax(probabilities)
|
34 |
+
confidence_score = probabilities[predicted_label]
|
35 |
+
|
36 |
+
# Decode the predicted label
|
37 |
+
label = "positive" if predicted_label == 1 else "negative"
|
38 |
+
|
39 |
+
return label, confidence_score
|
40 |
+
|
41 |
+
|
42 |
+
# Create the Gradio interface
|
43 |
+
text_input = gr.components.Textbox(lines=5, label="Enter your text here")
|
44 |
+
output_text = gr.components.Textbox(label="Predicted Sentiment")
|
45 |
+
|
46 |
+
# Define the Gradio interface
|
47 |
+
iface=gr.Interface(fn=predict_sentiment, inputs=text_input, outputs=output_text, title="Sentiment Analysis Application System")
|
48 |
+
# Launch the Gradio app
|
49 |
+
iface.launch(share=True)
|
config.json
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "bert-base-uncased",
|
3 |
+
"architectures": [
|
4 |
+
"BertForSequenceClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"classifier_dropout": null,
|
8 |
+
"gradient_checkpointing": false,
|
9 |
+
"hidden_act": "gelu",
|
10 |
+
"hidden_dropout_prob": 0.1,
|
11 |
+
"hidden_size": 768,
|
12 |
+
"initializer_range": 0.02,
|
13 |
+
"intermediate_size": 3072,
|
14 |
+
"layer_norm_eps": 1e-12,
|
15 |
+
"max_position_embeddings": 512,
|
16 |
+
"model_type": "bert",
|
17 |
+
"num_attention_heads": 12,
|
18 |
+
"num_hidden_layers": 12,
|
19 |
+
"pad_token_id": 0,
|
20 |
+
"position_embedding_type": "absolute",
|
21 |
+
"transformers_version": "4.40.0",
|
22 |
+
"type_vocab_size": 2,
|
23 |
+
"use_cache": true,
|
24 |
+
"vocab_size": 30522
|
25 |
+
}
|