Jedi09's picture
Upload 3 files
9c8785c verified
raw
history blame
12.7 kB
"""
Ses Deşifre Pro - Türkçe Ses-Metin Dönüştürme
Hugging Face Spaces için profesyonel arayüz.
Gradio 6.x uyumlu + Gemini AI Özet Desteği.
"""
import gradio as gr
from faster_whisper import WhisperModel
import tempfile
import time
import requests
# ==================== CONFIGURATION ====================
MODEL_SIZE = "medium" # Options: tiny, base, small, medium, large-v3
# =======================================================
# Load model once at startup
print("🔄 Model yükleniyor... (Bu işlem birkaç dakika sürebilir)")
model = WhisperModel(
MODEL_SIZE,
device="cpu",
compute_type="int8"
)
print("✅ Model yüklendi!")
def summarize_with_gemini(text: str, api_key: str, custom_prompt: str = "") -> str:
"""
Summarize text using Google Gemini API.
API key is not stored - used only for this single request.
"""
if not api_key or not api_key.strip():
return "⚠️ Gemini API anahtarı girilmedi."
if not text or text.strip() == "" or text.startswith("⚠️") or text.startswith("❌"):
return "⚠️ Önce bir transkripsiyon oluşturun."
# Extract only the transcription text (remove stats section)
clean_text = text.split("───────────────────────────────────")[0].strip()
if not clean_text:
return "⚠️ Özetlenecek metin bulunamadı."
# Build the prompt
base_instruction = "Aşağıdaki Türkçe metni analiz et ve özetle."
if custom_prompt and custom_prompt.strip():
full_prompt = f"{custom_prompt.strip()}\n\nMetin:\n{clean_text}"
else:
full_prompt = f"{base_instruction}\n\nMetin:\n{clean_text}"
# Gemini API endpoint
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={api_key}"
headers = {"Content-Type": "application/json"}
payload = {
"contents": [{"parts": [{"text": full_prompt}]}],
"generationConfig": {
"temperature": 0.7,
"maxOutputTokens": 2048
}
}
try:
response = requests.post(url, headers=headers, json=payload, timeout=60)
if response.status_code == 200:
result = response.json()
if "candidates" in result and len(result["candidates"]) > 0:
candidate = result["candidates"][0]
if "content" in candidate and "parts" in candidate["content"]:
parts = candidate["content"]["parts"]
if len(parts) > 0 and "text" in parts[0]:
return parts[0]["text"]
return "❌ Gemini yanıtı beklenmedik formatta."
elif response.status_code == 400:
return "❌ Geçersiz istek. API anahtarını kontrol edin."
elif response.status_code in [401, 403]:
return "❌ API anahtarı geçersiz veya yetkisiz."
elif response.status_code == 429:
return "❌ API kullanım limiti aşıldı. Lütfen bekleyin."
else:
return f"❌ Gemini API hatası: {response.status_code}"
except requests.exceptions.Timeout:
return "❌ Gemini API zaman aşımı. Tekrar deneyin."
except requests.exceptions.RequestException as e:
return f"❌ Bağlantı hatası: {str(e)}"
except Exception as e:
return f"❌ Beklenmeyen hata: {str(e)}"
def transcribe(audio_path: str):
"""
Transcribe audio file to Turkish text.
Returns: (transcription_text, download_file_path)
"""
if audio_path is None:
return "⚠️ Lütfen bir ses dosyası yükleyin veya mikrofonla kayıt yapın.", None
try:
start_time = time.time()
# Transcribe with Turkish language
segments, info = model.transcribe(
audio_path,
language="tr",
beam_size=5
)
# Collect all segments
full_text = []
for segment in segments:
full_text.append(segment.text)
result = " ".join(full_text).strip()
elapsed = time.time() - start_time
if not result:
return "⚠️ Ses dosyasında konuşma algılanamadı. Lütfen daha net bir kayıt deneyin.", None
# Create downloadable TXT file
txt_file = tempfile.NamedTemporaryFile(
mode='w',
suffix='.txt',
delete=False,
encoding='utf-8'
)
txt_file.write(result)
txt_file.close()
# Format display text with stats
display_text = f"""{result}
───────────────────────────────────
📊 İstatistikler
• Algılanan dil: Türkçe
• Ses süresi: {info.duration:.1f} saniye
• İşlem süresi: {elapsed:.1f} saniye
• Hız: {info.duration/elapsed:.1f}x gerçek zamanlı
───────────────────────────────────"""
return display_text, txt_file.name
except Exception as e:
error_msg = str(e)
if "ffmpeg" in error_msg.lower():
return "❌ Ses dosyası işlenemedi. Desteklenen formatlar: MP3, WAV, M4A, OGG, FLAC", None
return f"❌ Bir hata oluştu: {error_msg}", None
# Build Interface (Gradio 6.x compatible)
with gr.Blocks(title="Ses Deşifre Pro") as demo:
# Header via HTML
gr.HTML("""
<style>
footer { display: none !important; }
.gradio-container { max-width: 900px !important; margin: auto !important; }
</style>
<div style="text-align: center; padding: 40px 20px 30px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 20px; margin-bottom: 24px; color: white;">
<h1 style="font-size: 2.5rem; font-weight: 700; margin: 0 0 8px 0;">
🎙️ Ses Deşifre Pro
</h1>
<p style="font-size: 1.1rem; opacity: 0.95; margin: 0;">
Yapay zeka ile Türkçe ses kayıtlarını metne dönüştürün
</p>
</div>
""")
# Main content
with gr.Row():
with gr.Column():
gr.HTML('<div style="font-weight: 600; margin-bottom: 12px;">📤 Ses Kaynağı</div>')
audio_input = gr.Audio(
label="Ses Dosyası veya Mikrofon",
type="filepath",
sources=["upload", "microphone"]
)
submit_btn = gr.Button(
"✨ Transkripsiyon Başlat",
variant="primary",
size="lg"
)
# Tips
gr.HTML("""
<div style="background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
border: 1px solid #86efac; border-radius: 12px;
padding: 16px 20px; margin-top: 16px;">
<p style="margin: 0; color: #166534; font-size: 14px;">
💡 <strong>İpucu:</strong> En iyi sonuç için net, gürültüsüz ses kayıtları kullanın.
Desteklenen formatlar: MP3, WAV, M4A, OGG, FLAC
</p>
</div>
""")
# Results section
with gr.Row():
with gr.Column():
gr.HTML('<div style="font-weight: 600; margin-bottom: 12px;">📝 Transkripsiyon Sonucu</div>')
output_text = gr.Textbox(
label="",
placeholder="Sonuç burada görünecek...",
lines=12,
interactive=False
)
download_file = gr.File(
label="📥 Sonucu İndir (.txt)"
)
# ==================== GEMINI AI SECTION ====================
gr.HTML("""
<div style="margin-top: 32px; padding: 24px;
background: linear-gradient(135deg, #1e1b4b 0%, #312e81 100%);
border-radius: 16px; border: 1px solid #4338ca;">
<div style="display: flex; align-items: center; gap: 12px; margin-bottom: 16px;">
<span style="font-size: 28px;">✨</span>
<div>
<h2 style="color: white; margin: 0; font-size: 1.3rem;">Gemini AI ile Özet</h2>
<p style="color: #a5b4fc; margin: 4px 0 0 0; font-size: 0.85rem;">
Opsiyonel • API anahtarınız saklanmaz
</p>
</div>
</div>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
gemini_api_key = gr.Textbox(
label="🔑 Gemini API Anahtarı",
placeholder="AIza... şeklinde API anahtarınızı girin",
type="password",
info="Güvenli: Anahtarınız sunucuda saklanmaz, yalnızca bu istek için kullanılır."
)
gemini_prompt = gr.Textbox(
label="📝 Gemini'ye Not (Opsiyonel)",
placeholder="Örn: Sınavım için özet yap, En önemli 5 maddeyi listele, Bu metnin raporunu çıkar...",
lines=2,
info="Boş bırakırsanız genel bir özet oluşturulur."
)
gemini_btn = gr.Button(
"🤖 Gemini ile Özetle",
variant="secondary",
size="lg"
)
with gr.Column(scale=1):
gemini_output = gr.Textbox(
label="Gemini Özet Sonucu",
placeholder="Özet burada görünecek...",
lines=10,
show_copy_button=True
)
gr.HTML("""
<div style="background: #fef3c7; border: 1px solid #f59e0b; border-radius: 8px;
padding: 12px 16px; margin-top: 16px;">
<p style="margin: 0; color: #92400e; font-size: 13px;">
🔒 <strong>Gizlilik:</strong> API anahtarınız sunucuda saklanmaz.
"Gemini ile Özetle" butonuna basmadığınız sürece hiçbir veri dışarıya gönderilmez.
<a href="https://aistudio.google.com/apikey" target="_blank"
style="color: #1d4ed8; text-decoration: underline;">
Ücretsiz API anahtarı alın →
</a>
</p>
</div>
""")
# Features
gr.HTML("""
<div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-top: 32px;">
<div style="text-align: center; padding: 20px; background: #f9fafb; border-radius: 12px;">
<div style="font-size: 28px; margin-bottom: 8px;">🚀</div>
<div style="font-size: 13px; color: #6b7280; font-weight: 500;">Hızlı İşlem</div>
</div>
<div style="text-align: center; padding: 20px; background: #f9fafb; border-radius: 12px;">
<div style="font-size: 28px; margin-bottom: 8px;">🎯</div>
<div style="font-size: 13px; color: #6b7280; font-weight: 500;">Yüksek Doğruluk</div>
</div>
<div style="text-align: center; padding: 20px; background: #f9fafb; border-radius: 12px;">
<div style="font-size: 28px; margin-bottom: 8px;">🔒</div>
<div style="font-size: 13px; color: #6b7280; font-weight: 500;">Gizlilik Odaklı</div>
</div>
<div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #ede9fe 0%, #ddd6fe 100%); border-radius: 12px;">
<div style="font-size: 28px; margin-bottom: 8px;">✨</div>
<div style="font-size: 13px; color: #5b21b6; font-weight: 500;">AI Özet</div>
</div>
</div>
""")
# Footer
gr.HTML("""
<div style="text-align: center; padding: 24px 0; color: #9ca3af; font-size: 13px;">
<p>Powered by Faster-Whisper & Gemini AI • CPU Optimized • Made with ❤️</p>
</div>
""")
# Event handling
submit_btn.click(
fn=transcribe,
inputs=[audio_input],
outputs=[output_text, download_file]
)
gemini_btn.click(
fn=summarize_with_gemini,
inputs=[output_text, gemini_api_key, gemini_prompt],
outputs=gemini_output
)
# Launch
if __name__ == "__main__":
demo.launch(share=False, show_error=True)