Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 6 |
+
from youtube_transcript_api.formatters import TextFormatter
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Load environment variables from .env file
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Get the user ID and password from environment variables
|
| 14 |
+
USER_ID = os.getenv('USER_ID')
|
| 15 |
+
PASSWORD = os.getenv('PASSWORD')
|
| 16 |
+
|
| 17 |
+
# Initialize the summarization pipeline
|
| 18 |
+
text_summary = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", torch_dtype=torch.bfloat16)
|
| 19 |
+
|
| 20 |
+
def summary(text_youtube):
|
| 21 |
+
output = text_summary(text_youtube)
|
| 22 |
+
return output[0]['summary_text']
|
| 23 |
+
|
| 24 |
+
def extract_video_id(url):
|
| 25 |
+
# Updated regex to extract the video ID from various YouTube URL formats, including /shorts/ and others with parameters
|
| 26 |
+
regex = r"(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})"
|
| 27 |
+
match = re.search(regex, url)
|
| 28 |
+
if match:
|
| 29 |
+
return match.group(1)
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
def get_youtube_transcript(video_url):
|
| 33 |
+
video_id = extract_video_id(video_url)
|
| 34 |
+
if not video_id:
|
| 35 |
+
return "Video ID could not be extracted.", ""
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
# Fetch the transcript
|
| 39 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 40 |
+
|
| 41 |
+
# Format the transcript into plain text
|
| 42 |
+
formatter = TextFormatter()
|
| 43 |
+
text_transcript = formatter.format_transcript(transcript)
|
| 44 |
+
summary_text = summary(text_transcript)
|
| 45 |
+
|
| 46 |
+
return text_transcript, summary_text
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"An error occurred: {e}", ""
|
| 49 |
+
|
| 50 |
+
# Authentication function
|
| 51 |
+
def authenticate(username, password):
|
| 52 |
+
if username == USER_ID and password == PASSWORD:
|
| 53 |
+
return True
|
| 54 |
+
return False
|
| 55 |
+
|
| 56 |
+
def secure_get_youtube_transcript(username, password, video_url):
|
| 57 |
+
if authenticate(username, password):
|
| 58 |
+
return get_youtube_transcript(video_url)
|
| 59 |
+
else:
|
| 60 |
+
return "Authentication failed", ""
|
| 61 |
+
|
| 62 |
+
# Remove any existing Gradio interfaces
|
| 63 |
+
gr.close_all()
|
| 64 |
+
|
| 65 |
+
# Define the Gradio interface
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
fn=secure_get_youtube_transcript,
|
| 68 |
+
inputs=[
|
| 69 |
+
gr.Textbox(label="Username", lines=1),
|
| 70 |
+
gr.Textbox(label="Password", lines=1, type="password"),
|
| 71 |
+
gr.Textbox(label="Input YouTube URL to summarize", lines=1)
|
| 72 |
+
],
|
| 73 |
+
outputs=[
|
| 74 |
+
gr.Textbox(label="Text extracted from video", lines=4),
|
| 75 |
+
gr.Textbox(label="Summarized text", lines=4)
|
| 76 |
+
],
|
| 77 |
+
title="YouTube Script for Text Extract & Summarizer"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Launch the Gradio interface
|
| 81 |
+
demo.launch()
|