|
|
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}") |
|
|
|
|
|
transcript = YouTubeTranscriptApi.get_transcript(video_id) |
|
|
raw_text = " ".join([entry["text"] for entry in transcript]) |
|
|
print(f"Retrieved Transcript: {raw_text[:500]}") |
|
|
|
|
|
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}") |
|
|
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() |