lgsantini1 commited on
Commit
cb7453d
·
verified ·
1 Parent(s): ab786b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -1,28 +1,44 @@
1
  import gradio as gr
2
  from langchain_community.document_loaders import YoutubeLoader
3
-
4
- from langchain_core.prompts import ChatPromptTemplate
5
  from youtube_transcript_api import NoTranscriptFound
6
  from pytube.exceptions import PytubeError
7
 
8
  def load_youtube_video_info(url):
9
- # Initialize YouTube loader with preferred language as Portuguese
10
-
11
-
 
 
 
 
 
 
12
  try:
 
13
  loader = YoutubeLoader.from_youtube_url(url, add_video_info=True, language="pt")
14
-
15
  # Load the video information and transcript
16
  result = loader.load()
17
 
18
- if result:
19
  # Retrieve metadata information
20
  author = result[0].metadata.get('author', 'Unknown Author')
21
  length = result[0].metadata.get('length', 'Unknown Length')
 
 
22
  # Check if transcript is available
23
  transcript = result[0].page_content if hasattr(result[0], 'page_content') else "No transcript available."
24
 
 
 
 
 
 
 
 
 
25
  info = "No result found."
 
26
  except NoTranscriptFound:
27
  info = "Transcript not found for this video."
28
  except PytubeError as e:
@@ -30,4 +46,4 @@ def load_youtube_video_info(url):
30
  except Exception as e:
31
  info = f"An unexpected error occurred: {str(e)}"
32
 
33
- return info
 
1
  import gradio as gr
2
  from langchain_community.document_loaders import YoutubeLoader
 
 
3
  from youtube_transcript_api import NoTranscriptFound
4
  from pytube.exceptions import PytubeError
5
 
6
  def load_youtube_video_info(url):
7
+ """
8
+ Load YouTube video information including metadata and transcript.
9
+
10
+ Args:
11
+ url (str): URL of the YouTube video.
12
+
13
+ Returns:
14
+ str: Information about the video, including metadata and transcript or error messages.
15
+ """
16
  try:
17
+ # Initialize YouTube loader with preferred language as Portuguese
18
  loader = YoutubeLoader.from_youtube_url(url, add_video_info=True, language="pt")
19
+
20
  # Load the video information and transcript
21
  result = loader.load()
22
 
23
+ if result and len(result) > 0:
24
  # Retrieve metadata information
25
  author = result[0].metadata.get('author', 'Unknown Author')
26
  length = result[0].metadata.get('length', 'Unknown Length')
27
+ title = result[0].metadata.get('title', 'Unknown Title')
28
+
29
  # Check if transcript is available
30
  transcript = result[0].page_content if hasattr(result[0], 'page_content') else "No transcript available."
31
 
32
+ # Format information
33
+ info = (
34
+ f"Video Title: {title}\n"
35
+ f"Author: {author}\n"
36
+ f"Length: {length}\n\n"
37
+ f"Transcript:\n{transcript}"
38
+ )
39
+ else:
40
  info = "No result found."
41
+
42
  except NoTranscriptFound:
43
  info = "Transcript not found for this video."
44
  except PytubeError as e:
 
46
  except Exception as e:
47
  info = f"An unexpected error occurred: {str(e)}"
48
 
49
+ return info