mulavamshi commited on
Commit
435b1b4
ยท
verified ยท
1 Parent(s): 57da61f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -20
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  os.environ["TRANSFORMERS_CACHE"] = "./hf_cache"
3
 
@@ -6,7 +7,9 @@ from transformers import pipeline
6
  from streamlit_lottie import st_lottie
7
  import requests
8
  import datetime
 
9
 
 
10
  def load_lottieurl(url):
11
  r = requests.get(url)
12
  if r.status_code != 200:
@@ -15,9 +18,11 @@ def load_lottieurl(url):
15
 
16
  lottie_animation = load_lottieurl("https://assets2.lottiefiles.com/packages/lf20_w51pcehl.json")
17
 
 
18
  st.markdown("<h1 style='text-align: center;'>๐Ÿ“ Text Summarization App</h1>", unsafe_allow_html=True)
19
  st_lottie(lottie_animation, height=250, key="header_anim")
20
 
 
21
  @st.cache_resource(show_spinner="๐Ÿ”„ Loading summarization model...")
22
  def load_summarizer(model_name):
23
  return pipeline("summarization", model=model_name)
@@ -28,34 +33,44 @@ model_map = {
28
  "PEGASUS": "google/pegasus-cnn_dailymail"
29
  }
30
 
31
- model_choice = st.selectbox("Choose Summarization Model", list(model_map.keys()))
32
  summarizer = load_summarizer(model_map[model_choice])
33
 
34
- mode = st.radio("Choose Output Mode:", ["Paragraph", "Bullet Points", "Custom"], horizontal=True)
 
35
 
36
  col1, col2 = st.columns(2)
37
 
38
  with col1:
39
- st.markdown("### โœ๏ธ Enter Your Text")
40
- user_input = st.text_area("", height=300, placeholder="Paste your job description, article, or any long-form text here...")
 
 
 
 
 
 
 
41
 
42
  word_count = len(user_input.split())
43
- st.markdown(f"**{word_count} words**")
44
 
 
45
  if mode != "Custom":
46
- length_label = st.radio("Summary Length", ["Short", "Medium"], horizontal=True)
47
  min_len = 40
48
  max_len = 150 if length_label == "Short" else 300
49
  else:
50
- st.markdown("### ๐Ÿ› ๏ธ Customize Summary Length")
51
  min_len = st.slider("Minimum Length", 20, 200, 50)
52
  max_len = st.slider("Maximum Length", 100, 500, 200)
53
 
 
54
  if st.button("โœจ Summarize", use_container_width=True):
55
  if not user_input.strip():
56
- st.warning("Please enter text to summarize.")
57
  else:
58
- with st.spinner("Generating your summary... hang tight! โณ"):
59
  try:
60
  result = summarizer(user_input, max_length=max_len, min_length=min_len, do_sample=False)
61
  summary = result[0]['summary_text']
@@ -68,9 +83,7 @@ with col1:
68
  except Exception as e:
69
  st.error(f"โš ๏ธ Error during summarization: {e}")
70
 
71
- import datetime # โœ… Make sure this is at the top of your file
72
-
73
- # Right Column: Output
74
  with col2:
75
  st.markdown("### ๐Ÿ“„ Summary Output")
76
 
@@ -79,17 +92,16 @@ with col2:
79
  summary_words = len(st.session_state["summary"].split())
80
  st.markdown(f"๐Ÿ“ {summary_words} words")
81
 
82
- # โœ… Direct Download Button
83
  st.download_button(
84
- label="๐Ÿ“ฅ Download This Summary",
85
  data=st.session_state["summary"],
86
  file_name="summary.txt",
87
  mime="text/plain"
88
  )
89
 
90
- # Save Options in Expander
91
  with st.expander("๐Ÿ’พ Save & View Summary History"):
92
- # Save to history file
93
  if st.button("โœ… Save this summary to history"):
94
  try:
95
  with open("summary_history.txt", "a", encoding="utf-8") as f:
@@ -104,16 +116,42 @@ with col2:
104
  except Exception as e:
105
  st.error(f"โŒ Failed to save summary: {e}")
106
 
107
- # View Summary History
108
  if st.checkbox("๐Ÿ“š Show Summary History"):
109
  try:
110
  with open("summary_history.txt", "r", encoding="utf-8") as f:
111
  history = f.read()
112
  st.text_area("๐Ÿ—‚๏ธ Summary History", value=history, height=300)
113
  except FileNotFoundError:
114
- st.info("No history found yet.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  else:
116
- st.info("Your summary will appear here once generated.")
117
 
 
118
  st.markdown("<hr>", unsafe_allow_html=True)
119
- st.markdown("<small>Built by MULA VAMSHI๐Ÿค using Hugging Face Transformers, Streamlit & Lottie</small>", unsafe_allow_html=True)
 
 
 
 
1
+ # ------------------------๐Ÿ”ง ENVIRONMENT SETUP ------------------------
2
  import os
3
  os.environ["TRANSFORMERS_CACHE"] = "./hf_cache"
4
 
 
7
  from streamlit_lottie import st_lottie
8
  import requests
9
  import datetime
10
+ import pandas as pd
11
 
12
+ # ------------------------๐ŸŽž๏ธ LOAD LOTTIE ANIMATION ------------------------
13
  def load_lottieurl(url):
14
  r = requests.get(url)
15
  if r.status_code != 200:
 
18
 
19
  lottie_animation = load_lottieurl("https://assets2.lottiefiles.com/packages/lf20_w51pcehl.json")
20
 
21
+ # ------------------------๐Ÿ“Œ APP TITLE & HEADER ------------------------
22
  st.markdown("<h1 style='text-align: center;'>๐Ÿ“ Text Summarization App</h1>", unsafe_allow_html=True)
23
  st_lottie(lottie_animation, height=250, key="header_anim")
24
 
25
+ # ------------------------๐Ÿš€ LOAD SUMMARIZATION MODELS ------------------------
26
  @st.cache_resource(show_spinner="๐Ÿ”„ Loading summarization model...")
27
  def load_summarizer(model_name):
28
  return pipeline("summarization", model=model_name)
 
33
  "PEGASUS": "google/pegasus-cnn_dailymail"
34
  }
35
 
36
+ model_choice = st.selectbox("๐Ÿ” Choose Summarization Model", list(model_map.keys()))
37
  summarizer = load_summarizer(model_map[model_choice])
38
 
39
+ # ------------------------๐Ÿ› ๏ธ USER INPUT & CONTROLS ------------------------
40
+ mode = st.radio("๐Ÿ“ค Choose Output Mode:", ["Paragraph", "Bullet Points", "Custom"], horizontal=True)
41
 
42
  col1, col2 = st.columns(2)
43
 
44
  with col1:
45
+ st.markdown("### โœ๏ธ Enter Text or Upload File")
46
+
47
+ uploaded_file = st.file_uploader("๐Ÿ“‚ Upload .txt file", type=["txt"])
48
+
49
+ if uploaded_file is not None:
50
+ user_input = uploaded_file.read().decode("utf-8")
51
+ st.text_area("๐Ÿ“ƒ Uploaded Text Preview", value=user_input, height=200)
52
+ else:
53
+ user_input = st.text_area("", height=300, placeholder="Paste your job description, article, or any long-form text here...")
54
 
55
  word_count = len(user_input.split())
56
+ st.markdown(f"**๐Ÿงฎ {word_count} words**")
57
 
58
+ # ๐Ÿ”ง Summary length control
59
  if mode != "Custom":
60
+ length_label = st.radio("๐Ÿ“ Summary Length", ["Short", "Medium"], horizontal=True)
61
  min_len = 40
62
  max_len = 150 if length_label == "Short" else 300
63
  else:
64
+ st.markdown("### ๐ŸŽš๏ธ Customize Summary Length")
65
  min_len = st.slider("Minimum Length", 20, 200, 50)
66
  max_len = st.slider("Maximum Length", 100, 500, 200)
67
 
68
+ # โœจ Generate Summary
69
  if st.button("โœจ Summarize", use_container_width=True):
70
  if not user_input.strip():
71
+ st.warning("โš ๏ธ Please enter text to summarize.")
72
  else:
73
+ with st.spinner("๐Ÿ”„ Generating your summary... hang tight! โณ"):
74
  try:
75
  result = summarizer(user_input, max_length=max_len, min_length=min_len, do_sample=False)
76
  summary = result[0]['summary_text']
 
83
  except Exception as e:
84
  st.error(f"โš ๏ธ Error during summarization: {e}")
85
 
86
+ # ------------------------๐Ÿ“„ SUMMARY OUTPUT & HISTORY ------------------------
 
 
87
  with col2:
88
  st.markdown("### ๐Ÿ“„ Summary Output")
89
 
 
92
  summary_words = len(st.session_state["summary"].split())
93
  st.markdown(f"๐Ÿ“ {summary_words} words")
94
 
95
+ # ๐Ÿ“ฅ Download Summary as TXT
96
  st.download_button(
97
+ label="๐Ÿ“ฅ Download This Summary (TXT)",
98
  data=st.session_state["summary"],
99
  file_name="summary.txt",
100
  mime="text/plain"
101
  )
102
 
103
+ # ๐Ÿ’พ Save to Summary History
104
  with st.expander("๐Ÿ’พ Save & View Summary History"):
 
105
  if st.button("โœ… Save this summary to history"):
106
  try:
107
  with open("summary_history.txt", "a", encoding="utf-8") as f:
 
116
  except Exception as e:
117
  st.error(f"โŒ Failed to save summary: {e}")
118
 
119
+ # ๐Ÿ“š View Summary History
120
  if st.checkbox("๐Ÿ“š Show Summary History"):
121
  try:
122
  with open("summary_history.txt", "r", encoding="utf-8") as f:
123
  history = f.read()
124
  st.text_area("๐Ÿ—‚๏ธ Summary History", value=history, height=300)
125
  except FileNotFoundError:
126
+ st.info("โ„น๏ธ No history found yet.")
127
+
128
+ # ๐Ÿ“Š Export as CSV
129
+ if st.button("โฌ‡๏ธ Export History as CSV"):
130
+ try:
131
+ summaries = []
132
+ with open("summary_history.txt", "r", encoding="utf-8") as f:
133
+ lines = f.read().split("="*50)
134
+ for entry in lines:
135
+ if "๐Ÿ•’ Timestamp" in entry:
136
+ lines_dict = {
137
+ "Timestamp": entry.split("๐Ÿ•’ Timestamp: ")[1].split("\n")[0].strip(),
138
+ "Model": entry.split("๐Ÿ”น Model Used: ")[1].split("\n")[0].strip(),
139
+ "Mode": entry.split("๐Ÿ”ธ Mode: ")[1].split("\n")[0].strip(),
140
+ "Original_Text": entry.split("๐Ÿ“ Original Text:\n")[1].split("\n\n")[0].strip(),
141
+ "Summary": entry.split("โœ… Summary:\n")[1].strip()
142
+ }
143
+ summaries.append(lines_dict)
144
+ df = pd.DataFrame(summaries)
145
+ csv = df.to_csv(index=False).encode('utf-8')
146
+ st.download_button("๐Ÿ“„ Download CSV File", csv, "summary_history.csv", "text/csv")
147
+ except Exception as e:
148
+ st.error(f"โŒ Failed to export as CSV: {e}")
149
  else:
150
+ st.info("โ„น๏ธ Your summary will appear here once generated.")
151
 
152
+ # ------------------------๐Ÿ”š FOOTER ------------------------
153
  st.markdown("<hr>", unsafe_allow_html=True)
154
+ st.markdown(
155
+ "<small>๐Ÿš€ Built by <b>MULA VAMSHI๐Ÿค</b> using Hugging Face Transformers, Streamlit & Lottie</small>",
156
+ unsafe_allow_html=True
157
+ )