elfgk commited on
Commit
1089e54
·
verified ·
1 Parent(s): fd6f765

Upload 2 files

Browse files
Files changed (2) hide show
  1. main.py +55 -0
  2. main.py~ +70 -0
main.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from gtts import gTTS
4
+ from datetime import datetime
5
+
6
+
7
+ def text_to_speech(file, text_input, lang_input):
8
+ # Kullanıcı dosya yüklediyse, dosyadan metin oku
9
+ if file is not None:
10
+ try:
11
+ with open(file.name, 'r', encoding='utf-8') as f:
12
+ metin = f.read()
13
+ except Exception as e:
14
+ return f"Dosya okunamadı: {e}"
15
+ # Dosya yoksa ve kullanıcı doğrudan metin girdiyse, metni kullan
16
+ elif text_input:
17
+ metin = text_input
18
+ else:
19
+ return "Lütfen bir dosya yükleyin veya metin girin."
20
+
21
+ try:
22
+ # gTTS nesnesini oluştur
23
+ kayit = gTTS(text=metin, lang=lang_input, slow=False)
24
+ dosya_adi = f"ses_{lang_input}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp3"
25
+ kayit.save(dosya_adi)
26
+
27
+ # Ses dosyasının yolunu döndür
28
+ if os.path.exists(dosya_adi):
29
+ return dosya_adi # Bu dosyayı indirme linki olarak döndüreceğiz
30
+ else:
31
+ return "Ses kaydedilemedi."
32
+ except Exception as e:
33
+ return f"Bir hata oluştu: {e}"
34
+
35
+
36
+ # Gradio arayüzü
37
+ with gr.Blocks() as demo:
38
+ gr.Markdown("# Metni veya Dosyayı Seslendirme Uygulaması")
39
+
40
+ with gr.Row():
41
+ file_input = gr.File(label="Metin Dosyasını Yükle (.txt)")
42
+ text_input = gr.Textbox(label="Metin Girin", placeholder="Seslendirmek istediğiniz metni buraya yazın.")
43
+
44
+ lang_input = gr.Radio(["tr", "en", "fr"], label="Dil Seçimi", value="tr")
45
+ output = gr.Audio(label="Ses Dosyası", interactive=True)
46
+
47
+ submit_button = gr.Button("Seslendir")
48
+ submit_button.click(
49
+ fn=text_to_speech,
50
+ inputs=[file_input, text_input, lang_input],
51
+ outputs=output
52
+ )
53
+
54
+ # Uygulamayı çalıştır
55
+ demo.launch(share=True)
main.py~ ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from gtts import gTTS
4
+ from pydub import AudioSegment
5
+ from datetime import datetime
6
+
7
+
8
+ def text_to_speech(file, text_input, lang_input, speed_input):
9
+ # Kullanıcı dosya yüklediyse, dosyadan metin oku
10
+ if file is not None:
11
+ try:
12
+ with open(file.name, 'r', encoding='utf-8') as f:
13
+ metin = f.read()
14
+ except Exception as e:
15
+ return f"Dosya okunamadı: {e}"
16
+ # Dosya yoksa ve kullanıcı doğrudan metin girdiyse, metni kullan
17
+ elif text_input:
18
+ metin = text_input
19
+ else:
20
+ return "Lütfen bir dosya yükleyin veya metin girin."
21
+
22
+ try:
23
+ # gTTS nesnesini oluştur
24
+ kayit = gTTS(text=metin, lang=lang_input, slow=False) # slow=False ile normal hızda başlat
25
+ dosya_adi = f"ses_{lang_input}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp3"
26
+ kayit.save(dosya_adi)
27
+
28
+ # Ses dosyasını hızlandır veya yavaşlat
29
+ sound = AudioSegment.from_mp3(dosya_adi)
30
+
31
+ # Hız seçeneğine göre ses hızını ayarla
32
+ if speed_input == "Yavaş":
33
+ sound = sound.speedup(playback_speed=0.75) # Ses hızını yavaşlat
34
+ elif speed_input == "Hızlı":
35
+ sound = sound.speedup(playback_speed=1.25) # Ses hızını hızlandır
36
+
37
+ # Yeni ses dosyasını kaydet
38
+ dosya_adi = f"ses_{lang_input}_{speed_input}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp3"
39
+ sound.export(dosya_adi, format="mp3")
40
+
41
+ # Ses dosyasının yolunu döndür
42
+ if os.path.exists(dosya_adi):
43
+ return dosya_adi # Bu dosyayı indirme linki olarak döndüreceğiz
44
+ else:
45
+ return "Ses kaydedilemedi."
46
+ except Exception as e:
47
+ return f"Bir hata oluştu: {e}"
48
+
49
+
50
+ # Gradio arayüzü
51
+ with gr.Blocks() as demo:
52
+ gr.Markdown("# Metni veya Dosyayı Seslendirme Uygulaması")
53
+
54
+ with gr.Row():
55
+ file_input = gr.File(label="Metin Dosyasını Yükle (.txt)")
56
+ text_input = gr.Textbox(label="Metin Girin", placeholder="Seslendirmek istediğiniz metni buraya yazın.")
57
+
58
+ lang_input = gr.Radio(["tr", "en", "fr"], label="Dil Seçimi", value="tr")
59
+ speed_input = gr.Radio(["Hızlı", "Yavaş"], label="Seslendirme Hızı", value="Hızlı")
60
+ output = gr.Audio(label="Ses Dosyası", interactive=True)
61
+
62
+ submit_button = gr.Button("Seslendir")
63
+ submit_button.click(
64
+ fn=text_to_speech,
65
+ inputs=[file_input, text_input, lang_input, speed_input],
66
+ outputs=output
67
+ )
68
+
69
+ # Uygulamayı çalıştır
70
+ demo.launch(share=True)