Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
5 |
+
import pickle
|
6 |
+
import re
|
7 |
+
|
8 |
+
# Load model and tokenizer
|
9 |
+
model = load_model("best_GRU_tuning_model.h5")
|
10 |
+
with open("my_tokenizer.pkl","rb") as f:
|
11 |
+
tokenizer = pickle.load(f)
|
12 |
+
|
13 |
+
|
14 |
+
def preprocess_text(text):
|
15 |
+
text = text.lower()
|
16 |
+
text = re.sub(r'[^a-zA-Z\s]', '', text).strip()
|
17 |
+
return text
|
18 |
+
|
19 |
+
|
20 |
+
def predict_sentiment(raw_text):
|
21 |
+
cleaned = preprocess_text(raw_text)
|
22 |
+
seq = tokenizer.texts_to_sequences([cleaned])
|
23 |
+
padded_seq = pad_sequences(seq, maxlen=200)
|
24 |
+
probs = model.predict(padded_seq)
|
25 |
+
predicted_class = np.argmax(probs, axis=1)[0]
|
26 |
+
rating = predicted_class + 1
|
27 |
+
return f"Predicted rating: {rating} (probabilities={probs[0]})"
|
28 |
+
|
29 |
+
|
30 |
+
demo = gr.Interface(fn=predict_sentiment,
|
31 |
+
inputs="text",
|
32 |
+
outputs="label")
|
33 |
+
demo.launch()
|