File size: 3,168 Bytes
51306d7 2819be2 ad28e9f 8626295 2abbd75 8626295 51306d7 2819be2 8626295 51306d7 ec4b4c0 51306d7 637c10a a427bef a37c688 c358952 7e9da05 51306d7 c358952 51306d7 2abbd75 2fa67b7 51306d7 2abbd75 51306d7 2fa67b7 2abbd75 51306d7 2fa67b7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
from youtubesearchpython import VideosSearch, Transcript # Changed import statement
import gradio as gr # Added import statement for Gradio
import openai
import os
# openai.api_key = os.getenv('O_API_KEY')
def search_youtube_videos(keyword):
videos_search = VideosSearch(keyword, limit=5)
results = videos_search.result()
video_urls = [video['link'] for video in results['result']]
return video_urls
# Function to get the transcript for a given list of video URLs
def get_transcript(urls):
contents = ''
for url in urls:
data = Transcript.get(url)
text = ""
for segment in data['segments']:
text += segment['text']
contents += text
return contents
# Function to summarize text using OpenAI's API
def summarize_text(contents, OPENAI_API_KEY):
response = openai.ChatCompletion.create(
model="gpt-4-turbo-preview", # Corrected engine identifier
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Summarize this: {contents}"}
],
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
)
return response.choices[0].message['content'].strip()
# def summarize_text(contents):
# response = openai.ChatCompletion.create(
# engine="gpt-3.5-turbo",
# prompt=f"์์ฝ: {contents}",
# max_tokens=150
# )
# return response.choices[0].text.strip()
# Function to integrate all the functionalities
def summarize_youtube_videos(keyword):
urls = search_youtube_videos(keyword)
contents = get_transcript(urls)
summary = summarize_text(contents)
return summary
# # Define Gradio interface
# iface = gr.Interface(
# input = gr.Textbox(label="keyword", placeholder='ํค์๋๋ฅผ ์
๋ ฅํ์ธ์. (์.๋นํธ์ฝ์ธ)'),
# OPENAI_API_KEY = gr.Textbox(label="OpenAI API ํค", placeholder="์ฌ๊ธฐ์ OpenAI API ํค๋ฅผ ์
๋ ฅํ์ธ์"),
# output =gr.JSON(label='์ ํ๋ธ 5๊ฐ ์์ฝ ๊ฒฐ๊ณผ'),
# result = gr.Button('submit'),
# result.click(
# fn = summarize_youtube_videos,
# inputs = [input, OPEN_API_KEY],
# outputs = [output]
# ),
# title="Summarize YouTube Videos",
# description="Enter a keyword to summarize related YouTube videos.")
# # Launch Gradio interface
# iface.launch()
# Define Gradio interface
iface = gr.Interface(
inputs=[
gr.Textbox(label="keyword", placeholder='ํค์๋๋ฅผ ์
๋ ฅํ์ธ์. (์.๋นํธ์ฝ์ธ)'),
gr.Textbox(label="OpenAI API ํค", placeholder="์ฌ๊ธฐ์ OpenAI API ํค๋ฅผ ์
๋ ฅํ์ธ์")
],
outputs=gr.JSON(label='์ ํ๋ธ 5๊ฐ ์์ฝ ๊ฒฐ๊ณผ'),
title="Summarize YouTube Videos",
description="Enter a keyword to summarize related YouTube videos.",
fn=summarize_button_click # ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์๋ฅผ ์ฌ๊ธฐ์ ์ ์ํฉ๋๋ค.
)
def summarize_button_click(keyword, OPENAI_API_KEY):
summary = summarize_youtube_videos(keyword, OPENAI_API_KEY)
return summary
# Launch Gradio interface
iface.launch()
|