rohitdiwane commited on
Commit
5e6bcca
Β·
verified Β·
1 Parent(s): aee62e4

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +11 -7
src/streamlit_app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  from docx import Document
3
  from io import BytesIO
 
4
 
5
  st.set_page_config(page_title="Doc Cleaner", layout="wide")
6
 
@@ -16,23 +17,26 @@ if uploaded_file:
16
 
17
  for para in doc.paragraphs:
18
  text = para.text.strip()
19
- style_name = para.style.name.lower()
20
 
21
  if not text:
22
  continue
23
 
24
- # πŸ”₯ Improved heading detection
 
 
25
  is_docx_heading = "heading" in style_name
26
  is_markdown_heading = text.startswith("#")
 
 
27
 
28
- if is_docx_heading or is_markdown_heading:
29
  removed_headings.append(text)
30
  else:
31
  cleaned_paragraphs.append(text)
32
 
33
  cleaned_text = "\n\n".join(cleaned_paragraphs)
34
 
35
- # βœ… Better UI
36
  st.subheader("πŸ—‘οΈ Removed Headings")
37
 
38
  if removed_headings:
@@ -40,6 +44,7 @@ if uploaded_file:
40
  else:
41
  st.info("No headings found")
42
 
 
43
  st.subheader("πŸ“ Cleaned Text")
44
 
45
  st.text_area(
@@ -48,13 +53,12 @@ if uploaded_file:
48
  height=400
49
  )
50
 
51
- # βœ… Download (no button nesting issue)
52
- buffer = BytesIO()
53
  new_doc = Document()
54
-
55
  for para in cleaned_paragraphs:
56
  new_doc.add_paragraph(para)
57
 
 
58
  new_doc.save(buffer)
59
  buffer.seek(0)
60
 
 
1
  import streamlit as st
2
  from docx import Document
3
  from io import BytesIO
4
+ import re
5
 
6
  st.set_page_config(page_title="Doc Cleaner", layout="wide")
7
 
 
17
 
18
  for para in doc.paragraphs:
19
  text = para.text.strip()
 
20
 
21
  if not text:
22
  continue
23
 
24
+ style_name = para.style.name.lower()
25
+
26
+ # βœ… Detection logic
27
  is_docx_heading = "heading" in style_name
28
  is_markdown_heading = text.startswith("#")
29
+ is_segment_heading = re.match(r"(segment\s*\d+)", text.lower())
30
+ is_colon_title = text.endswith(":") and len(text.split()) <= 6 # short titles like "Introduction:"
31
 
32
+ if is_docx_heading or is_markdown_heading or is_segment_heading or is_colon_title:
33
  removed_headings.append(text)
34
  else:
35
  cleaned_paragraphs.append(text)
36
 
37
  cleaned_text = "\n\n".join(cleaned_paragraphs)
38
 
39
+ # πŸ—‘οΈ Removed Headings Section
40
  st.subheader("πŸ—‘οΈ Removed Headings")
41
 
42
  if removed_headings:
 
44
  else:
45
  st.info("No headings found")
46
 
47
+ # πŸ“ Cleaned Text Section
48
  st.subheader("πŸ“ Cleaned Text")
49
 
50
  st.text_area(
 
53
  height=400
54
  )
55
 
56
+ # πŸ“₯ Download cleaned DOCX
 
57
  new_doc = Document()
 
58
  for para in cleaned_paragraphs:
59
  new_doc.add_paragraph(para)
60
 
61
+ buffer = BytesIO()
62
  new_doc.save(buffer)
63
  buffer.seek(0)
64