Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,85 +8,105 @@ import gradio as gr
|
|
| 8 |
from faster_whisper import WhisperModel
|
| 9 |
import tempfile
|
| 10 |
import time
|
|
|
|
| 11 |
|
| 12 |
# ==================== CONFIGURATION ====================
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
# =======================================================
|
| 15 |
|
| 16 |
# Load model once at startup
|
| 17 |
-
print("🔄 Model yükleniyor... (Bu işlem birkaç dakika sürebilir)")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
def transcribe(audio_path: str):
|
| 27 |
"""
|
| 28 |
-
Transcribe audio file to Turkish text.
|
| 29 |
-
Returns: (transcription_text, download_file_path)
|
| 30 |
"""
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
if audio_path is None:
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
try:
|
| 36 |
start_time = time.time()
|
| 37 |
|
| 38 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
segments, info = model.transcribe(
|
| 40 |
audio_path,
|
| 41 |
language="tr",
|
| 42 |
-
beam_size=5
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
full_text =
|
| 47 |
-
for segment in segments:
|
| 48 |
-
full_text.append(segment.text)
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
elapsed = time.time() - start_time
|
|
|
|
| 52 |
|
| 53 |
-
if not
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
txt_file = tempfile.NamedTemporaryFile(
|
| 58 |
mode='w',
|
| 59 |
suffix='.txt',
|
| 60 |
delete=False,
|
| 61 |
encoding='utf-8'
|
| 62 |
)
|
| 63 |
-
txt_file.write(
|
| 64 |
txt_file.close()
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
───────────────────────────────────
|
| 70 |
-
📊 İstatistikler
|
| 71 |
-
• Algılanan dil: Türkçe
|
| 72 |
-
• Ses süresi: {info.duration:.1f} saniye
|
| 73 |
-
• İşlem süresi: {elapsed:.1f} saniye
|
| 74 |
-
• Hız: {info.duration/elapsed:.1f}x gerçek zamanlı
|
| 75 |
-
───────────────────────────────────"""
|
| 76 |
-
|
| 77 |
-
return display_text, txt_file.name
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
-
|
| 81 |
-
if "ffmpeg" in error_msg.lower():
|
| 82 |
-
return "❌ Ses dosyası işlenemedi. Desteklenen formatlar: MP3, WAV, M4A, OGG, FLAC", None
|
| 83 |
-
return f"❌ Bir hata oluştu: {error_msg}", None
|
| 84 |
|
| 85 |
|
| 86 |
-
# Build Interface
|
| 87 |
with gr.Blocks(title="Ses Deşifre Pro") as demo:
|
| 88 |
|
| 89 |
-
# Header
|
| 90 |
gr.HTML("""
|
| 91 |
<style>
|
| 92 |
footer { display: none !important; }
|
|
@@ -96,7 +116,7 @@ with gr.Blocks(title="Ses Deşifre Pro") as demo:
|
|
| 96 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 97 |
border-radius: 20px; margin-bottom: 24px; color: white;">
|
| 98 |
<h1 style="font-size: 2.5rem; font-weight: 700; margin: 0 0 8px 0;">
|
| 99 |
-
🎙️ Voice to Text
|
| 100 |
</h1>
|
| 101 |
<p style="font-size: 1.1rem; opacity: 0.95; margin: 0;">
|
| 102 |
Yapay zeka ile Türkçe ses kayıtlarını metne dönüştürün
|
|
@@ -121,18 +141,17 @@ with gr.Blocks(title="Ses Deşifre Pro") as demo:
|
|
| 121 |
size="lg"
|
| 122 |
)
|
| 123 |
|
| 124 |
-
# Tips
|
| 125 |
gr.HTML("""
|
| 126 |
<div style="background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
|
| 127 |
border: 1px solid #86efac; border-radius: 12px;
|
| 128 |
padding: 16px 20px; margin-top: 16px;">
|
| 129 |
<p style="margin: 0; color: #166534; font-size: 14px;">
|
| 130 |
-
💡 <strong>İpucu:</strong>
|
| 131 |
-
|
| 132 |
</p>
|
| 133 |
</div>
|
| 134 |
""")
|
| 135 |
-
|
| 136 |
# Results section
|
| 137 |
with gr.Row():
|
| 138 |
with gr.Column():
|
|
@@ -140,7 +159,7 @@ with gr.Blocks(title="Ses Deşifre Pro") as demo:
|
|
| 140 |
|
| 141 |
output_text = gr.Textbox(
|
| 142 |
label="",
|
| 143 |
-
placeholder="
|
| 144 |
lines=15,
|
| 145 |
interactive=False
|
| 146 |
)
|
|
@@ -148,32 +167,14 @@ with gr.Blocks(title="Ses Deşifre Pro") as demo:
|
|
| 148 |
download_file = gr.File(
|
| 149 |
label="📥 Sonucu İndir (.txt)"
|
| 150 |
)
|
| 151 |
-
|
| 152 |
-
# Features
|
| 153 |
-
gr.HTML("""
|
| 154 |
-
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-top: 24px;">
|
| 155 |
-
<div style="text-align: center; padding: 20px; background: #f9fafb; border-radius: 12px;">
|
| 156 |
-
<div style="font-size: 28px; margin-bottom: 8px;">🚀</div>
|
| 157 |
-
<div style="font-size: 13px; color: #6b7280; font-weight: 500;">Hızlı İşlem</div>
|
| 158 |
-
</div>
|
| 159 |
-
<div style="text-align: center; padding: 20px; background: #f9fafb; border-radius: 12px;">
|
| 160 |
-
<div style="font-size: 28px; margin-bottom: 8px;">🎯</div>
|
| 161 |
-
<div style="font-size: 13px; color: #6b7280; font-weight: 500;">Yüksek Doğruluk</div>
|
| 162 |
-
</div>
|
| 163 |
-
<div style="text-align: center; padding: 20px; background: #f9fafb; border-radius: 12px;">
|
| 164 |
-
<div style="font-size: 28px; margin-bottom: 8px;">🔒</div>
|
| 165 |
-
<div style="font-size: 13px; color: #6b7280; font-weight: 500;">Gizlilik Odaklı</div>
|
| 166 |
-
</div>
|
| 167 |
-
</div>
|
| 168 |
-
""")
|
| 169 |
-
|
| 170 |
# Footer
|
| 171 |
gr.HTML("""
|
| 172 |
<div style="text-align: center; padding: 24px 0; color: #9ca3af; font-size: 13px;">
|
| 173 |
-
<p>Powered by Faster-Whisper AI • CPU Optimized
|
| 174 |
</div>
|
| 175 |
""")
|
| 176 |
-
|
| 177 |
# Event handling
|
| 178 |
submit_btn.click(
|
| 179 |
fn=transcribe,
|
|
@@ -183,4 +184,4 @@ with gr.Blocks(title="Ses Deşifre Pro") as demo:
|
|
| 183 |
|
| 184 |
# Launch
|
| 185 |
if __name__ == "__main__":
|
| 186 |
-
demo.launch(share=False, show_error=True)
|
|
|
|
| 8 |
from faster_whisper import WhisperModel
|
| 9 |
import tempfile
|
| 10 |
import time
|
| 11 |
+
import os
|
| 12 |
|
| 13 |
# ==================== CONFIGURATION ====================
|
| 14 |
+
# CPU için "small" daha hızlıdır. "medium" biraz yavaştır ama kalitelidir.
|
| 15 |
+
# Eğer hala çok yavaşsa burayı "small" yapın.
|
| 16 |
+
MODEL_SIZE = "medium"
|
| 17 |
# =======================================================
|
| 18 |
|
| 19 |
# Load model once at startup
|
| 20 |
+
print(f"🔄 Model yükleniyor ({MODEL_SIZE})... (Bu işlem birkaç dakika sürebilir)")
|
| 21 |
+
try:
|
| 22 |
+
model = WhisperModel(
|
| 23 |
+
MODEL_SIZE,
|
| 24 |
+
device="cpu",
|
| 25 |
+
compute_type="int8"
|
| 26 |
+
)
|
| 27 |
+
print("✅ Model yüklendi!")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"❌ Model yükleme hatası: {e}")
|
| 30 |
+
model = None
|
| 31 |
|
| 32 |
+
def transcribe(audio_path: str, progress=gr.Progress()):
|
| 33 |
"""
|
| 34 |
+
Transcribe audio file to Turkish text with streaming output.
|
|
|
|
| 35 |
"""
|
| 36 |
+
if model is None:
|
| 37 |
+
yield "❌ Hata: Model yüklenemedi. Logları kontrol edin.", None
|
| 38 |
+
return
|
| 39 |
+
|
| 40 |
if audio_path is None:
|
| 41 |
+
yield "⚠️ Lütfen bir ses dosyası yükleyin veya mikrofonla kayıt yapın.", None
|
| 42 |
+
return
|
| 43 |
+
|
| 44 |
try:
|
| 45 |
start_time = time.time()
|
| 46 |
|
| 47 |
+
# Kullanıcıya bilgi ver
|
| 48 |
+
progress(0, desc="Ses dosyası işleniyor...")
|
| 49 |
+
|
| 50 |
+
# 1. Transkripsiyon Başlatılıyor
|
| 51 |
+
# beam_size=1 (Greedy Search) CPU için çok daha hızlıdır.
|
| 52 |
+
# beam_size=5 daha kalitelidir ama 3-4 kat yavaştır.
|
| 53 |
segments, info = model.transcribe(
|
| 54 |
audio_path,
|
| 55 |
language="tr",
|
| 56 |
+
beam_size=1, # HIZ İÇİN 1 YAPILDI (Eskisi 5'ti)
|
| 57 |
+
vad_filter=True, # Sessiz alanları atla (Hız artırır)
|
| 58 |
+
word_timestamps=False
|
| 59 |
)
|
| 60 |
|
| 61 |
+
duration = info.duration
|
| 62 |
+
full_text = ""
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# 2. Segmentleri Akış Olarak Al (Streaming)
|
| 65 |
+
# Bu döngü her cümle bulunduğunda çalışır ve ekranı günceller.
|
| 66 |
+
for segment in segments:
|
| 67 |
+
full_text += segment.text + " "
|
| 68 |
+
|
| 69 |
+
# İlerleme çubuğunu güncelle (Tahmini)
|
| 70 |
+
current_time = segment.end
|
| 71 |
+
if duration > 0:
|
| 72 |
+
prog_val = min(current_time / duration, 0.99)
|
| 73 |
+
progress(prog_val, desc=f"Çevriliyor... ({int(current_time)}/{int(duration)} sn)")
|
| 74 |
+
|
| 75 |
+
# Kısmi sonucu ekrana yansıt (Dosya henüz yok)
|
| 76 |
+
yield full_text.strip(), None
|
| 77 |
+
|
| 78 |
elapsed = time.time() - start_time
|
| 79 |
+
final_result = full_text.strip()
|
| 80 |
|
| 81 |
+
if not final_result:
|
| 82 |
+
yield "⚠️ Ses dosyasında konuşma algılanamadı.", None
|
| 83 |
+
return
|
| 84 |
+
|
| 85 |
+
# 3. Dosyayı Oluştur (İşlem bitince)
|
| 86 |
+
progress(0.99, desc="Dosya kaydediliyor...")
|
| 87 |
txt_file = tempfile.NamedTemporaryFile(
|
| 88 |
mode='w',
|
| 89 |
suffix='.txt',
|
| 90 |
delete=False,
|
| 91 |
encoding='utf-8'
|
| 92 |
)
|
| 93 |
+
txt_file.write(final_result)
|
| 94 |
txt_file.close()
|
| 95 |
|
| 96 |
+
# İstatistikleri ekle
|
| 97 |
+
stats = f"\n\n───────────────────────────────────\n📊 İstatistikler\n• Ses süresi: {duration:.1f} sn\n• İşlem süresi: {elapsed:.1f} sn\n• Hız: {duration/elapsed:.1f}x\n───────────────────────────────────"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# Final çıktıyı ve dosyayı ver
|
| 100 |
+
yield final_result + stats, txt_file.name
|
| 101 |
+
|
| 102 |
except Exception as e:
|
| 103 |
+
yield f"❌ Bir hata oluştu: {str(e)}", None
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
|
| 106 |
+
# Build Interface
|
| 107 |
with gr.Blocks(title="Ses Deşifre Pro") as demo:
|
| 108 |
|
| 109 |
+
# Header
|
| 110 |
gr.HTML("""
|
| 111 |
<style>
|
| 112 |
footer { display: none !important; }
|
|
|
|
| 116 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 117 |
border-radius: 20px; margin-bottom: 24px; color: white;">
|
| 118 |
<h1 style="font-size: 2.5rem; font-weight: 700; margin: 0 0 8px 0;">
|
| 119 |
+
🎙️ Voice to Text (Canlı Akış)
|
| 120 |
</h1>
|
| 121 |
<p style="font-size: 1.1rem; opacity: 0.95; margin: 0;">
|
| 122 |
Yapay zeka ile Türkçe ses kayıtlarını metne dönüştürün
|
|
|
|
| 141 |
size="lg"
|
| 142 |
)
|
| 143 |
|
|
|
|
| 144 |
gr.HTML("""
|
| 145 |
<div style="background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
|
| 146 |
border: 1px solid #86efac; border-radius: 12px;
|
| 147 |
padding: 16px 20px; margin-top: 16px;">
|
| 148 |
<p style="margin: 0; color: #166534; font-size: 14px;">
|
| 149 |
+
💡 <strong>İpucu:</strong> Sonuçlar artık anlık olarak ekrana düşecektir.
|
| 150 |
+
İşlem hızı dosya boyutuna göre değişebilir.
|
| 151 |
</p>
|
| 152 |
</div>
|
| 153 |
""")
|
| 154 |
+
|
| 155 |
# Results section
|
| 156 |
with gr.Row():
|
| 157 |
with gr.Column():
|
|
|
|
| 159 |
|
| 160 |
output_text = gr.Textbox(
|
| 161 |
label="",
|
| 162 |
+
placeholder="Sonuçlar konuşma algılandıkça buraya akacak...",
|
| 163 |
lines=15,
|
| 164 |
interactive=False
|
| 165 |
)
|
|
|
|
| 167 |
download_file = gr.File(
|
| 168 |
label="📥 Sonucu İndir (.txt)"
|
| 169 |
)
|
| 170 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
# Footer
|
| 172 |
gr.HTML("""
|
| 173 |
<div style="text-align: center; padding: 24px 0; color: #9ca3af; font-size: 13px;">
|
| 174 |
+
<p>Powered by Faster-Whisper AI • CPU Optimized</p>
|
| 175 |
</div>
|
| 176 |
""")
|
| 177 |
+
|
| 178 |
# Event handling
|
| 179 |
submit_btn.click(
|
| 180 |
fn=transcribe,
|
|
|
|
| 184 |
|
| 185 |
# Launch
|
| 186 |
if __name__ == "__main__":
|
| 187 |
+
demo.launch(share=False, show_error=True)
|