Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
307 lines (266 loc) · 9.15 KB
/
Copy pathapp.py
File metadata and controls
307 lines (266 loc) · 9.15 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""Rewrite Rescue — diagnose and revise pasted text. Session-only, no storage."""
from pathlib import Path
import streamlit as st
from src.providers.byo import BYOAdapter
from src.providers.demo import SAMPLE_TEXT, DemoAdapter
from src.providers.errors import ProviderError
from src.services.diagnostics import diagnose
from src.services.revision import run_revision
from src.ui.output_tabs import render_output
from src.ui.report_view import diagnose_only_result, revision_result
TEXT_TYPES = [
"General prose",
"Essay / article",
"Email / message",
"Report / memo",
"Web page text",
"Script / dialogue",
"Fiction / narrative",
"Social post",
"Technical / explanatory",
"Custom",
]
REGISTERS = [
"Plain",
"Conversational",
"Formal",
"Technical",
"Essayistic",
"Narrative",
"Custom",
]
OUTPUT_MODES = ["Diagnose + revise", "Revise only", "Diagnose only"]
REVISION_STRENGTHS = ["Light", "Standard", "Heavy", "Surgical"]
DETAIL_MODES = [
"Ask me for missing details",
"Only use details already in the text",
"Leave vague lines alone",
]
STRUCTURE_MODES = [
"Keep current structure",
"Lightly reshape structure",
"Rewrite structure freely",
]
ARTIFACT_MODES = ["Off", "Safe cleanup", "Aggressive cleanup"]
ASSETS_DIR = Path(__file__).parent / "assets"
def inject_neobrutal_css() -> None:
"""Load the Neo-Brutalist skin; a missing file degrades to the base theme."""
css_path = ASSETS_DIR / "neobrutal.css"
if css_path.exists():
st.markdown(f"<style>{css_path.read_text()}</style>", unsafe_allow_html=True)
st.set_page_config(page_title="Rewrite Rescue", page_icon="🛠️", layout="wide")
inject_neobrutal_css()
st.title("Rewrite Rescue")
st.caption(
"Neutral prose naturalization: diagnose linguistic patterns, revise carefully, "
"preserve meaning, and invent nothing."
)
with st.sidebar:
st.subheader("Run configuration")
provider_name = st.radio(
"Provider",
["Demo (offline)", "BYO API endpoint"],
help=(
"Demo runs with no API call. BYO API endpoint sends the text to a "
"model endpoint you provide for live rewriting."
),
)
byo_url = ""
byo_model = ""
byo_timeout = 60
if provider_name == "Demo (offline)":
st.info(
"Demo mode is a fixed offline sanity check. It can show the app flow, "
"but it does not use style samples, register, or revision strength. "
"Use BYO API endpoint for live style-sensitive rewriting."
)
if provider_name == "BYO API endpoint":
byo_url = st.text_input(
"Base URL",
placeholder="https://api.example-provider.com/v1",
help="Use an OpenAI-compatible chat-completions base URL.",
)
byo_model = st.text_input(
"Model name",
placeholder="provider-model-name",
)
st.text_input(
"API key",
type="password",
key="byo_api_key",
help="Stored only in this Streamlit session.",
)
byo_timeout = st.number_input("Timeout seconds", 5, 300, 60)
st.divider()
text_type = st.selectbox(
"Text type",
TEXT_TYPES,
index=0,
help="Helps the live model preserve the right kind of structure and register.",
)
custom_text_type = ""
if text_type == "Custom":
custom_text_type = st.text_input("Custom text type")
output_mode = st.radio(
"Output mode",
OUTPUT_MODES,
index=0,
help=(
"Diagnose + revise shows both findings and rewritten text. Revise only hides "
"the findings. Diagnose only never rewrites."
),
)
revision_strength = st.radio(
"Revision strength",
REVISION_STRENGTHS,
index=1,
help=(
"Light = minimal edits. Standard = balanced. Heavy = more structural reshaping. "
"Surgical = only touch the flagged parts."
),
)
register = st.selectbox(
"Register",
REGISTERS,
index=0,
help="The live model uses this to keep the writing formal, plain, technical, etc.",
)
custom_register = ""
if register == "Custom":
custom_register = st.text_area("Custom register instruction", height=80)
style_sample_disabled = provider_name == "Demo (offline)"
style_sample = st.text_area(
"Style sample",
height=120,
placeholder=(
"Optional. Paste a sample whose rhythm, syntax, and stance should guide "
"the live rewrite."
),
disabled=style_sample_disabled,
help=(
"Live BYO mode uses this as an authorial profile. Demo mode ignores it "
"because no model call is made."
),
)
st.divider()
specificity_mode = st.radio(
"When the text needs a real detail",
DETAIL_MODES,
index=0,
help=(
"Ask me = show a question when the text needs a real fact. "
"Only use details already in the text = sharpen without adding anything. "
"Leave vague lines alone = preserve abstractions instead of rewriting them."
),
)
structure_mode = st.radio(
"Structure handling",
STRUCTURE_MODES,
index=1,
help=(
"Controls whether the live model keeps the paragraph/list structure or reshapes it."
),
)
artifact_cleanup = st.select_slider(
"Pasted-junk cleanup",
ARTIFACT_MODES,
value="Safe cleanup",
help=(
"Cleans obvious non-prose residue such as placeholder names, broken citation "
"markers, stray Markdown, copied interface text, or tool-added link junk."
),
)
audit_mode = st.radio(
"Audit level",
["Standard", "Strict"],
index=0,
help=(
"Strict mode asks the live model to be tougher about meaning drift "
"and overcorrection."
),
)
preserve_quotes = st.checkbox(
"Preserve quoted material",
value=True,
help="Keeps quoted text unchanged unless it contains obvious pasted junk.",
)
preserve_citations = st.checkbox(
"Preserve citations and URLs",
value=True,
help="Keeps links and citations unless cleanup safely removes obvious tool-added junk.",
)
st.checkbox(
"Do not invent specifics",
value=True,
disabled=True,
help="Always on. Missing real details become questions, not made-up facts.",
)
if st.button("Clear session", use_container_width=True):
st.session_state.clear()
st.rerun()
text = st.text_area(
"Text to review",
height=280,
placeholder="Paste English prose here…",
)
char_count = len(text)
if char_count > 30_000:
st.warning(
f"{char_count:,} characters. Long-document chunking is not part of v1; "
"shorter sections will usually produce better results."
)
elif char_count > 10_000:
st.info(f"{char_count:,} characters. Consider running long text in sections.")
elif char_count:
st.caption(f"{char_count:,} characters")
else:
st.caption("Paste text, or leave blank to run the built-in demo sample.")
config = {
"provider": provider_name,
"text_type": custom_text_type or text_type,
"output_mode": output_mode,
"revision_strength": revision_strength,
"register": custom_register or register,
"style_sample": style_sample,
"specificity_mode": specificity_mode,
"structure_mode": structure_mode,
"artifact_cleanup": artifact_cleanup,
"audit_mode": audit_mode,
"preserve_quotes": preserve_quotes,
"preserve_citations": preserve_citations,
"do_not_invent_specifics": True,
}
if st.button("Run", type="primary"):
source = text.strip() or SAMPLE_TEXT
if not text.strip():
st.info("No text pasted — running the built-in sample through the pipeline.")
try:
if output_mode == "Diagnose only":
report = diagnose(source)
result = diagnose_only_result(source, report)
else:
if provider_name == "BYO API endpoint":
if not byo_url.strip() or not byo_model.strip():
st.error("Enter a base URL and model name, or switch to Demo.")
st.stop()
adapter = BYOAdapter(
base_url=byo_url.strip(),
model=byo_model.strip(),
api_key=st.session_state.get("byo_api_key", ""),
timeout=float(byo_timeout),
)
else:
adapter = DemoAdapter()
report, revision = run_revision(source, config, adapter)
result = revision_result(source, report, revision, output_mode)
except ProviderError as err:
st.error(f"Provider error: {err}")
else:
st.session_state["last_result"] = result
if "last_result" in st.session_state:
render_output(
st.session_state["last_result"],
st.session_state["last_result"].get("changes") or [],
)
else:
st.caption("Run the app to see revised text, diagnostics, changes, and audit notes.")