-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
203 lines (90 loc) · 2.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
"""BERTT.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1QIlNsVXXwpMp8u-BAYEW3BdUEzLbcoge
"""
from google.colab import drive
drive.mount('/content/drive')
import numpy as np
import pandas as pd
!pip install simpletransformers
from simpletransformers.classification import ClassificationModel
a=pd.read_excel("/content/drive/MyDrive/Bert/Projem.xlsx")
a
a["kategori"].unique()
a['labels'] = pd.factorize(a.kategori)[0]
from sklearn.model_selection import train_test_split
train, test = train_test_split(a, test_size=0.2, random_state=42)
train=train[["metin","labels"]]
test=test[["metin","labels"]]
#for bert text = string label = int
train["metin"]=train["metin"].apply(lambda r: str(r))
train['labels']=train['labels'].astype(int)
model = ClassificationModel('bert', 'dbmdz/bert-base-turkish-uncased', num_labels=5, use_cuda=False,
args={'reprocess_input_data': True, 'overwrite_output_dir': True, 'num_train_epochs': 3, "train_batch_size": 64 , "fp16":False, "output_dir": "bert_model"})
model.train_model(train)
result, model_outputs, wrong_predictions = model.eval_model(test)
predictions = model_outputs.argmax(axis=1)
actuals = test.labels.values
predictions[:10]
actuals[:10]
from sklearn.metrics import accuracy_score
accuracy_score(actuals, predictions)
örnek = test.iloc[43]['metin']
print(örnek)
tahmin=model.predict([dene])
if tahmin[0] ==0:
print("BİLİM VE TEKNOLOJİ")
elif tahmin[0]==1:
print("EKONOMİ")
elif tahmin[0]==2:
print("SAĞLIK")
elif tahmin[0]==3:
print("SİYASET")
else:
print("SPOR")
#deneme import
denemem=pd.read_excel("/content/drive/MyDrive/Bert/denemem.xlsx",names=["kategori","metin"])
denemem
dene=denemem.iloc[3]["metin"]
örnek=denemem.iloc[11]["metin"]
tahmin=model.predict([örnek])
if tahmin[0] ==0:
print("BİLİM VE TEKNOLOJİ")
elif tahmin[0]==1:
print("EKONOMİ")
elif tahmin[0]==2:
print("SAĞLIK")
elif tahmin[0]==3:
print("SİYASET")
else:
print("SPOR")
# ses kaydından sınıflandırma
import speech_recognition as sr
recognizer = sr.Recognizer()
''' recording the sound '''
with sr.AudioFile("/content/drive/MyDrive/Bert/Bilim Ve Teknoloji.wav") as source:
recorded_audio = recognizer.listen(source)
print("Done recording")
''' Recorgnizing the Audio '''
try:
print("Recognizing the text")
text = recognizer.recognize_google(
recorded_audio,
language='tr-tr'
)
model.predict([dene])
except Exception as ex:
print(ex)
c=model.predict([text])
if c[0] ==0:
print("BİLİM VE TEKNOLOJİ")
elif c[0]==1:
print("EKONOMİ")
elif c[0]==2:
print("SAĞLIK")
elif c[0]==3:
print("SİYASET")
else:
print("SPOR")