File size: 3,473 Bytes
51306d7 1ab5a2e 2819be2 ad28e9f eb9c355 8626295 2abbd75 8626295 51306d7 2819be2 8626295 51306d7 ec4b4c0 51306d7 e0b59a0 637c10a ca1bd19 ff843a4 ca1bd19 637c10a ca1bd19 e0b59a0 ca1bd19 e0b59a0 c358952 51306d7 e0b59a0 658998d 1ab5a2e 51306d7 8f3a300 ff843a4 2abbd75 e0b59a0 c196821 6000f8f c196821 2bb8654 ff843a4 c196821 2abbd75 c196821 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
from youtubesearchpython import VideosSearch, Transcript # Changed import statement
import gradio as gr
import openai
import os
import requests
# 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):
API_URL = "https://api.openai.com/v1/chat/completions"
payload = {
"model": "gpt-4-0125-preview", # λͺ¨λΈ μ΄λ¦ νμΈ νμ
"messages": [{
"role": "system",
"content": "λΉμ μ λ΄μ©μ μμ½νλ μ±λ΄μ
λλ€."
}, {
"role": "user",
"content": f"μ νλΈ λ΄μ©μΈ '{contents}'μ λν΄ μμ½ν΄μ£ΌμΈμ."
}],
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
response = requests.post(API_URL, headers=headers, json=payload)
if response.status_code == 200:
data = response.json()
return data["choices"][0]["message"]["content"]
else:
# μ€λ₯ λ©μμ§ κ°μ
return f"μ€λ₯κ° λ°μνμ΅λλ€. μν μ½λ: {response.status_code}, λ©μμ§: {response.json().get('error', {}).get('message', 'Unknown error')}", ""
# 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_youtube_videos(keyword, OPENAI_API_KEY):
urls = search_youtube_videos(keyword)
contents = get_transcript(urls)
summary = summarize_text(contents, OPENAI_API_KEY)
return summary
with gr.Blocks(css="footer {visibility: hidden;}") as demo:
with gr.Tab():
keyword = gr.Textbox(label="keyword", placeholder='ν€μλλ₯Ό μ
λ ₯νμΈμ. (μ.λΉνΈμ½μΈ)')
OPENAI_API_KEY = gr.Textbox(label="OpenAI API ν€", placeholder="OpenAI API ν€λ₯Ό μ
λ ₯νμΈμ")
analysis_result = gr.HTML()
analysis_btn = gr.Button("submit")
analysis_btn.click(
fn=summarize_youtube_videos,
inputs=[keyword, OPENAI_API_KEY],
outputs=[analysis_result]
)
demo.launch()
|