Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +59 -3
- requirements.txt +2 -0
app.py
CHANGED
@@ -2,8 +2,37 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
import torch
|
4 |
import os
|
|
|
|
|
|
|
|
|
|
|
5 |
from transformers import BertTokenizer, BertModel
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
class BertClassifier(torch.nn.Module):
|
8 |
def __init__(self, dropout=0.5):
|
9 |
super(BertClassifier, self).__init__()
|
@@ -35,7 +64,9 @@ class BertClassifier(torch.nn.Module):
|
|
35 |
model = BertClassifier()
|
36 |
tokenizer = BertTokenizer.from_pretrained("dbmdz/bert-base-turkish-uncased")
|
37 |
model.load_state_dict(torch.load('tubitak2.pt', map_location=torch.device('cpu')))
|
38 |
-
|
|
|
|
|
39 |
def predict_text(model, sentence):
|
40 |
device = torch.device("cpu")
|
41 |
#model = model.cuda()
|
@@ -68,10 +99,35 @@ def predict_text(model, sentence):
|
|
68 |
# Kategorik sınıfı döndür.
|
69 |
return categories.get(output.argmax(dim=1).item())
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
def predict(df):
|
72 |
# TODO:
|
73 |
-
|
74 |
-
|
|
|
75 |
for i in range(len(df)):
|
76 |
df.loc[i, 'label'] = predict_text(model, df['text'][i])
|
77 |
|
|
|
2 |
import pandas as pd
|
3 |
import torch
|
4 |
import os
|
5 |
+
import re
|
6 |
+
import string
|
7 |
+
import ntlk
|
8 |
+
import emoji
|
9 |
+
from nltk.corpus import stopwords
|
10 |
from transformers import BertTokenizer, BertModel
|
11 |
|
12 |
+
nltk.download('stopwords')
|
13 |
+
stop_words_list = stopwords.words('turkish')
|
14 |
+
|
15 |
+
# Ön işleme adımlarını yapmak için fonksiyonumuzu tanımlıyoruz.
|
16 |
+
def preprocess_text(text):
|
17 |
+
# Küçük harflere çevirme
|
18 |
+
text = text.lower()
|
19 |
+
# Satır sonu karakterlerini kaldırma
|
20 |
+
text = re.sub(r'\n', ' ', text)
|
21 |
+
# Rakamları kaldırma
|
22 |
+
text = re.sub(r'\d', '', text)
|
23 |
+
# Noktalama işaretlerini kaldırma
|
24 |
+
text = text.translate(str.maketrans("", "", string.punctuation))
|
25 |
+
# Stop-words'leri kaldırma
|
26 |
+
words = text.split()
|
27 |
+
words = [word for word in words if not word in stop_words_list]
|
28 |
+
# Tekrarlanan karakterlerin kaldırılması
|
29 |
+
words = [re.sub(r'(.)\1{1,}', r'\1\1', word) for word in words]
|
30 |
+
# Tekrarlanan boşlukların kaldırılması
|
31 |
+
words = [word.strip() for word in words if len(word.strip()) > 1]
|
32 |
+
|
33 |
+
text = " ".join(words)
|
34 |
+
return text
|
35 |
+
|
36 |
class BertClassifier(torch.nn.Module):
|
37 |
def __init__(self, dropout=0.5):
|
38 |
super(BertClassifier, self).__init__()
|
|
|
64 |
model = BertClassifier()
|
65 |
tokenizer = BertTokenizer.from_pretrained("dbmdz/bert-base-turkish-uncased")
|
66 |
model.load_state_dict(torch.load('tubitak2.pt', map_location=torch.device('cpu')))
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
def predict_text(model, sentence):
|
71 |
device = torch.device("cpu")
|
72 |
#model = model.cuda()
|
|
|
99 |
# Kategorik sınıfı döndür.
|
100 |
return categories.get(output.argmax(dim=1).item())
|
101 |
|
102 |
+
import re
|
103 |
+
|
104 |
+
# Ön işleme adımlarını yapmak için fonksiyonumuzu tanımlıyoruz.
|
105 |
+
def preprocess_text(text):
|
106 |
+
# Küçük harflere çevirme
|
107 |
+
text = text.lower()
|
108 |
+
# Satır sonu karakterlerini kaldırma
|
109 |
+
text = re.sub(r'\n', ' ', text)
|
110 |
+
# Rakamları kaldırma
|
111 |
+
text = re.sub(r'\d', '', text)
|
112 |
+
# Noktalama işaretlerini kaldırma
|
113 |
+
import string
|
114 |
+
text = text.translate(str.maketrans("", "", string.punctuation))
|
115 |
+
# Stop-words'leri kaldırma
|
116 |
+
words = text.split()
|
117 |
+
words = [word for word in words if not word in stop_words_list]
|
118 |
+
# Tekrarlanan karakterlerin kaldırılması
|
119 |
+
words = [re.sub(r'(.)\1{1,}', r'\1\1', word) for word in words]
|
120 |
+
# Tekrarlanan boşlukların kaldırılması
|
121 |
+
words = [word.strip() for word in words if len(word.strip()) > 1]
|
122 |
+
|
123 |
+
text = " ".join(words)
|
124 |
+
return text
|
125 |
+
|
126 |
def predict(df):
|
127 |
# TODO:
|
128 |
+
regex = r'@\w+\s?'
|
129 |
+
df['clean_text'] = df['text'].apply(lambda x: re.sub(regex, '', x))
|
130 |
+
df['clean_text'] = df['clean_text'].apply(preprocess_text)
|
131 |
for i in range(len(df)):
|
132 |
df.loc[i, 'label'] = predict_text(model, df['text'][i])
|
133 |
|
requirements.txt
CHANGED
@@ -1,4 +1,6 @@
|
|
|
|
1 |
gradio==3.28.3
|
|
|
2 |
pandas==1.5.3
|
3 |
torch==2.0.0
|
4 |
transformers==4.27.2
|
|
|
1 |
+
emoji==2.2.0
|
2 |
gradio==3.28.3
|
3 |
+
nltk==3.8
|
4 |
pandas==1.5.3
|
5 |
torch==2.0.0
|
6 |
transformers==4.27.2
|