Spaces:
Running
Running
import gradio as gr | |
from google import genai | |
import requests | |
import json | |
import re | |
import os | |
from urllib.parse import urlparse, parse_qs | |
# Video ID çıkarma fonksiyonu | |
def extract_video_id(url): | |
# YouTube URL formatları için regex | |
youtube_regex = r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})' | |
match = re.match(youtube_regex, url) | |
if match: | |
return match.group(6) | |
# URL parametrelerinden ID çıkarma | |
parsed_url = urlparse(url) | |
if parsed_url.netloc in ['youtube.com', 'www.youtube.com']: | |
query_params = parse_qs(parsed_url.query) | |
if 'v' in query_params: | |
return query_params['v'][0] | |
# youtu.be URL'leri için | |
if parsed_url.netloc == 'youtu.be': | |
return parsed_url.path.strip('/') | |
return None | |
# RapidAPI üzerinden transkript alma fonksiyonu | |
def get_transcript_via_rapidapi(video_url): | |
try: | |
# Video ID'yi çıkar | |
video_id = extract_video_id(video_url) | |
if not video_id: | |
return "Geçersiz YouTube URL'si. Lütfen geçerli bir YouTube video URL'si girin." | |
# Hugging Face'de ayarladığınız secret değeri al | |
RAPID_API_KEY = os.environ.get("RAPID_API_KEY", "") | |
if not RAPID_API_KEY: | |
return "RapidAPI anahtarı bulunamadı. Lütfen Hugging Face Space ayarlarında RAPID_API_KEY adlı bir secret oluşturun." | |
# ----- RapidAPI Endpoint Seçeneği 1: YouTube Transcriptor ----- | |
# API endpoint ve parametreleri | |
url = "https://youtube-transcriptor.p.rapidapi.com/transcript" | |
querystring = {"video_id": video_id, "lang": "tr"} | |
headers = { | |
"X-RapidAPI-Key": RAPID_API_KEY, | |
"X-RapidAPI-Host": "youtube-transcriptor.p.rapidapi.com" | |
} | |
# API isteği gönder | |
response = requests.get(url, headers=headers, params=querystring) | |
# ----- VEYA ----- | |
# ----- RapidAPI Endpoint Seçeneği 2: Ultimate YouTube API ----- | |
# url = "https://ultimate-youtube-api.p.rapidapi.com/video/transcript" | |
# querystring = {"id": video_id} | |
# headers = { | |
# "X-RapidAPI-Key": RAPID_API_KEY, | |
# "X-RapidAPI-Host": "ultimate-youtube-api.p.rapidapi.com" | |
# } | |
# response = requests.get(url, headers=headers, params=querystring) | |
# API yanıtını kontrol et | |
if response.status_code == 200: | |
try: | |
data = response.json() | |
# API'nin yanıt formatına göre ayarla - YouTube Transcriptor API için: | |
if data and 'transcript' in data: | |
transcript_parts = [] | |
for item in data['transcript']: | |
if 'text' in item: | |
transcript_parts.append(item['text']) | |
return " ".join(transcript_parts) | |
# Ultimate YouTube API formatı için: | |
# if data and isinstance(data, list): | |
# transcript_parts = [] | |
# for item in data: | |
# if 'text' in item: | |
# transcript_parts.append(item['text']) | |
# return " ".join(transcript_parts) | |
return "Transkript verileri beklenen formatta değil. Manuel olarak girmeyi deneyin." | |
except Exception as e: | |
return f"JSON verileri işlenirken hata: {str(e)}. API yanıtı: {response.text[:100]}..." | |
else: | |
return f"API yanıt vermedi (Hata kodu: {response.status_code}). Yanıt: {response.text[:100]}... Lütfen transkripti manuel olarak girin." | |
except Exception as e: | |
return f"Transkript alınırken hata oluştu: {str(e)}. Lütfen transkripti manuel olarak girin." | |
# Gemini API ile metni özetleme | |
def fn_sum_text(transkript_text, word_count, model_sel, lang_sel, action_sel): | |
try: | |
# Hugging Face'de ayarladığınız secret değeri al | |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "") | |
if not GEMINI_API_KEY: | |
return "Gemini API anahtarı bulunamadı. Lütfen Hugging Face Space ayarlarında GEMINI_API_KEY adlı bir secret oluşturun." | |
client = genai.Client(api_key=GEMINI_API_KEY) | |
prompt = f"{transkript_text} metni {word_count} sayıda kelimeyle {lang_sel} dilinde {action_sel}" | |
response = client.models.generate_content( | |
model=model_sel, | |
contents=[prompt] | |
) | |
return response.text | |
except Exception as e: | |
return f"Özet oluşturulurken hata: {str(e)}" | |
# Her ikisini birleştiren fonksiyon | |
def process_video(video_url, word_count, model_sel, lang_sel, action_sel): | |
# Önce transkripti al | |
transcript = get_transcript_via_rapidapi(video_url) | |
# Hata kontrolü | |
if transcript.startswith("Hata") or transcript.startswith("Geçersiz") or transcript.startswith("API"): | |
return transcript, "" | |
# Transkript başarıyla alındıysa, özetle | |
summary = fn_sum_text(transcript, word_count, model_sel, lang_sel, action_sel) | |
return transcript, summary | |
# Gradio arayüzü | |
with gr.Blocks(title="YouTube Transcript Özetleyici") as demo: | |
gr.Markdown(""" | |
# YouTube Transcript Özetleyici | |
## Nasıl Kullanılır: | |
1. YouTube video URL'sini girin | |
2. Özet ayarlarını yapın | |
3. "Transkript Al ve Özetle" butonuna tıklayın | |
Not: Bu uygulama RapidAPI ve Gemini API'yi kullanır. API anahtarları Hugging Face Space'in secret bölümünde saklanmalıdır. | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
video_url = gr.Textbox( | |
placeholder="https://www.youtube.com/watch?v=...", | |
label="YouTube Video URL'si" | |
) | |
with gr.Accordion("Özet Ayarları", open=True): | |
word_count = gr.Slider( | |
minimum=50, | |
maximum=1000, | |
value=200, | |
step=10, | |
label="Özet Kelime Sayısı" | |
) | |
model_sel = gr.Dropdown( | |
choices=[ | |
'gemini-2.0-flash', | |
'gemini-2.0-flash-lite', | |
'gemini-1.5-pro' | |
], | |
value='gemini-2.0-flash', | |
label="Model Seçimi" | |
) | |
lang_sel = gr.Dropdown( | |
choices=[ | |
'Türkçe', | |
'İngilizce', | |
'Almanca', | |
'Fransızca', | |
'İspanyolca' | |
], | |
value='Türkçe', | |
label="Dil Seçimi" | |
) | |
action_sel = gr.Dropdown( | |
choices=[ | |
'özetle', | |
'tam çeviri yap', | |
'madde madde özetle', | |
'ana başlıkları çıkar' | |
], | |
value='özetle', | |
label="İşlem Türü" | |
) | |
auto_process_btn = gr.Button("Transkript Al ve Özetle", variant="primary") | |
with gr.Accordion("Manuel İşlemler", open=False): | |
get_transcript_btn = gr.Button("Sadece Transkripti Al") | |
summarize_btn = gr.Button("Sadece Özetle") | |
with gr.Column(): | |
transcript_text = gr.Textbox( | |
label="Video Transkripti", | |
placeholder="Transkript burada görünecek veya manuel olarak girebilirsiniz...", | |
lines=10 | |
) | |
summary_text = gr.Textbox( | |
label="Özet Sonucu", | |
lines=10 | |
) | |
# Buton işlevleri | |
auto_process_btn.click( | |
fn=process_video, | |
inputs=[video_url, word_count, model_sel, lang_sel, action_sel], | |
outputs=[transcript_text, summary_text] | |
) | |
get_transcript_btn.click( | |
fn=get_transcript_via_rapidapi, | |
inputs=[video_url], | |
outputs=[transcript_text] | |
) | |
summarize_btn.click( | |
fn=fn_sum_text, | |
inputs=[transcript_text, word_count, model_sel, lang_sel, action_sel], | |
outputs=[summary_text] | |
) | |
with gr.Accordion("Sorun Giderme", open=False): | |
gr.Markdown(""" | |
### Sık Karşılaşılan Sorunlar | |
**API Anahtarı Hatası Alıyorum** | |
- Hugging Face Space'inizin ayarlarında "Repository Secrets" bölümünde "RAPID_API_KEY" ve "GEMINI_API_KEY" adlı iki secret oluşturduğunuzdan emin olun. | |
**"Transkript Bulunamadı" Hatası** | |
- Videonun altyazıları bulunmuyor olabilir | |
- Seçilen RapidAPI servisi bu video için altyazı sunmuyor olabilir | |
- Farklı bir RapidAPI endpoint'i deneyebilirsiniz (kod içinde yorum satırlarındaki alternatif endpoint'i kullanın) | |
**Diğer Sorunlar** | |
- Hugging Face Space'in loglarını kontrol edin | |
- RapidAPI kullanım limitinizi aşmış olabilirsiniz | |
""") | |
# Uygulamayı başlat | |
if __name__ == '__main__': | |
demo.launch() |