FrancoisHB commited on
Commit
40be60a
1 Parent(s): 4d10fc5

Commit Test SRT

Browse files
Files changed (1) hide show
  1. app.py +35 -28
app.py CHANGED
@@ -1,7 +1,10 @@
1
  import streamlit as st
 
2
  from transformers import pipeline
3
  from keybert import KeyBERT
4
 
 
 
5
 
6
  # Function to extract text from SRT-formatted text
7
  def extract_text_from_srt_text(srt_text):
@@ -9,7 +12,6 @@ def extract_text_from_srt_text(srt_text):
9
  texts = [subtitle.split("\n")[2] for subtitle in lines if subtitle.strip()]
10
  return " ".join(texts)
11
 
12
-
13
  # Function to generate summary from text
14
  def generate_summary(text, summary_length):
15
  summarizer = pipeline("summarization")
@@ -17,43 +19,48 @@ def generate_summary(text, summary_length):
17
  summary_text = summary[0]["summary_text"]
18
  return summary_text
19
 
20
-
21
  # Function to extract top 4 topics from text
22
  def extract_top_topics(text, n_top_topics):
23
  model = KeyBERT('distilbert-base-nli-mean-tokens')
24
- keywords = model.extract_keywords(text, keyphrase_ngram_range=(1, 3), stop_words='english', use_maxsum=True,
25
- nr_candidates=20, top_n=n_top_topics)
26
- return keywords
27
-
28
 
29
  # Streamlit app
30
  st.title("SRT Summarization")
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Define summarization route
34
- @st.experimental_memo
35
- def summarize_text(srt_text):
 
 
 
 
36
  text_to_summarize = extract_text_from_srt_text(srt_text)
37
  summary = generate_summary(text_to_summarize, 150)
38
  top_topics = extract_top_topics(text_to_summarize, 4)
39
- return summary, top_topics
40
-
41
-
42
- # Main entry point
43
- def main():
44
- srt_text_input = st.text_area("Paste SRT-formatted text here:")
45
-
46
- if st.button("Summarize"):
47
- if srt_text_input.strip():
48
- summary, top_topics = summarize_text(srt_text_input)
49
- st.subheader("Summary:")
50
- st.write(summary)
51
- st.subheader("Top 4 Keywords:")
52
- for topic, _ in top_topics:
53
- st.write(f"- {topic}")
54
- else:
55
- st.warning("Please enter some SRT-formatted text.")
56
-
57
 
58
  if __name__ == "__main__":
59
- main()
 
1
  import streamlit as st
2
+ from flask import Flask, request, jsonify
3
  from transformers import pipeline
4
  from keybert import KeyBERT
5
 
6
+ # Initialize Flask app
7
+ app = Flask(__name__)
8
 
9
  # Function to extract text from SRT-formatted text
10
  def extract_text_from_srt_text(srt_text):
 
12
  texts = [subtitle.split("\n")[2] for subtitle in lines if subtitle.strip()]
13
  return " ".join(texts)
14
 
 
15
  # Function to generate summary from text
16
  def generate_summary(text, summary_length):
17
  summarizer = pipeline("summarization")
 
19
  summary_text = summary[0]["summary_text"]
20
  return summary_text
21
 
 
22
  # Function to extract top 4 topics from text
23
  def extract_top_topics(text, n_top_topics):
24
  model = KeyBERT('distilbert-base-nli-mean-tokens')
25
+ keywords = model.extract_keywords(text, keyphrase_ngram_range=(1, 3), stop_words='english', use_maxsum=True, nr_candidates=20, top_n=n_top_topics)
26
+ return [topic for topic, _ in keywords]
 
 
27
 
28
  # Streamlit app
29
  st.title("SRT Summarization")
30
 
31
+ # Text area for user to input SRT-formatted text
32
+ srt_text_input = st.text_area("Paste SRT-formatted text here:")
33
+
34
+ # Button to trigger summarization
35
+ if st.button("Summarize"):
36
+ # Check if text area is not empty
37
+ if srt_text_input.strip():
38
+ # Extract text from SRT-formatted text
39
+ text_to_summarize = extract_text_from_srt_text(srt_text_input)
40
+ # Generate summary
41
+ summary = generate_summary(text_to_summarize, 150)
42
+ # Extract top 4 topics
43
+ top_topics = extract_top_topics(text_to_summarize, 4)
44
+ # Display summary and top 4 topics
45
+ st.subheader("Summary:")
46
+ st.write(summary)
47
+ st.subheader("Top 4 Keywords:")
48
+ for topic in top_topics:
49
+ st.write(f"- {topic}")
50
+ else:
51
+ st.warning("Please enter some SRT-formatted text.")
52
 
53
+ # Define endpoint for REST API
54
+ @app.route("/summarize", methods=["POST"])
55
+ def summarize():
56
+ data = request.json
57
+ if "srt_text" not in data:
58
+ return jsonify({"error": "Missing 'srt_text' parameter"}), 400
59
+ srt_text = data["srt_text"]
60
  text_to_summarize = extract_text_from_srt_text(srt_text)
61
  summary = generate_summary(text_to_summarize, 150)
62
  top_topics = extract_top_topics(text_to_summarize, 4)
63
+ return jsonify({"summary": summary, "top_topics": top_topics})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  if __name__ == "__main__":
66
+ app.run()