Spaces:
Sleeping
Sleeping
Update pages/model.py
Browse files- pages/model.py +39 -3
pages/model.py
CHANGED
|
@@ -2,12 +2,12 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
| 4 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
os.environ["HF_TOKEN"] = os.getenv('HF_Token')
|
| 8 |
|
| 9 |
st.set_page_config(page_title="Tube2Insight π¬", layout="centered")
|
| 10 |
-
st.markdown("## π₯ Tube2Insight\n*Summarize any YouTube video using
|
| 11 |
|
| 12 |
video_url = st.text_input("πΊ Paste a YouTube video link")
|
| 13 |
|
|
@@ -19,5 +19,41 @@ with st.expander("βΉοΈ How It Works", expanded=False):
|
|
| 19 |
4. You get a quick summary without watching the whole video! β‘
|
| 20 |
""")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
| 4 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 5 |
+
from langchain_community.document_loaders import YoutubeLoader
|
| 6 |
|
| 7 |
+
HF_Token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
|
|
|
| 8 |
|
| 9 |
st.set_page_config(page_title="Tube2Insight π¬", layout="centered")
|
| 10 |
+
st.markdown("## π₯ Tube2Insight\n*Summarize any YouTube video using LLaMA power!*")
|
| 11 |
|
| 12 |
video_url = st.text_input("πΊ Paste a YouTube video link")
|
| 13 |
|
|
|
|
| 19 |
4. You get a quick summary without watching the whole video! β‘
|
| 20 |
""")
|
| 21 |
|
| 22 |
+
if not hf_token:
|
| 23 |
+
st.warning("β οΈ Please set `HUGGINGFACEHUB_API_TOKEN` in your environment.", icon="π")
|
| 24 |
+
|
| 25 |
+
if st.button("π Generate Summary") and video_url and hf_token:
|
| 26 |
+
try:
|
| 27 |
+
st.info("β³ Fetching transcript from YouTube...")
|
| 28 |
+
loader = YoutubeLoader.from_youtube_url(video_url)
|
| 29 |
+
docs = loader.load()
|
| 30 |
+
transcript = docs[0].page_content
|
| 31 |
+
|
| 32 |
+
with st.expander("π View Full Transcript"):
|
| 33 |
+
st.write(transcript)
|
| 34 |
+
|
| 35 |
+
st.info("π§ Running LLaMA summarization...")
|
| 36 |
+
|
| 37 |
+
llm = ChatHuggingFace(
|
| 38 |
+
repo_id="meta-llama/Llama-3.1-8B-Instruct",
|
| 39 |
+
huggingfacehub_api_token=HF_Token,
|
| 40 |
+
model_kwargs={"temperature": 0.5, "max_new_tokens": 512}
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 44 |
+
("system", "You are a helpful assistant that summarizes YouTube transcripts."),
|
| 45 |
+
("human", "Summarize the following transcript:\n\n{transcript}")
|
| 46 |
+
])
|
| 47 |
+
|
| 48 |
+
messages = prompt.format_messages(transcript=transcript)
|
| 49 |
+
response = llm(messages)
|
| 50 |
+
|
| 51 |
+
st.success("β
Summary Ready!")
|
| 52 |
+
st.markdown("### π Summary")
|
| 53 |
+
st.write(response.content)
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(f"β Error: {e}")
|
| 57 |
+
|
| 58 |
|
| 59 |
|