youtube_summary / app.py
anpigon's picture
Create app.py
38b983a verified
import streamlit as st
from pytubefix import YouTube
from pytubefix.cli import on_progress
import google.generativeai as genai
import time
import os
# Gemini API ν‚€ μ„€μ •
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
def process_video(url):
with st.spinner("λ™μ˜μƒ 처리 쀑..."):
yt = YouTube(url, on_progress_callback=on_progress)
st.sidebar.info(f"제λͺ©: {yt.title}")
video_file_name = "video.mp4"
ys = yt.streams.get_lowest_resolution()
ys.download(filename=video_file_name)
st.sidebar.info("유튜브 λ™μ˜μƒ λ‹€μš΄λ‘œλ“œ μ™„λ£Œ")
video_file = genai.upload_file(path=video_file_name)
st.sidebar.info(f"generativeai에 λ™μ˜μƒ μ—…λ‘œλ“œ μ™„λ£Œ")
while video_file.state.name == "PROCESSING":
st.sidebar.info("처리 쀑...")
time.sleep(10)
video_file = genai.get_file(video_file.name)
if video_file.state.name == "FAILED":
raise ValueError(video_file.state.name)
prompt = "이 λ™μ˜μƒμ„ μš”μ•½ν•©λ‹ˆλ‹€. μ£Όμš” λ‚΄μš©μ„ 5개의 bullet point둜 μ •λ¦¬ν•˜κ³ , κ·Έ μ•„λž˜μ— μƒμ„Έν•œ μš”μ•½μ„ μ œκ³΅ν•΄μ£Όμ„Έμš”."
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
st.sidebar.info("AI λͺ¨λΈμ΄ λ™μ˜μƒμ„ 뢄석 쀑...")
response = model.generate_content(
[video_file, prompt], request_options={"timeout": 600}
)
genai.delete_file(video_file.name)
st.sidebar.info("μž„μ‹œ 파일 μ‚­μ œ μ™„λ£Œ")
os.remove(video_file_name)
st.sidebar.info("μž„μ‹œ λΉ„λ””μ˜€ 파일 μ‚­μ œ μ™„λ£Œ")
return response.text
st.set_page_config(page_title="유튜브 λ™μ˜μƒ μš”μ•½ μ•±", layout="wide")
st.title("πŸŽ₯ 유튜브 λ™μ˜μƒ μš”μ•½ μ•±")
url = st.text_input("유튜브 URL을 μž…λ ₯ν•˜μ„Έμš”:")
if st.button("μš”μ•½ν•˜κΈ°", key="summarize"):
if url:
try:
summary = process_video(url)
st.success("μš”μ•½μ΄ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€!")
# μš”μ•½ κ²°κ³Ό ν‘œμ‹œ
st.header("πŸ“Š μš”μ•½ κ²°κ³Ό")
# μ£Όμš” λ‚΄μš© (bullet points)
st.subheader("μ£Όμš” λ‚΄μš©")
bullet_points = summary.split("\n\n")[0].split("\n")[1:]
for point in bullet_points:
st.markdown(f"- {point.strip()}")
# 상세 μš”μ•½
st.subheader("상세 μš”μ•½")
detailed_summary = "\n\n".join(summary.split("\n\n")[1:])
st.markdown(detailed_summary)
except Exception as e:
st.error(f"였λ₯˜ λ°œμƒ: {str(e)}")
else:
st.warning("URL을 μž…λ ₯ν•΄μ£Όμ„Έμš”.")
# μ‚¬μ΄λ“œλ°”μ— 둜그 정보 ν‘œμ‹œ
st.sidebar.title("처리 둜그")