|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
import re |
|
from youtube_transcript_api import YouTubeTranscriptApi |
|
from youtube_transcript_api.formatters import TextFormatter |
|
import torch |
|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16) |
|
|
|
|
|
|
|
|
|
|
|
|
|
def summary (input): |
|
output = text_summary(input) |
|
return output[0]['summary_text'] |
|
|
|
def extract_video_id(url): |
|
|
|
regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})" |
|
match = re.search(regex, url) |
|
if match: |
|
return match.group(1) |
|
return None |
|
|
|
|
|
def get_youtube_transcript(video_url): |
|
video_id = extract_video_id(video_url) |
|
if not video_id: |
|
return "Video ID could not be extracted." |
|
|
|
try: |
|
|
|
transcript = YouTubeTranscriptApi.get_transcript(video_id) |
|
|
|
|
|
formatter = TextFormatter() |
|
text_transcript = formatter.format_transcript(transcript) |
|
summary_text = summary(text_transcript) |
|
|
|
return summary_text |
|
except Exception as e: |
|
return f"An error occurred: {e}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.close_all() |
|
|
|
demo = gr.ChatInterface( |
|
get_youtube_transcript, |
|
additional_inputs=[ |
|
gr.Textbox(value="You are a friendly Chatbot.", label="System message"), |
|
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), |
|
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), |
|
gr.Slider( |
|
minimum=0.1, |
|
maximum=1.0, |
|
value=0.95, |
|
step=0.05, |
|
label="Top-p (nucleus sampling)", |
|
), |
|
], |
|
) |
|
|
|
demo.launch() |
|
|