Sengil commited on
Commit
e1e7ab1
·
verified ·
1 Parent(s): a8bc5b2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +49 -20
README.md CHANGED
@@ -45,34 +45,63 @@ The model achieved the following performance on the test set:
45
 
46
  ## 🚀 Usage Example
47
 
 
48
  ```python
 
49
  import pickle
50
- import numpy as np
51
  from tensorflow.keras.models import load_model
52
- from tensorflow.keras.preprocessing.sequence import pad_sequences
53
 
54
- # Load the model and tokenizer
55
- model = load_model("absa_bilstm_model.keras")
56
- with open("tokenizer.pkl", "rb") as f:
 
 
 
 
 
57
  tokenizer = pickle.load(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- # Maximum sentence length used during training
60
- max_len = 84 # Adjust this value based on your training configuration
61
-
62
- # Prediction function
63
- def predict_sentiment(sentence, aspect):
64
- input_text = f"{sentence} [ASP] {aspect}"
65
- sequence = tokenizer.texts_to_sequences([input_text])
66
- padded_sequence = pad_sequences(sequence, maxlen=max_len, padding='post')
67
- prediction = model.predict(padded_sequence)
68
- label = np.argmax(prediction, axis=1)[0]
69
- labels = {0: "Negative", 1: "Neutral", 2: "Positive"}
 
 
 
70
  return labels[label]
 
 
 
 
 
 
71
 
72
- # Example usage
73
- sentence = "Manzara şahane evet ama servis rezalet."
74
- aspect = "Servis"
75
- print(f"Sentiment for '{aspect}': {predict_sentiment(sentence, aspect)}")
76
  ````
77
 
78
  ## 🏋️‍♀️ Training Details
 
45
 
46
  ## 🚀 Usage Example
47
 
48
+ Download model from HF
49
  ```python
50
+ from huggingface_hub import hf_hub_download
51
  import pickle
 
52
  from tensorflow.keras.models import load_model
 
53
 
54
+ model_path = hf_hub_download(repo_id="Sengil/Turkish-ABSA-BiLSTM-Word2Vec", filename="absa_bilstm_model.keras")
55
+ tokenizer_path = hf_hub_download(repo_id="Sengil/Turkish-ABSA-BiLSTM-Word2Vec", filename="tokenizer.pkl")
56
+
57
+ # load model
58
+ model = load_model(model_path)
59
+
60
+ # load tokenizer
61
+ with open(tokenizer_path, "rb") as f:
62
  tokenizer = pickle.load(f)
63
+ ````
64
+
65
+ Input preprocessing
66
+ ```python
67
+ import re
68
+ import nltk
69
+ nltk.download('punkt')
70
+
71
+ def preprocess_turkish(text):
72
+ text = text.lower()
73
+ text = re.sub(r"http\S+|www\S+|https\S+", "<url>", text)
74
+ text = re.sub(r"@\w+", "<user>", text)
75
+ text = re.sub(r"[^a-zA-Z0-9çğıöşüÇĞİÖŞÜ\s]", " ", text)
76
+ text = re.sub(r"(.)\1{2,}", r"\1\1", text)
77
+ text = re.sub(r"\s+", " ", text).strip()
78
+ return text
79
+ ````
80
 
81
+ Predict the input
82
+ ```python
83
+ import numpy as np
84
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
85
+
86
+ def predict_sentiment(sentence, aspect, max_len=84):
87
+ input_text = sentence + " [ASP] " + aspect
88
+ cleaned = preprocess_turkish(input_text)
89
+ tokenized = tokenizer.texts_to_sequences([cleaned])
90
+ padded = pad_sequences(tokenized, maxlen=max_len, padding='post')
91
+
92
+ pred = model.predict(padded)
93
+ label = np.argmax(pred)
94
+ labels = {0: "Negatif", 1: "Nötr", 2: "Pozitif"}
95
  return labels[label]
96
+ ````
97
+
98
+ run
99
+ ```python
100
+ sentence = "Manzara sahane evet ama servis rezalet."
101
+ aspect = "manzara"
102
 
103
+ predict = predict_sentiment(sentence, aspect)
104
+ print("predict:", predict)
 
 
105
  ````
106
 
107
  ## 🏋️‍♀️ Training Details