Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
print("selam")
|
| 5 |
+
|
| 6 |
+
st.title("Sentiment Analysis with BERT")
|
| 7 |
+
|
| 8 |
+
user_input = st.text_area("Enter your text here:")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
if st.button("Analyze"):
|
| 13 |
+
import streamlit as st
|
| 14 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 15 |
+
import torch
|
| 16 |
+
|
| 17 |
+
tokenizer = BertTokenizer.from_pretrained("ulascelenk/siena")
|
| 18 |
+
model = BertForSequenceClassification.from_pretrained("ulascelenk/siena")
|
| 19 |
+
|
| 20 |
+
inputs = tokenizer.encode_plus(
|
| 21 |
+
user_input,
|
| 22 |
+
return_tensors='pt', # PyTorch tensörü olarak döndür
|
| 23 |
+
truncation=True, # Metni maksimum uzunluğa göre kes
|
| 24 |
+
max_length=128, # Maksimum token uzunluğunu belirleyin
|
| 25 |
+
padding='max_length' # Token uzunluğunu maksimuma göre doldur
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
input_ids = inputs['input_ids']
|
| 30 |
+
attention_mask = inputs['attention_mask']
|
| 31 |
+
outputs = model(input_ids, attention_mask=attention_mask)
|
| 32 |
+
logits = outputs.logits
|
| 33 |
+
|
| 34 |
+
# Tahmin edilen sınıfı alın
|
| 35 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
| 36 |
+
|
| 37 |
+
sentiment_dict = {
|
| 38 |
+
0: "Mild Negative",
|
| 39 |
+
1: "Mild Positive",
|
| 40 |
+
2: "Neutral",
|
| 41 |
+
3: "Strong Negative",
|
| 42 |
+
4: "Strong Positive"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Tahmini kullanıcıya gösterin
|
| 46 |
+
st.write(f"Predicted class: {sentiment_dict[predicted_class]}")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|