Spaces:
Sleeping
Sleeping
import gradio as gr | |
import whisper | |
import os | |
import asyncio | |
import shutil | |
import tempfile | |
import uuid | |
import torch | |
import subprocess | |
# Whisper modeli yükleme | |
MODEL_SIZE = os.getenv("MODEL_SIZE", "base") # Daha hızlı model seçildi | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
yerel_model = whisper.load_model(MODEL_SIZE).to(device) | |
# Kısıtlamalar | |
MAX_DOSYA_BOYUTU_MB = int(os.getenv("MAX_FILE_SIZE_MB", 50)) # 50MB sınırı | |
DESTEKLENEN_FORMATLAR = {"mp3", "wav", "m4a", "ogg"} | |
def ses_dosyasini_donustur(girdi_yolu, cikti_yolu): | |
"""Ses dosyasını 16kHz mono formata çevirir.""" | |
try: | |
subprocess.run([ | |
"ffmpeg", "-i", girdi_yolu, "-ar", "16000", "-ac", "1", "-c:a", "pcm_s16le", cikti_yolu | |
], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
return cikti_yolu | |
except Exception as e: | |
print(f"Ses dönüştürme hatası: {e}") | |
return girdi_yolu | |
async def ses_cozasync(ses_dosyasi, dil): | |
return await ses_isle_ve_coz(ses_dosyasi, dil) | |
async def ses_isle_ve_coz(ses_yolu, dil): | |
if not ses_yolu or not os.path.exists(ses_yolu): | |
return "", "❌ Ses dosyası yüklenmedi." | |
dosya_boyutu_mb = os.path.getsize(ses_yolu) / (1024 * 1024) | |
if dosya_boyutu_mb > MAX_DOSYA_BOYUTU_MB: | |
return "", f"❌ Dosya boyutu sınırı {MAX_DOSYA_BOYUTU_MB} MB (Mevcut: {dosya_boyutu_mb:.2f} MB)" | |
gecici_ses_yolu = os.path.join(tempfile.gettempdir(), f"{uuid.uuid4()}.wav") | |
ses_dosyasini_donustur(ses_yolu, gecici_ses_yolu) | |
try: | |
metin = await ses_yazıya_dok(gecici_ses_yolu, dil) | |
return metin, "✅ Yazıya dökme tamamlandı." | |
except Exception as e: | |
return "", f"⚠️ Hata oluştu: {str(e)}" | |
finally: | |
if os.path.exists(gecici_ses_yolu): | |
os.remove(gecici_ses_yolu) | |
async def ses_yazıya_dok(ses_yolu, dil): | |
sonuc = await asyncio.to_thread( | |
yerel_model.transcribe, | |
ses_yolu, | |
language="tr", | |
temperature=0.0, | |
beam_size=5, | |
fp16=True if device == "cuda" else False | |
) | |
return sonuc["text"] | |
with gr.Blocks() as uygulama: | |
gr.Markdown("## 🎤 Türkçe Ses Kayıtlarını Yazıya Dökme Aracı") | |
gr.Markdown("Ses kaydı yükleyin veya mikrofon ile kaydedin. **50MB'a kadar dosyalar desteklenir.**") | |
ses_girdisi = gr.Audio(label="Ses kaydı yükleyin veya kaydedin", type="filepath") | |
dil_girdisi = gr.Radio(choices=["Türkçe"], label="Dil", value="Türkçe") | |
cevir_buton = gr.Button("Çevir") | |
durum_yazisi = gr.Textbox(label="Durum", interactive=False) | |
sonuc_metin = gr.Textbox(label="Çıktı") | |
cevir_buton.click(fn=ses_isle_ve_coz, inputs=[ses_girdisi, dil_girdisi], outputs=[sonuc_metin, durum_yazisi]) | |
uygulama.launch() | |