Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import torch
|
|
| 3 |
from torch.nn.functional import softmax
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
|
| 6 |
-
# ---- MODEL AYARLARI ----
|
| 7 |
MODEL_ID = "celalkartoglu/tr-sentiment-bert-win-v1"
|
| 8 |
LABELS = ["Negative","Notr","Positive"]
|
| 9 |
MAX_LEN_DEFAULT = 256
|
|
@@ -12,7 +11,6 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
| 12 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 13 |
model.eval()
|
| 14 |
|
| 15 |
-
# ---- POST-PROCESS (Notr kuralı) ----
|
| 16 |
def postprocess_label(probs, min_conf=0.55, close_gap=0.10):
|
| 17 |
neg, notr, pos = probs
|
| 18 |
top = probs.argmax()
|
|
@@ -37,7 +35,6 @@ def infer_one(text, max_len=MAX_LEN_DEFAULT, use_rule=False, min_conf=0.55, clos
|
|
| 37 |
return label, scores
|
| 38 |
|
| 39 |
def infer_batch(texts, max_len=MAX_LEN_DEFAULT, use_rule=False, min_conf=0.55, close_gap=0.10):
|
| 40 |
-
# çoklu giriş: her satır bir örnek
|
| 41 |
if isinstance(texts, str):
|
| 42 |
rows = [t.strip() for t in texts.split("\n") if t.strip()]
|
| 43 |
else:
|
|
@@ -53,8 +50,6 @@ def export_csv(table):
|
|
| 53 |
df = pd.DataFrame(table, columns=["text","label","Negative","Notr","Positive"])
|
| 54 |
return gr.File.update(value=df.to_csv(index=False).encode("utf-8"), visible=True)
|
| 55 |
|
| 56 |
-
# ---- TEMA + STİL ----
|
| 57 |
-
# Not: .set(...) kullanmıyoruz; bazı Space sürümlerinde desteklenmiyor.
|
| 58 |
theme = gr.themes.Soft(primary_hue="blue", neutral_hue="slate")
|
| 59 |
CUSTOM_CSS = """
|
| 60 |
#title h1 { font-weight: 800; letter-spacing: -0.02em; }
|
|
@@ -62,7 +57,6 @@ footer {opacity:.7}
|
|
| 62 |
"""
|
| 63 |
|
| 64 |
with gr.Blocks(theme=theme, css=CUSTOM_CSS, title="Türkçe Duygu Analizi | BERT") as demo:
|
| 65 |
-
# ÜST BÖLÜM
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column(scale=7):
|
| 68 |
gr.Markdown(
|
|
@@ -78,7 +72,6 @@ with gr.Blocks(theme=theme, css=CUSTOM_CSS, title="Türkçe Duygu Analizi | BERT
|
|
| 78 |
close_gap = gr.Slider(0.00, 0.50, value=0.10, step=0.01, label="Neg-Pos yakınlık eşiği")
|
| 79 |
max_len = gr.Slider(64, 384, value=MAX_LEN_DEFAULT, step=8, label="Maks. token uzunluğu")
|
| 80 |
|
| 81 |
-
# ANA İÇERİK
|
| 82 |
with gr.Tabs():
|
| 83 |
with gr.Tab("Tek Cümle"):
|
| 84 |
with gr.Row():
|
|
@@ -102,7 +95,6 @@ with gr.Blocks(theme=theme, css=CUSTOM_CSS, title="Türkçe Duygu Analizi | BERT
|
|
| 102 |
inputs=txt
|
| 103 |
)
|
| 104 |
|
| 105 |
-
# Etkileşim
|
| 106 |
btn.click(
|
| 107 |
fn=infer_one,
|
| 108 |
inputs=[txt, max_len, use_rule, min_conf, close_gap],
|
|
@@ -128,7 +120,6 @@ with gr.Blocks(theme=theme, css=CUSTOM_CSS, title="Türkçe Duygu Analizi | BERT
|
|
| 128 |
)
|
| 129 |
to_csv.click(export_csv, inputs=table, outputs=csv_file)
|
| 130 |
|
| 131 |
-
# ALTLIK
|
| 132 |
gr.Markdown("---")
|
| 133 |
gr.Markdown("💡 İpucu: Nötr cümleler negatif/pozitife kayıyorsa, sağdaki <b>Notr kuralı</b> ayarlarını kullanarak dengeleyebilirsin.")
|
| 134 |
gr.Markdown("© 2025 • Gradio ile oluşturuldu")
|
|
|
|
| 3 |
from torch.nn.functional import softmax
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
|
|
|
|
| 6 |
MODEL_ID = "celalkartoglu/tr-sentiment-bert-win-v1"
|
| 7 |
LABELS = ["Negative","Notr","Positive"]
|
| 8 |
MAX_LEN_DEFAULT = 256
|
|
|
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 12 |
model.eval()
|
| 13 |
|
|
|
|
| 14 |
def postprocess_label(probs, min_conf=0.55, close_gap=0.10):
|
| 15 |
neg, notr, pos = probs
|
| 16 |
top = probs.argmax()
|
|
|
|
| 35 |
return label, scores
|
| 36 |
|
| 37 |
def infer_batch(texts, max_len=MAX_LEN_DEFAULT, use_rule=False, min_conf=0.55, close_gap=0.10):
|
|
|
|
| 38 |
if isinstance(texts, str):
|
| 39 |
rows = [t.strip() for t in texts.split("\n") if t.strip()]
|
| 40 |
else:
|
|
|
|
| 50 |
df = pd.DataFrame(table, columns=["text","label","Negative","Notr","Positive"])
|
| 51 |
return gr.File.update(value=df.to_csv(index=False).encode("utf-8"), visible=True)
|
| 52 |
|
|
|
|
|
|
|
| 53 |
theme = gr.themes.Soft(primary_hue="blue", neutral_hue="slate")
|
| 54 |
CUSTOM_CSS = """
|
| 55 |
#title h1 { font-weight: 800; letter-spacing: -0.02em; }
|
|
|
|
| 57 |
"""
|
| 58 |
|
| 59 |
with gr.Blocks(theme=theme, css=CUSTOM_CSS, title="Türkçe Duygu Analizi | BERT") as demo:
|
|
|
|
| 60 |
with gr.Row():
|
| 61 |
with gr.Column(scale=7):
|
| 62 |
gr.Markdown(
|
|
|
|
| 72 |
close_gap = gr.Slider(0.00, 0.50, value=0.10, step=0.01, label="Neg-Pos yakınlık eşiği")
|
| 73 |
max_len = gr.Slider(64, 384, value=MAX_LEN_DEFAULT, step=8, label="Maks. token uzunluğu")
|
| 74 |
|
|
|
|
| 75 |
with gr.Tabs():
|
| 76 |
with gr.Tab("Tek Cümle"):
|
| 77 |
with gr.Row():
|
|
|
|
| 95 |
inputs=txt
|
| 96 |
)
|
| 97 |
|
|
|
|
| 98 |
btn.click(
|
| 99 |
fn=infer_one,
|
| 100 |
inputs=[txt, max_len, use_rule, min_conf, close_gap],
|
|
|
|
| 120 |
)
|
| 121 |
to_csv.click(export_csv, inputs=table, outputs=csv_file)
|
| 122 |
|
|
|
|
| 123 |
gr.Markdown("---")
|
| 124 |
gr.Markdown("💡 İpucu: Nötr cümleler negatif/pozitife kayıyorsa, sağdaki <b>Notr kuralı</b> ayarlarını kullanarak dengeleyebilirsin.")
|
| 125 |
gr.Markdown("© 2025 • Gradio ile oluşturuldu")
|