Spaces:
Sleeping
Sleeping
Update pages/model.py
Browse files- 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
st.set_page_config(page_title="Tube2Insight
|
| 10 |
-
st.markdown("## π₯ Tube2Insight\n*Summarize any YouTube video using LLaMA power!*")
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 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("
|
| 24 |
-
|
| 25 |
-
st.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
with st.expander("π View
|
| 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 |
-
|
| 38 |
-
model_kwargs={"temperature": 0.5, "max_new_tokens": 512}
|
| 39 |
)
|
| 40 |
|
| 41 |
prompt = ChatPromptTemplate.from_messages([
|
| 42 |
-
("
|
| 43 |
-
("
|
| 44 |
])
|
| 45 |
-
|
| 46 |
messages = prompt.format_messages(transcript=transcript)
|
| 47 |
-
response = llm(messages)
|
| 48 |
|
| 49 |
-
st.
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|