Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import langchain
|
| 4 |
+
import langchain_huggingface
|
| 5 |
+
import huggingface_hub
|
| 6 |
+
from langchain_community.document_loaders import YoutubeLoader
|
| 7 |
+
from langchain_huggingface import HuggingFaceEndpoint,ChatHuggingFace
|
| 8 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.getenv("Utube")
|
| 9 |
+
os.environ['HF_TOKEN'] = os.getenv("Utube")
|
| 10 |
+
meta_llm=HuggingFaceEndpoint(repo_id="meta-llama/Llama-3.1-8B-Instruct",
|
| 11 |
+
provider="nebius",
|
| 12 |
+
temperature=0.5,max_new_tokens=150,
|
| 13 |
+
task="conversational")
|
| 14 |
+
model=ChatHuggingFace(llm=meta_llm,
|
| 15 |
+
repo_id="meta-llama/Llama-3.1-8B-Instruct",
|
| 16 |
+
provider="nebius",
|
| 17 |
+
temperature=0.5,
|
| 18 |
+
max_new_tokens=50,
|
| 19 |
+
task="conversational")
|
| 20 |
+
def get_youtube_transcript(url):
|
| 21 |
+
loader = YoutubeLoader.from_youtube_url(url)
|
| 22 |
+
docs = loader.load()
|
| 23 |
+
return docs[0].page_content
|
| 24 |
+
|
| 25 |
+
# 📝 Function to summarize
|
| 26 |
+
def summarize_youtube_video(url):
|
| 27 |
+
transcript = get_youtube_transcript(url)
|
| 28 |
+
if len(transcript) > 3000:
|
| 29 |
+
transcript = transcript[:3000] # Trim to avoid token limit
|
| 30 |
+
prompt = f"Summarize this YouTube video transcript:\n\n{transcript}"
|
| 31 |
+
response = model.invoke(prompt)
|
| 32 |
+
return response.content
|
| 33 |
+
st.set_page_config(page_title="YouTube Video Summarizer", layout="centered")
|
| 34 |
+
st.title("🎬 YouTube Video Summarizer")
|
| 35 |
+
st.write("Paste a YouTube video URL below to get a concise summary using Llama 3.1 🧠")
|
| 36 |
+
|
| 37 |
+
youtube_url = st.text_input("📥 Enter YouTube URL:")
|
| 38 |
+
if st.button("Summarize"):
|
| 39 |
+
if youtube_url.strip() == "":
|
| 40 |
+
st.warning("Please enter a valid YouTube video URL.")
|
| 41 |
+
else:
|
| 42 |
+
with st.spinner("Fetching and summarizing..."):
|
| 43 |
+
try:
|
| 44 |
+
summary = summarize_youtube_video(youtube_url)
|
| 45 |
+
st.success("✅ Summary Generated:")
|
| 46 |
+
st.write(summary)
|
| 47 |
+
except Exception as e:
|
| 48 |
+
st.error(f"Something went wrong: {e}")
|