File size: 2,448 Bytes
9f27a52
40be60a
75855be
2beb89b
42c2597
40be60a
 
7c96a81
a8924ed
 
7c96a81
 
a1d11c2
ece6dcf
2a6b055
 
 
 
4fa5477
2beb89b
 
 
 
 
40be60a
 
a1d11c2
 
 
 
40be60a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c96a81
40be60a
 
 
 
 
 
 
7c96a81
 
 
40be60a
7c96a81
 
40be60a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import streamlit as st
from flask import Flask, request, jsonify
from transformers import pipeline
from keybert import KeyBERT

# Initialize Flask app
app = Flask(__name__)

# Function to extract text from SRT-formatted text
def extract_text_from_srt_text(srt_text):
    lines = srt_text.strip().split("\n\n")
    texts = [subtitle.split("\n")[2] for subtitle in lines if subtitle.strip()]
    return " ".join(texts)

# Function to generate summary from text
def generate_summary(text, summary_length):
    summarizer = pipeline("summarization")
    summary = summarizer(text, max_length=summary_length, min_length=30, do_sample=False)
    summary_text = summary[0]["summary_text"]
    return summary_text

# Function to extract top 4 topics from text
def extract_top_topics(text, n_top_topics):
    model = KeyBERT('distilbert-base-nli-mean-tokens')
    keywords = model.extract_keywords(text, keyphrase_ngram_range=(1, 3), stop_words='english', use_maxsum=True, nr_candidates=20, top_n=n_top_topics)
    return [topic for topic, _ in keywords]

# Streamlit app
st.title("SRT Summarization")

# Text area for user to input SRT-formatted text
srt_text_input = st.text_area("Paste SRT-formatted text here:")

# Button to trigger summarization
if st.button("Summarize"):
    # Check if text area is not empty
    if srt_text_input.strip():
        # Extract text from SRT-formatted text
        text_to_summarize = extract_text_from_srt_text(srt_text_input)
        # Generate summary
        summary = generate_summary(text_to_summarize, 150)
        # Extract top 4 topics
        top_topics = extract_top_topics(text_to_summarize, 4)
        # Display summary and top 4 topics
        st.subheader("Summary:")
        st.write(summary)
        st.subheader("Top 4 Keywords:")
        for topic in top_topics:
            st.write(f"- {topic}")
    else:
        st.warning("Please enter some SRT-formatted text.")

# Define endpoint for REST API
@app.route("/summarize", methods=["POST"])
def summarize():
    data = request.json
    if "srt_text" not in data:
        return jsonify({"error": "Missing 'srt_text' parameter"}), 400
    srt_text = data["srt_text"]
    text_to_summarize = extract_text_from_srt_text(srt_text)
    summary = generate_summary(text_to_summarize, 150)
    top_topics = extract_top_topics(text_to_summarize, 4)
    return jsonify({"summary": summary, "top_topics": top_topics})

if __name__ == "__main__":
    app.run()