Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,51 @@
|
|
1 |
-
from youtubesearchpython import VideosSearch
|
2 |
-
import gradio as gr
|
3 |
import openai
|
4 |
-
from langchain_community.document_loaders import YoutubeLoader
|
5 |
import os
|
6 |
|
7 |
-
# OpenAI API ν€ μ€μ
|
8 |
openai.api_key = os.getenv('O_API_KEY')
|
9 |
|
10 |
-
def search_youtube_videos(keyword
|
11 |
-
videos_search = VideosSearch(keyword, limit=
|
12 |
results = videos_search.result()
|
13 |
video_urls = [video['link'] for video in results['result']]
|
14 |
return video_urls
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
)
|
28 |
|
29 |
-
# Gradio
|
30 |
-
|
31 |
-
interface.launch()
|
32 |
-
|
33 |
-
# def get_transcript(url):
|
34 |
-
# loader = YoutubeLoader()
|
35 |
-
# transcript = loader.load(url)
|
36 |
-
# text = " ".join([segment['text'] for segment in transcript['segments']])
|
37 |
-
# return text
|
38 |
-
|
39 |
-
# def summarize_text(text):
|
40 |
-
# response = openai.Completion.create(
|
41 |
-
# engine="text-davinci-003",
|
42 |
-
# prompt=f"μμ½: {text}",
|
43 |
-
# max_tokens=150
|
44 |
-
# )
|
45 |
-
# return response.choices[0].text.strip()
|
46 |
-
|
47 |
-
# def process_keyword(keyword):
|
48 |
-
# video_urls = search_youtube_videos(keyword)
|
49 |
-
# summaries = []
|
50 |
-
# for url in video_urls:
|
51 |
-
# try:
|
52 |
-
# text = get_transcript(url)
|
53 |
-
# summary = summarize_text(text)
|
54 |
-
# summaries.append(f"URL: {url}\nSummary: {summary}\n")
|
55 |
-
# except Exception as e:
|
56 |
-
# summaries.append(f"URL: {url}\nError: {str(e)}\n")
|
57 |
-
# return "\n".join(summaries)
|
58 |
-
|
59 |
-
# # Gradio μΈν°νμ΄μ€
|
60 |
-
# interface = gr.Interface(
|
61 |
-
# fn=process_keyword,
|
62 |
-
# inputs=gr.Textbox(label="κ²μ ν€μλ"),
|
63 |
-
# outputs=gr.Textbox(label="κ²°κ³Ό"),
|
64 |
-
# )
|
65 |
-
|
66 |
-
# interface.launch()
|
67 |
-
|
68 |
-
|
69 |
-
# from youtubesearchpython import VideosSearch
|
70 |
-
# from langchain_community.document_loaders import YoutubeLoader
|
71 |
-
# import gradio as gr
|
72 |
-
|
73 |
-
# def search_youtube_videos(keyword, limit=5, order='date'):
|
74 |
-
# videos_search = VideosSearch(keyword, limit=limit, order='date')
|
75 |
-
# results = videos_search.result()
|
76 |
-
|
77 |
-
# video_urls = [video['link'] for video in results['result']]
|
78 |
-
# return video_urls
|
79 |
-
|
80 |
-
# def gradio_interface(keyword):
|
81 |
-
# video_urls = search_youtube_videos(keyword)
|
82 |
-
# return "\n".join(video_urls)
|
83 |
-
|
84 |
-
# interface = gr.Interface(
|
85 |
-
# fn=gradio_interface,
|
86 |
-
# inputs=gr.Textbox(label="κ²μ ν€μλλ₯Ό μ
λ ₯νμΈμ"),
|
87 |
-
# outputs=gr.Textbox(label="κ²μλ μ νλΈ λμμ URL"),
|
88 |
-
# title="μ νλΈ κ²μ λμ°λ―Έ",
|
89 |
-
# description="ν€μλλ₯Ό μ
λ ₯νλ©΄ μ νλΈμμ ν΄λΉ ν€μλλ‘ κ²μν ν λμμ URLμ 보μ¬μ€λλ€."
|
90 |
-
# )
|
91 |
-
|
92 |
-
# if __name__ == "__main__":
|
93 |
-
# interface.launch()
|
|
|
1 |
+
from youtubesearchpython import VideosSearch, Transcript # Changed import statement
|
2 |
+
import gradio as gr # Added import statement for Gradio
|
3 |
import openai
|
|
|
4 |
import os
|
5 |
|
|
|
6 |
openai.api_key = os.getenv('O_API_KEY')
|
7 |
|
8 |
+
def search_youtube_videos(keyword):
|
9 |
+
videos_search = VideosSearch(keyword, limit=5)
|
10 |
results = videos_search.result()
|
11 |
video_urls = [video['link'] for video in results['result']]
|
12 |
return video_urls
|
13 |
|
14 |
+
# Function to get the transcript for a given list of video URLs
|
15 |
+
def get_transcript(urls):
|
16 |
+
contents = ''
|
17 |
+
for url in urls:
|
18 |
+
data = Transcript.get(url)
|
19 |
+
text = ""
|
20 |
+
for segment in data['transcript']:
|
21 |
+
text += segment['text']
|
22 |
+
contents += text
|
23 |
+
return contents
|
24 |
+
|
25 |
+
# Function to summarize text using OpenAI's API
|
26 |
+
def summarize_text(contents):
|
27 |
+
response = openai.Completion.create(
|
28 |
+
engine="text-davinci-003",
|
29 |
+
prompt=f"μμ½: {contents}",
|
30 |
+
max_tokens=150
|
31 |
+
)
|
32 |
+
return response.choices[0].text.strip()
|
33 |
+
|
34 |
+
# Function to integrate all the functionalities
|
35 |
+
def summarize_youtube_videos(keyword):
|
36 |
+
urls = search_youtube_videos(keyword)
|
37 |
+
contents = get_transcript(urls)
|
38 |
+
summary = summarize_text(contents)
|
39 |
+
return summary
|
40 |
+
|
41 |
+
# Define Gradio interface
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=summarize_youtube_videos,
|
44 |
+
inputs="text", # Changed input to "text" (user input)
|
45 |
+
outputs="text",
|
46 |
+
title="Summarize YouTube Videos",
|
47 |
+
description="Enter a keyword to summarize related YouTube videos.",
|
48 |
)
|
49 |
|
50 |
+
# Launch Gradio interface
|
51 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|