Harika22 commited on
Commit
c7a6370
Β·
verified Β·
1 Parent(s): 026568c

Update pages/model.py

Browse files
Files changed (1) hide show
  1. pages/model.py +52 -31
pages/model.py CHANGED
@@ -4,54 +4,75 @@ from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, H
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
 
14
- with st.expander("ℹ️ How It Works", expanded=False):
15
- st.markdown("""
16
- 1. Enter the YouTube URL 🎯
17
- 2. The app grabs the transcript πŸ“
18
- 3. LLaMA summarizes it using smart prompts πŸ¦™
19
- 4. You get a quick summary without watching the whole video! ⚑
20
- """)
21
 
 
22
 
23
- if st.button("πŸš€ Generate Summary") and video_url and HF_Token:
24
- try:
25
- st.info("⏳ Fetching transcript from YouTube...")
26
- loader = YoutubeLoader.from_youtube_url(video_url)
27
- docs = loader.load()
28
- transcript = docs[0].page_content
 
 
 
 
 
 
29
 
30
- with st.expander("πŸ“„ View Full Transcript"):
31
  st.write(transcript)
32
 
33
- st.info("🧠 Running LLaMA summarization...")
34
-
35
  llm = ChatHuggingFace(
36
  repo_id="meta-llama/Llama-3.1-8B-Instruct",
37
- huggingfacehub_api_token=HF_Token,
38
- model_kwargs={"temperature": 0.5, "max_new_tokens": 512}
39
  )
40
 
41
  prompt = ChatPromptTemplate.from_messages([
42
- ("system", "You are a helpful assistant that summarizes YouTube transcripts."),
43
- ("human", "Summarize the following transcript:\n\n{transcript}")
44
  ])
45
-
46
  messages = prompt.format_messages(transcript=transcript)
47
- response = llm(messages)
48
 
49
- st.success("βœ… Summary Ready!")
50
- st.markdown("### πŸ“ Summary")
51
- st.write(response.content)
 
 
52
 
53
- except Exception as e:
54
- st.error(f"❌ Error: {e}")
 
 
 
55
 
56
 
57
 
 
4
  from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
5
  from langchain_community.document_loaders import YoutubeLoader
6
 
7
+ hf_token = os.getenv("HF_Token")
8
+ if hf_token is None:
9
+ st.error("πŸ” Please set the 'HF_Token' environment variable with your Hugging Face token.")
10
+ else:
11
+ os.environ["HUGGINGFACEHUB_API_KEY"] = hf_token
12
 
13
+ st.set_page_config(page_title="🎬 Tube2Insight", page_icon="πŸŽ₯", layout="wide")
 
14
 
15
+ st.markdown("""
16
+ <style>
17
+ .stApp { background-color: #f7f9fc; }
18
+ .video-card {
19
+ background-color: #ffffff;
20
+ border: 2px solid #e0e0e0;
21
+ padding: 20px;
22
+ border-radius: 15px;
23
+ text-align: center;
24
+ box-shadow: 2px 2px 10px #ddd;
25
+ }
26
+ .video-card:hover {
27
+ border-color: #FF4B4B;
28
+ box-shadow: 3px 3px 12px #bbb;
29
+ }
30
+ </style>
31
+ """, unsafe_allow_html=True)
32
 
33
+ st.markdown("<h1 style='text-align: center; color: #FF4B4B;'>🎬 Tube2Insight</h1>", unsafe_allow_html=True)
34
+ st.markdown("### Paste a YouTube link below to get an instant summary πŸ‘‡")
 
 
 
 
 
35
 
36
+ video_url = st.text_input("πŸ“Ί YouTube Video URL")
37
 
38
+ if st.button("πŸ” Summarize"):
39
+ if not video_url:
40
+ st.warning("Please provide a video link.")
41
+ else:
42
+ with st.spinner("πŸ“„ Fetching transcript..."):
43
+ try:
44
+ docs = YoutubeLoader.from_youtube_url(video_url).load()
45
+ transcript = docs[0].page_content
46
+ st.success("βœ… Transcript loaded!")
47
+ except Exception as e:
48
+ st.error(f"Error loading video: {e}")
49
+ st.stop()
50
 
51
+ with st.expander("πŸ“„ View Transcript"):
52
  st.write(transcript)
53
 
 
 
54
  llm = ChatHuggingFace(
55
  repo_id="meta-llama/Llama-3.1-8B-Instruct",
56
+ model_kwargs={"temperature": 0.5, "max_new_tokens": 300}
 
57
  )
58
 
59
  prompt = ChatPromptTemplate.from_messages([
60
+ SystemMessagePromptTemplate.from_template("You are a helpful assistant summarizing YouTube transcripts."),
61
+ HumanMessagePromptTemplate.from_template("{transcript}")
62
  ])
 
63
  messages = prompt.format_messages(transcript=transcript)
 
64
 
65
+ with st.spinner("🧠 Summarizing with LLaMA..."):
66
+ response = llm.invoke(messages)
67
+
68
+ st.markdown("### πŸ“Œ Summary")
69
+ st.success(response.content)
70
 
71
+ st.markdown("---")
72
+ st.markdown(
73
+ "<p style='text-align: center; color: gray;'>πŸš€ Built with LangChain + LLaMA + Streamlit | by <a href='https://github.com/yourusername' target='_blank'>YourName</a></p>",
74
+ unsafe_allow_html=True
75
+ )
76
 
77
 
78