MariamKili commited on
Commit
fb153b1
1 Parent(s): 0335713

Add application file

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +42 -0
  3. tf_model.h5 +3 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ sent.env
2
+ sent
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from transformers import DistilBertTokenizer
4
+ import numpy as np
5
+
6
+ # Load the saved model and tokenizer
7
+ model = tf.keras.models.load_model('tf_model.h5')
8
+ tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
9
+
10
+ import gradio as gr
11
+ # Function to make predictions on new text inputs
12
+ def predict_sentiment(text):
13
+ # Tokenize and encode the input text
14
+ encoded_input = tokenizer.encode_plus(
15
+ text,
16
+ add_special_tokens=True,
17
+ max_length=512,
18
+ pad_to_max_length=True,
19
+ return_attention_mask=True,
20
+ truncation=True,
21
+ return_tensors="tf"
22
+ )
23
+
24
+ # Make predictions
25
+ output = model(encoded_input)
26
+ probabilities = tf.nn.softmax(output.logits, axis=1).numpy()[0]
27
+ predicted_label = np.argmax(probabilities)
28
+ confidence_score = probabilities[predicted_label]
29
+
30
+ # Decode the predicted label
31
+ label = "positive" if predicted_label == 1 else "negative"
32
+
33
+ return label, confidence_score
34
+
35
+ # Create the Gradio interface
36
+ text_input = gr.components.Textbox(lines=5, label="Enter your text here")
37
+ output_text = gr.components.Textbox(label="Predicted Sentiment")
38
+
39
+ # Define the Gradio interface
40
+ iface=gr.Interface(fn=predict_sentiment, inputs=text_input, outputs=output_text, title="Sentiment Analysis")
41
+ # Launch the Gradio app
42
+ iface.launch(inline=False)
tf_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6f09109d7f8c02a5480a0810c178771b8fabc5e10b067a6fdd6cfc11c74b9655
3
+ size 267955144