Update app.py
Browse files
app.py
CHANGED
|
@@ -15,14 +15,12 @@ summarizer = pipeline(
|
|
| 15 |
device=-1 # CPU only
|
| 16 |
)
|
| 17 |
|
| 18 |
-
MAX_MODEL_TOKENS = 1024
|
| 19 |
CHUNK_SIZE = 900 # safe margin
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
-
# Utilities
|
| 23 |
# =========================
|
| 24 |
def clean_text(text: str) -> str:
|
| 25 |
-
"""Fix quotes, spacing, repetition, and broken punctuation."""
|
| 26 |
text = text.replace("β", "'").replace("β", "'")
|
| 27 |
text = text.replace("β", '"').replace("β", '"')
|
| 28 |
text = re.sub(r"[.]{2,}", ".", text)
|
|
@@ -38,9 +36,7 @@ def clean_text(text: str) -> str:
|
|
| 38 |
result.append(s.strip())
|
| 39 |
return " ".join(result)
|
| 40 |
|
| 41 |
-
|
| 42 |
def chunk_text(text: str):
|
| 43 |
-
"""Token-aware chunking to avoid model overflow."""
|
| 44 |
tokens = tokenizer.encode(text, add_special_tokens=False)
|
| 45 |
chunks = []
|
| 46 |
for i in range(0, len(tokens), CHUNK_SIZE):
|
|
@@ -49,15 +45,11 @@ def chunk_text(text: str):
|
|
| 49 |
chunks.append(chunk_text)
|
| 50 |
return chunks
|
| 51 |
|
| 52 |
-
|
| 53 |
def summarize_long_text(text: str) -> str:
|
| 54 |
-
"""Summarize arbitrarily long text safely."""
|
| 55 |
if not text or len(text.strip()) == 0:
|
| 56 |
return "No text provided."
|
| 57 |
-
|
| 58 |
chunks = chunk_text(text)
|
| 59 |
summaries = []
|
| 60 |
-
|
| 61 |
for chunk in chunks:
|
| 62 |
summary = summarizer(
|
| 63 |
chunk,
|
|
@@ -66,13 +58,10 @@ def summarize_long_text(text: str) -> str:
|
|
| 66 |
do_sample=False
|
| 67 |
)[0]["summary_text"]
|
| 68 |
summaries.append(summary)
|
| 69 |
-
|
| 70 |
merged = " ".join(summaries)
|
| 71 |
return clean_text(merged)
|
| 72 |
|
| 73 |
-
|
| 74 |
def read_pdf(file) -> str:
|
| 75 |
-
"""Safely extract text from PDF."""
|
| 76 |
try:
|
| 77 |
reader = PdfReader(file)
|
| 78 |
pages = [page.extract_text() or "" for page in reader.pages]
|
|
@@ -80,7 +69,6 @@ def read_pdf(file) -> str:
|
|
| 80 |
except Exception as e:
|
| 81 |
return f"PDF read error: {e}"
|
| 82 |
|
| 83 |
-
|
| 84 |
# =========================
|
| 85 |
# Main handler
|
| 86 |
# =========================
|
|
@@ -89,9 +77,8 @@ def process_input(text, file):
|
|
| 89 |
text = read_pdf(file)
|
| 90 |
return summarize_long_text(text)
|
| 91 |
|
| 92 |
-
|
| 93 |
# =========================
|
| 94 |
-
# Custom theme + CSS
|
| 95 |
# =========================
|
| 96 |
custom_theme = gr.themes.Default(
|
| 97 |
primary_hue="blue",
|
|
@@ -103,32 +90,61 @@ custom_theme = gr.themes.Default(
|
|
| 103 |
body_background_fill_dark="#ffffff",
|
| 104 |
block_background_fill="#ffffff",
|
| 105 |
block_background_fill_dark="#ffffff",
|
| 106 |
-
button_primary_background_fill="#2563eb",
|
| 107 |
-
button_primary_background_fill_hover="#1d4ed8",
|
| 108 |
button_primary_text_color="#ffffff",
|
| 109 |
button_primary_border_color="#2563eb",
|
|
|
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
-
|
| 113 |
custom_css = """
|
| 114 |
-
.gradio-container {
|
| 115 |
background-color: #ffffff !important;
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
|
| 124 |
.gr-button-primary {
|
|
|
|
| 125 |
border-radius: 8px !important;
|
| 126 |
font-weight: 600 !important;
|
| 127 |
}
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
}
|
| 133 |
"""
|
| 134 |
|
|
@@ -138,13 +154,11 @@ h1 {
|
|
| 138 |
with gr.Blocks(theme=custom_theme, css=custom_css) as demo:
|
| 139 |
gr.Markdown(
|
| 140 |
"# π Long Text Summarizer (Free-Tier Safe)",
|
| 141 |
-
elem_classes=["pb-2"]
|
| 142 |
)
|
| 143 |
gr.Markdown(
|
| 144 |
"β’ Handles **thousands of words** \n"
|
| 145 |
"β’ Supports **PDF upload** \n"
|
| 146 |
"β’ Optimized for **CPU / free tier**",
|
| 147 |
-
elem_classes=["text-gray-600", "text-sm", "mb-6"]
|
| 148 |
)
|
| 149 |
|
| 150 |
with gr.Row():
|
|
@@ -160,7 +174,7 @@ with gr.Blocks(theme=custom_theme, css=custom_css) as demo:
|
|
| 160 |
file_types=[".pdf"],
|
| 161 |
)
|
| 162 |
|
| 163 |
-
summarize_btn = gr.Button("Summarize", variant="primary"
|
| 164 |
|
| 165 |
output = gr.Textbox(
|
| 166 |
lines=10,
|
|
@@ -174,6 +188,4 @@ with gr.Blocks(theme=custom_theme, css=custom_css) as demo:
|
|
| 174 |
outputs=output
|
| 175 |
)
|
| 176 |
|
| 177 |
-
|
| 178 |
-
demo.launch()
|
| 179 |
-
# demo.launch(server_name="0.0.0.0") # β use this on HF Spaces if needed
|
|
|
|
| 15 |
device=-1 # CPU only
|
| 16 |
)
|
| 17 |
|
|
|
|
| 18 |
CHUNK_SIZE = 900 # safe margin
|
| 19 |
|
| 20 |
# =========================
|
| 21 |
+
# Utilities (unchanged)
|
| 22 |
# =========================
|
| 23 |
def clean_text(text: str) -> str:
|
|
|
|
| 24 |
text = text.replace("β", "'").replace("β", "'")
|
| 25 |
text = text.replace("β", '"').replace("β", '"')
|
| 26 |
text = re.sub(r"[.]{2,}", ".", text)
|
|
|
|
| 36 |
result.append(s.strip())
|
| 37 |
return " ".join(result)
|
| 38 |
|
|
|
|
| 39 |
def chunk_text(text: str):
|
|
|
|
| 40 |
tokens = tokenizer.encode(text, add_special_tokens=False)
|
| 41 |
chunks = []
|
| 42 |
for i in range(0, len(tokens), CHUNK_SIZE):
|
|
|
|
| 45 |
chunks.append(chunk_text)
|
| 46 |
return chunks
|
| 47 |
|
|
|
|
| 48 |
def summarize_long_text(text: str) -> str:
|
|
|
|
| 49 |
if not text or len(text.strip()) == 0:
|
| 50 |
return "No text provided."
|
|
|
|
| 51 |
chunks = chunk_text(text)
|
| 52 |
summaries = []
|
|
|
|
| 53 |
for chunk in chunks:
|
| 54 |
summary = summarizer(
|
| 55 |
chunk,
|
|
|
|
| 58 |
do_sample=False
|
| 59 |
)[0]["summary_text"]
|
| 60 |
summaries.append(summary)
|
|
|
|
| 61 |
merged = " ".join(summaries)
|
| 62 |
return clean_text(merged)
|
| 63 |
|
|
|
|
| 64 |
def read_pdf(file) -> str:
|
|
|
|
| 65 |
try:
|
| 66 |
reader = PdfReader(file)
|
| 67 |
pages = [page.extract_text() or "" for page in reader.pages]
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"PDF read error: {e}"
|
| 71 |
|
|
|
|
| 72 |
# =========================
|
| 73 |
# Main handler
|
| 74 |
# =========================
|
|
|
|
| 77 |
text = read_pdf(file)
|
| 78 |
return summarize_long_text(text)
|
| 79 |
|
|
|
|
| 80 |
# =========================
|
| 81 |
+
# Custom theme + stronger CSS for black text
|
| 82 |
# =========================
|
| 83 |
custom_theme = gr.themes.Default(
|
| 84 |
primary_hue="blue",
|
|
|
|
| 90 |
body_background_fill_dark="#ffffff",
|
| 91 |
block_background_fill="#ffffff",
|
| 92 |
block_background_fill_dark="#ffffff",
|
| 93 |
+
button_primary_background_fill="#2563eb",
|
| 94 |
+
button_primary_background_fill_hover="#1d4ed8",
|
| 95 |
button_primary_text_color="#ffffff",
|
| 96 |
button_primary_border_color="#2563eb",
|
| 97 |
+
body_text_color="#111111", # β dark almost-black
|
| 98 |
+
body_text_color_dark="#111111",
|
| 99 |
)
|
| 100 |
|
|
|
|
| 101 |
custom_css = """
|
| 102 |
+
.gradio-container, body, .main, .app, .wrap {
|
| 103 |
background-color: #ffffff !important;
|
| 104 |
+
color: #000000 !important;
|
| 105 |
}
|
| 106 |
|
| 107 |
+
* {
|
| 108 |
+
color: #000000 !important; /* Force almost everything black */
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
label, .label, span, p, div, h1, h2, h3, h4, h5, h6 {
|
| 112 |
+
color: #000000 !important;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
.gr-markdown, .prose, .prose p, .prose li, .prose strong {
|
| 116 |
+
color: #111111 !important;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
input, textarea, .textbox, .gr-text-input, .gr-textarea {
|
| 120 |
+
color: #000000 !important;
|
| 121 |
+
background-color: #ffffff !important;
|
| 122 |
+
border: 1px solid #d1d5db !important;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
::placeholder {
|
| 126 |
+
color: #6b7280 !important; /* Gray placeholder β easier to read */
|
| 127 |
}
|
| 128 |
|
| 129 |
.gr-button-primary {
|
| 130 |
+
color: #ffffff !important; /* White text on blue button */
|
| 131 |
border-radius: 8px !important;
|
| 132 |
font-weight: 600 !important;
|
| 133 |
}
|
| 134 |
|
| 135 |
+
header, .gr-top, .gr-header {
|
| 136 |
+
background-color: #f8f9fa !important;
|
| 137 |
+
border-bottom: 1px solid #e5e7eb !important;
|
| 138 |
+
padding: 12px 24px !important;
|
| 139 |
+
color: #000000 !important;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
a {
|
| 143 |
+
color: #2563eb !important; /* Blue links for contrast */
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
a:hover {
|
| 147 |
+
color: #1d4ed8 !important;
|
| 148 |
}
|
| 149 |
"""
|
| 150 |
|
|
|
|
| 154 |
with gr.Blocks(theme=custom_theme, css=custom_css) as demo:
|
| 155 |
gr.Markdown(
|
| 156 |
"# π Long Text Summarizer (Free-Tier Safe)",
|
|
|
|
| 157 |
)
|
| 158 |
gr.Markdown(
|
| 159 |
"β’ Handles **thousands of words** \n"
|
| 160 |
"β’ Supports **PDF upload** \n"
|
| 161 |
"β’ Optimized for **CPU / free tier**",
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
with gr.Row():
|
|
|
|
| 174 |
file_types=[".pdf"],
|
| 175 |
)
|
| 176 |
|
| 177 |
+
summarize_btn = gr.Button("Summarize", variant="primary")
|
| 178 |
|
| 179 |
output = gr.Textbox(
|
| 180 |
lines=10,
|
|
|
|
| 188 |
outputs=output
|
| 189 |
)
|
| 190 |
|
| 191 |
+
demo.launch()
|
|
|
|
|
|