Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer, AutoModelForTokenClassification,
|
3 |
import pandas as pd
|
4 |
import spacy
|
5 |
-
import io
|
6 |
-
import torch
|
7 |
-
import torchaudio
|
8 |
-
from transformers import Wav2Vec2ForCTC, Wav2Vec2FeatureExtractor
|
9 |
-
from transformers import Wav2Vec2Processor
|
10 |
|
11 |
st.set_page_config(layout="wide")
|
12 |
|
@@ -20,13 +15,14 @@ Birinci Dünya Savaşı sırasında Osmanlı ordusunda görev yapan Atatürk, Ç
|
|
20 |
# Uygulama başlığı
|
21 |
st.title("NLP Toolkit")
|
22 |
|
|
|
23 |
# Model seçim
|
24 |
-
model_list =
|
25 |
-
|
26 |
'Metin Analizi': 'savasy/bert-base-turkish-ner-cased',
|
27 |
'Duygu Analizi': 'akdeniz27/xlm-roberta-base-turkish-ner',
|
28 |
'Metin Oluşturma': 'dbmdz/bert-base-turkish-cased'
|
29 |
-
|
30 |
|
31 |
st.sidebar.header("Select NER Model")
|
32 |
model_checkpoint = st.sidebar.radio("", model_list)
|
@@ -45,7 +41,7 @@ else:
|
|
45 |
|
46 |
# Metin giriş yöntemi
|
47 |
st.subheader("Metin Giriş Yöntemi Seç")
|
48 |
-
input_method = st.radio("", ('Örneklerden Seç', 'Metin Yaz veya Yapıştır', 'Dosya Yükle'
|
49 |
|
50 |
if input_method == 'Örneklerden Seç':
|
51 |
selected_text = st.selectbox('Metin Seç', example_list, index=0, key=1)
|
@@ -61,11 +57,6 @@ elif input_method == "Dosya Yükle":
|
|
61 |
input_text = str(uploaded_file.read(), "utf-8")
|
62 |
else:
|
63 |
input_text = ""
|
64 |
-
elif input_method == "Ses Dosyası Yükle":
|
65 |
-
st.subheader("Ses Dosyası")
|
66 |
-
uploaded_audio = st.file_uploader("Ses Dosyasını Seç", type=["wav"], key="audio_file_uploader")
|
67 |
-
if uploaded_audio is not None:
|
68 |
-
audio_bytes = uploaded_audio.read()
|
69 |
|
70 |
@st.cache_resource
|
71 |
def load_pipeline(model_name, task_type):
|
@@ -82,7 +73,7 @@ def load_pipeline(model_name, task_type):
|
|
82 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
83 |
return pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
|
84 |
elif task_type == "Metin Oluşturma":
|
85 |
-
model =
|
86 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
87 |
return pipeline('text-generation', model=model, tokenizer=tokenizer)
|
88 |
|
@@ -111,49 +102,9 @@ def entity_comb(output):
|
|
111 |
output_comb.append(entity)
|
112 |
return output_comb
|
113 |
|
114 |
-
# Ses dosyasını metne çevirme fonksiyonu
|
115 |
-
def transcribe_audio(audio_file):
|
116 |
-
# Wav2Vec2 model ve feature extractor yükleme
|
117 |
-
processor = Wav2Vec2FeatureExtractor.from_pretrained("facebook/wav2vec2-large-xlsr-53")
|
118 |
-
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-xlsr-53")
|
119 |
-
|
120 |
-
# Ses dosyasını yükleme
|
121 |
-
audio_input = io.BytesIO(audio_file)
|
122 |
-
waveform, sample_rate = torchaudio.load(audio_input, normalize=True)
|
123 |
-
|
124 |
-
# Ses verisini işleme
|
125 |
-
inputs = processor(waveform.squeeze().numpy(), sampling_rate=sample_rate, return_tensors="pt", padding="longest")
|
126 |
-
|
127 |
-
# Model ile tahmin yapma
|
128 |
-
with torch.no_grad():
|
129 |
-
logits = model(inputs.input_values).logits
|
130 |
-
|
131 |
-
# Tahmin sonuçlarını çözme
|
132 |
-
predicted_ids = torch.argmax(logits, dim=-1)
|
133 |
-
transcription = processor.batch_decode(predicted_ids)[0]
|
134 |
-
|
135 |
-
return transcription
|
136 |
-
|
137 |
Run_Button = st.button("Çalıştır", key=None)
|
138 |
|
139 |
-
if
|
140 |
-
st.subheader("Metin Girişi")
|
141 |
-
input_text = st.text_area("Metni buraya yazın veya yapıştırın:", key="text_input_area")
|
142 |
-
|
143 |
-
if input_text != "":
|
144 |
-
st.subheader("Girdiğiniz Metin")
|
145 |
-
st.write(input_text)
|
146 |
-
|
147 |
-
elif input_method == "Ses Dosyası Yükle":
|
148 |
-
st.subheader("Ses Dosyası")
|
149 |
-
uploaded_audio = st.file_uploader("Ses Dosyasını Seç", type=["wav"], key="audio_file_uploader")
|
150 |
-
|
151 |
-
if uploaded_audio is not None:
|
152 |
-
transcription = transcribe_audio(uploaded_audio)
|
153 |
-
st.subheader("Ses Transkripsiyonu")
|
154 |
-
st.write(transcription)
|
155 |
-
|
156 |
-
if input_text != "":
|
157 |
if task == "Metin Sınıflandırma":
|
158 |
pipeline_model = load_pipeline(model_checkpoint, task)
|
159 |
output = pipeline_model(input_text)
|
@@ -206,4 +157,4 @@ if input_text != "":
|
|
206 |
output = pipeline_model(input_text, max_length=50, num_return_sequences=1)
|
207 |
st.subheader(f"{task} Sonuçları")
|
208 |
for idx, item in enumerate(output):
|
209 |
-
st.write(f"Öneri {idx+1}: {item['generated_text']}")
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer, AutoModelForTokenClassification, AutoModelWithLMHead
|
3 |
import pandas as pd
|
4 |
import spacy
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
st.set_page_config(layout="wide")
|
7 |
|
|
|
15 |
# Uygulama başlığı
|
16 |
st.title("NLP Toolkit")
|
17 |
|
18 |
+
|
19 |
# Model seçim
|
20 |
+
model_list = [
|
21 |
+
'Metin Sınıflandırma': 'dbmdz/bert-base-turkish-cased',
|
22 |
'Metin Analizi': 'savasy/bert-base-turkish-ner-cased',
|
23 |
'Duygu Analizi': 'akdeniz27/xlm-roberta-base-turkish-ner',
|
24 |
'Metin Oluşturma': 'dbmdz/bert-base-turkish-cased'
|
25 |
+
]
|
26 |
|
27 |
st.sidebar.header("Select NER Model")
|
28 |
model_checkpoint = st.sidebar.radio("", model_list)
|
|
|
41 |
|
42 |
# Metin giriş yöntemi
|
43 |
st.subheader("Metin Giriş Yöntemi Seç")
|
44 |
+
input_method = st.radio("", ('Örneklerden Seç', 'Metin Yaz veya Yapıştır', 'Dosya Yükle'))
|
45 |
|
46 |
if input_method == 'Örneklerden Seç':
|
47 |
selected_text = st.selectbox('Metin Seç', example_list, index=0, key=1)
|
|
|
57 |
input_text = str(uploaded_file.read(), "utf-8")
|
58 |
else:
|
59 |
input_text = ""
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
@st.cache_resource
|
62 |
def load_pipeline(model_name, task_type):
|
|
|
73 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
74 |
return pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
|
75 |
elif task_type == "Metin Oluşturma":
|
76 |
+
model = AutoModelWithLMHead.from_pretrained(model_name)
|
77 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
78 |
return pipeline('text-generation', model=model, tokenizer=tokenizer)
|
79 |
|
|
|
102 |
output_comb.append(entity)
|
103 |
return output_comb
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
Run_Button = st.button("Çalıştır", key=None)
|
106 |
|
107 |
+
if Run_Button and input_text != "":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
if task == "Metin Sınıflandırma":
|
109 |
pipeline_model = load_pipeline(model_checkpoint, task)
|
110 |
output = pipeline_model(input_text)
|
|
|
157 |
output = pipeline_model(input_text, max_length=50, num_return_sequences=1)
|
158 |
st.subheader(f"{task} Sonuçları")
|
159 |
for idx, item in enumerate(output):
|
160 |
+
st.write(f"Öneri {idx+1}: {item['generated_text']}")
|