File size: 2,377 Bytes
51306d7 1ab5a2e 2819be2 ad28e9f 8626295 2abbd75 8626295 51306d7 2819be2 8626295 51306d7 ec4b4c0 51306d7 e0b59a0 637c10a a427bef e0b59a0 c358952 7e9da05 51306d7 c358952 e0b59a0 c358952 51306d7 e0b59a0 1ab5a2e 51306d7 802a1f3 51306d7 e0b59a0 2fa67b7 51306d7 2abbd75 7640f86 1ab5a2e 2abbd75 51306d7 2fa67b7 2abbd75 e0b59a0 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 |
from youtubesearchpython import VideosSearch, Transcript # Changed import statement
import gradio as gr
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
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
def summarize_text(contents, OPENAI_API_KEY):
response = openai.ChatCompletion.create(
model="gpt-4-turbo-preview",
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, OPENAI_API_KEY):
urls = search_youtube_videos(keyword)
contents = get_transcript(urls)
summary = summarize_text(contents,OPENAI_API_KEY)
return summary
def summarize_button_click(keyword, OPENAI_API_KEY):
summary = summarize_youtube_videos(keyword, OPENAI_API_KEY)
return summary
# 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 # ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์๋ฅผ ์ฌ๊ธฐ์ ์ ์ํฉ๋๋ค.
)
# Launch Gradio interface
iface.launch()
|