import os
import streamlit as st
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
from youtube_transcript_api import YouTubeTranscriptApi
from langchain_community.document_loaders import YoutubeLoader
from langchain_google_genai import GoogleGenerativeAI,ChatGoogleGenerativeAI
from youtube_transcript_api._errors import TranscriptsDisabled
gemini_token = os.environ.get("gemini")
os.environ["GOOGLE_API_KEY"] = gemini_token
st.set_page_config(page_title="🎬 Tube2Insight", page_icon="🎥", layout="wide")
st.markdown("""
""", unsafe_allow_html=True)
st.markdown("
🎬 Tube2Insight
", unsafe_allow_html=True)
st.markdown("### Paste a YouTube link below to get an instant summary 👇")
video_url = st.text_input("📺 YouTube Video URL")
if st.button("🔍 Summarize"):
if not video_url:
st.warning("Please provide a video link.")
else:
with st.spinner("📄 Fetching transcript..."):
try:
docs = YoutubeLoader.from_youtube_url(video_url).load()
transcript = docs[0].page_content
st.success("✅ Transcript loaded!")
except Exception as e:
st.error(f"Error loading video: {e}")
st.stop()
with st.expander("📄 View Transcript"):
st.write(transcript)
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
temperature=0.5,
max_output_tokens=512
)
prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("You are a helpful assistant summarizing YouTube transcripts."),
HumanMessagePromptTemplate.from_template("{transcript}")
])
messages = prompt.format_messages(transcript=transcript)
with st.spinner("🧠 Summarizing with Gemini 2.0 Flash..."):
response = llm.invoke(messages)
st.markdown("### 📌 Summary")
st.success(response.content)
st.markdown("---")
st.markdown(
"🚀 Built with LangChain + Gemini + Streamlit
",
unsafe_allow_html=True
)