Harika22 commited on
Commit
a56f268
Β·
verified Β·
1 Parent(s): cfd73e5

Update pages/model.py

Browse files
Files changed (1) hide show
  1. 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
- os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv('HF_Token')
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 LLM!*")
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