Spaces:
Sleeping
Sleeping
Mert Şengil
commited on
Commit
·
19995b3
1
Parent(s):
9a13bdc
Add Turkish aspect term extraction app
Browse files- app.py +123 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
import torch
|
4 |
+
import re
|
5 |
+
from collections import Counter
|
6 |
+
|
7 |
+
# LOAD MODEL
|
8 |
+
MODEL_ID = "Sengil/t5-turkish-aspect-term-extractor"
|
9 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
+
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID).to(DEVICE)
|
13 |
+
model.eval()
|
14 |
+
|
15 |
+
TURKISH_STOPWORDS = {
|
16 |
+
"ve", "çok", "ama", "bir", "bu", "daha", "gibi", "ile", "için",
|
17 |
+
"de", "da", "ki", "o", "şu", "bu", "sen", "biz", "siz", "onlar"
|
18 |
+
}
|
19 |
+
|
20 |
+
def is_valid_aspect(word):
|
21 |
+
word = word.strip().lower()
|
22 |
+
return (
|
23 |
+
len(word) > 1 and
|
24 |
+
word not in TURKISH_STOPWORDS and
|
25 |
+
word.isalpha()
|
26 |
+
)
|
27 |
+
|
28 |
+
def extract_and_rank_aspects(text, max_tokens=64, beams=5):
|
29 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(DEVICE)
|
30 |
+
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = model.generate(
|
33 |
+
input_ids=inputs["input_ids"],
|
34 |
+
attention_mask=inputs["attention_mask"],
|
35 |
+
max_new_tokens=max_tokens,
|
36 |
+
num_beams=beams,
|
37 |
+
num_return_sequences=beams,
|
38 |
+
early_stopping=True
|
39 |
+
)
|
40 |
+
|
41 |
+
all_predictions = [
|
42 |
+
tokenizer.decode(output, skip_special_tokens=True)
|
43 |
+
for output in outputs
|
44 |
+
]
|
45 |
+
|
46 |
+
all_terms = []
|
47 |
+
for pred in all_predictions:
|
48 |
+
candidates = re.split(r"[;,–—\-]|(?:\s*,\s*)", pred)
|
49 |
+
all_terms.extend([w.strip().lower() for w in candidates if is_valid_aspect(w)])
|
50 |
+
|
51 |
+
ranked = Counter(all_terms).most_common()
|
52 |
+
return ranked
|
53 |
+
|
54 |
+
def process_text(text):
|
55 |
+
if not text.strip():
|
56 |
+
return "Lütfen analiz edilecek bir metin girin."
|
57 |
+
|
58 |
+
try:
|
59 |
+
ranked_aspects = extract_and_rank_aspects(text)
|
60 |
+
|
61 |
+
if not ranked_aspects:
|
62 |
+
return "Metinde herhangi bir aspect term bulunamadı."
|
63 |
+
|
64 |
+
result = "🎯 **Bulunan Aspect Terimler:**\n\n"
|
65 |
+
for i, (term, score) in enumerate(ranked_aspects, 1):
|
66 |
+
result += f"{i}. **{term.title()}** - Skor: {score}\n"
|
67 |
+
|
68 |
+
return result
|
69 |
+
except Exception as e:
|
70 |
+
return f"Hata oluştu: {str(e)}"
|
71 |
+
|
72 |
+
# Gradio Interface
|
73 |
+
with gr.Blocks(title="🇹🇷 Türkçe Aspect Term Extraction", theme=gr.themes.Soft()) as demo:
|
74 |
+
gr.HTML("""
|
75 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
76 |
+
<h1>🇹🇷 Türkçe Aspect Term Extraction</h1>
|
77 |
+
<p>T5 tabanlı model ile Türkçe metinlerden aspect terimleri çıkarın</p>
|
78 |
+
<p><i>Model: Sengil/t5-turkish-aspect-term-extractor</i></p>
|
79 |
+
</div>
|
80 |
+
""")
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
with gr.Column():
|
84 |
+
text_input = gr.Textbox(
|
85 |
+
label="📝 Analiz edilecek metin",
|
86 |
+
placeholder="Örnek: Artılar: Göl manzarasıyla harika bir atmosfer, Ipoh'un her zaman sıcak olan havası nedeniyle iyi bir klima olan restoran...",
|
87 |
+
lines=5,
|
88 |
+
max_lines=10
|
89 |
+
)
|
90 |
+
|
91 |
+
analyze_btn = gr.Button("🔍 Aspect Terimleri Çıkar", variant="primary", size="lg")
|
92 |
+
|
93 |
+
with gr.Column():
|
94 |
+
output = gr.Markdown(
|
95 |
+
label="📊 Sonuçlar",
|
96 |
+
value="Sonuçlar burada görünecek..."
|
97 |
+
)
|
98 |
+
|
99 |
+
# Example texts
|
100 |
+
gr.HTML("<h3>📋 Örnek Metinler:</h3>")
|
101 |
+
|
102 |
+
examples = [
|
103 |
+
["Artılar: Göl manzarasıyla harika bir atmosfer, Ipoh'un her zaman sıcak olan havası nedeniyle iyi bir klima olan restoran, iyi ve hızlı hizmet sunan garsonlar, temassız ödeme kabul eden e-cüzdan, ücretsiz otopark ama sıcak güneş altında açık, yemeklerin tadı güzel."],
|
104 |
+
["Bu otelin konumu mükemmel, personel çok yardımısever. Kahvaltı çeşitliliği iyi ancak oda temizliği yetersiz. WiFi hızı da çok yavaş."],
|
105 |
+
["Ürünün kalitesi harika, kargo hızlı geldi. Fiyat biraz yüksek ama memnunum. Müşteri hizmeti de gayet iyi."]
|
106 |
+
]
|
107 |
+
|
108 |
+
gr.Examples(
|
109 |
+
examples=examples,
|
110 |
+
inputs=[text_input],
|
111 |
+
outputs=[output],
|
112 |
+
fn=process_text,
|
113 |
+
cache_examples=False
|
114 |
+
)
|
115 |
+
|
116 |
+
analyze_btn.click(
|
117 |
+
fn=process_text,
|
118 |
+
inputs=[text_input],
|
119 |
+
outputs=[output]
|
120 |
+
)
|
121 |
+
|
122 |
+
if __name__ == "__main__":
|
123 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers>=4.30.0
|
2 |
+
torch>=2.0.0
|
3 |
+
torchvision
|
4 |
+
torchaudio
|
5 |
+
accelerate
|
6 |
+
sentencepiece
|
7 |
+
protobuf
|