Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from langchain_community.document_loaders import YoutubeLoader
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
import torch
|
| 5 |
-
|
| 6 |
-
st.set_page_config(page_title="DeepSeek YouTube Summarizer", layout="centered")
|
| 7 |
-
st.title("📺 YouTube Video Summarizer with DeepSeek")
|
| 8 |
-
|
| 9 |
-
# Input YouTube URL
|
| 10 |
-
url = st.text_input("Enter YouTube Video URL:")
|
| 11 |
-
|
| 12 |
-
# Load DeepSeek model and tokenizer
|
| 13 |
-
@st.cache_resource
|
| 14 |
-
def load_model():
|
| 15 |
-
model_id = "deepseek-ai/deepseek-llm-7b-instruct"
|
| 16 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 17 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
-
model_id, device_map="auto", torch_dtype=torch.float16, trust_remote_code=True
|
| 19 |
-
)
|
| 20 |
-
return tokenizer, model
|
| 21 |
-
|
| 22 |
-
tokenizer, model = load_model()
|
| 23 |
-
|
| 24 |
-
def summarize_with_deepseek(text):
|
| 25 |
-
prompt = f"""<|system|>\nYou are a helpful assistant.\n<|user|>\nSummarize the following text:\n{text}\n<|assistant|>"""
|
| 26 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 27 |
-
outputs = model.generate(
|
| 28 |
-
**inputs,
|
| 29 |
-
max_new_tokens=300,
|
| 30 |
-
do_sample=False,
|
| 31 |
-
temperature=0.7,
|
| 32 |
-
top_k=50,
|
| 33 |
-
top_p=0.95,
|
| 34 |
-
)
|
| 35 |
-
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
-
return summary.split("<|assistant|>")[-1].strip()
|
| 37 |
-
|
| 38 |
-
if st.button("Extract and Summarize"):
|
| 39 |
-
if url:
|
| 40 |
-
try:
|
| 41 |
-
loader = YoutubeLoader.from_youtube_url(url)
|
| 42 |
-
data = loader.load()
|
| 43 |
-
transcript = data[0].page_content if data else "No transcript found."
|
| 44 |
-
|
| 45 |
-
st.subheader("📖 Extracted Transcript")
|
| 46 |
-
st.text_area("Transcript:", transcript, height=300)
|
| 47 |
-
|
| 48 |
-
# Truncate for prompt length safety
|
| 49 |
-
if len(transcript) > 1500:
|
| 50 |
-
transcript = transcript[:1500]
|
| 51 |
-
|
| 52 |
-
with st.spinner("Summarizing using DeepSeek..."):
|
| 53 |
-
summary = summarize_with_deepseek(transcript)
|
| 54 |
-
|
| 55 |
-
st.subheader("🧠 Summary")
|
| 56 |
-
st.success(summary)
|
| 57 |
-
|
| 58 |
-
except Exception as e:
|
| 59 |
-
st.error(f"Error: {str(e)}")
|
| 60 |
-
else:
|
| 61 |
-
st.warning("Please enter a valid YouTube URL.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|