Senasu's picture
Update app.py
433ceb3 verified
import gradio as gr
from google import genai
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api._errors import NoTranscriptFound, VideoUnavailable, TranscriptsDisabled
def get_transcript(video_url, lang_sel, model_sel, api_key_input):
client = genai.Client(api_key=api_key_input)
try:
video_id = video_url.split("v=")[1].split("&")[0]
print(f"Extracted Video ID: {video_id}") # Debugging
transcript = YouTubeTranscriptApi.get_transcript(video_id)
raw_text = " ".join([entry["text"] for entry in transcript])
print(f"Retrieved Transcript: {raw_text[:500]}") # Print first 500 characters
if lang_sel != "English":
prompt = f"Translate this text to {lang_sel} language: {raw_text}"
response = client.models.generate_content(
model=model_sel,
contents=[prompt]
)
return response.text
return raw_text
except (NoTranscriptFound, VideoUnavailable, TranscriptsDisabled) as e:
print(f"Transcript Retrieval Error: {e}") # Debugging
return f"⚠️ Error: {e}"
def process_transcript(video_url, word_count, model_sel, lang_sel, api_key_input):
client = genai.Client(api_key=api_key_input)
transcript = get_transcript(video_url, lang_sel, model_sel, api_key_input)
prompt = f"Summarize this text in {lang_sel} language using {word_count} words: {transcript}"
response = client.models.generate_content(
model=model_sel,
contents=[prompt]
)
return response.text
with gr.Blocks(theme=gr.themes.Default()) as demo:
gr.Markdown("""
# 🎬 YouTube Video Transcription & Summarization @SenasuDemir
**Paste a YouTube link, select options, and generate transcriptions or summaries easily!**
""")
with gr.Row():
api_key = gr.Textbox(
placeholder="Enter your Gemini API Key",
label="πŸ”‘ API Key",
type="password",
interactive=True
)
gr.Markdown("### 1️⃣ Enter Video URL")
video_url = gr.Textbox(
placeholder="Paste YouTube video link here...",
label="πŸ“Ί YouTube Video URL",
interactive=True
)
gr.Markdown("### 2️⃣ Select Language & Model")
with gr.Row():
language_selection = gr.Radio(
choices=["Turkish", "English", "Italian", "German"],
value="English",
label="🌍 Select Translation Language"
)
model_selection = gr.Dropdown(
choices=["gemini-2.0-flash", "gemini-2.0-flash-lite", "gemini-1.5-flash", "gemini-1.5-pro"],
value="gemini-2.0-flash",
label="πŸ€– Choose AI Model"
)
word_count = gr.Slider(
50, 500, step=10, value=80,
label="πŸ”’ Summary Word Count",
info="Choose a length between 50 and 500 words"
)
gr.Markdown("### 3️⃣ Get Transcript & Summarization")
with gr.Row():
trs_btn = gr.Button("πŸ“œ Get Transcript", variant="primary")
sum_btn = gr.Button("✍️ Summarize", variant="secondary")
with gr.Row():
transkript_text = gr.Textbox(label="🎀 Video Transcription", lines=5, interactive=False)
sum_text = gr.Textbox(label="πŸ“ Summarized Text", lines=3, interactive=False)
trs_btn.click(
fn=get_transcript,
inputs=[video_url, language_selection, model_selection, api_key],
outputs=transkript_text
)
sum_btn.click(
fn=process_transcript,
inputs=[video_url, word_count, model_selection, language_selection, api_key],
outputs=sum_text
)
if __name__ == "__main__":
demo.launch()