Spaces:
Sleeping
Sleeping
jedick
commited on
Commit
Β·
cc65797
1
Parent(s):
50f75fc
Add special random button
Browse files- app.py +41 -7
- app_functions.py +106 -0
app.py
CHANGED
|
@@ -19,15 +19,16 @@ from app_functions import (
|
|
| 19 |
_run_heuristic_classifier,
|
| 20 |
_run_fewshot_classifier,
|
| 21 |
_run_judge,
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
|
| 25 |
-
def start_parent_span(
|
| 26 |
"""
|
| 27 |
Start a parent span and return the context for propagation to children.
|
| 28 |
See https://logfire.pydantic.dev/docs/how-to-guides/distributed-tracing/#manual-context-propagation
|
| 29 |
"""
|
| 30 |
-
span_name = f"{
|
| 31 |
with logfire.span(span_name) as span:
|
| 32 |
span.__enter__()
|
| 33 |
context = logfire.get_context()
|
|
@@ -93,15 +94,15 @@ with gr.Blocks(title="Noteworthy Differences") as demo:
|
|
| 93 |
</colgroup>
|
| 94 |
<tr>
|
| 95 |
<td>
|
| 96 |
-
<i class="fa-brands fa-wikipedia-w"></i> Compare current and old revisions of a Wikipedia article
|
| 97 |
-
π
You choose the number of revisions or days behind
|
| 98 |
</td>
|
| 99 |
<td>
|
| 100 |
-
β β΄ β Two classifier models and a judge predict the noteworthiness of the differences
|
| 101 |
</td>
|
| 102 |
<td>
|
| 103 |
-
<i class="fa-brands fa-github"></i> The <a href="https://github.com/jedick/noteworthy-differences">GitHub repository</a> describes how the judge was aligned with human preferences
|
| 104 |
-
π₯ The <a href="https://huggingface.co/datasets/jedick/noteworthy-differences-feedback">feedback dataset</a> holds all user feedback collected to date
|
| 105 |
</td>
|
| 106 |
</tr>
|
| 107 |
</table>
|
|
@@ -203,6 +204,17 @@ with gr.Blocks(title="Noteworthy Differences") as demo:
|
|
| 203 |
thumbs_down_btn = gr.Button(
|
| 204 |
"π Disagree", size="md", variant="primary"
|
| 205 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
# States to store boolean values
|
| 208 |
heuristic_noteworthy = gr.State()
|
|
@@ -336,6 +348,28 @@ with gr.Blocks(title="Noteworthy Differences") as demo:
|
|
| 336 |
api_name=False,
|
| 337 |
)
|
| 338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
if __name__ == "__main__":
|
| 340 |
|
| 341 |
# Setup theme without background image
|
|
|
|
| 19 |
_run_heuristic_classifier,
|
| 20 |
_run_fewshot_classifier,
|
| 21 |
_run_judge,
|
| 22 |
+
find_interesting_example,
|
| 23 |
)
|
| 24 |
|
| 25 |
|
| 26 |
+
def start_parent_span(page_title: str, number_behind: int, units_behind: str):
|
| 27 |
"""
|
| 28 |
Start a parent span and return the context for propagation to children.
|
| 29 |
See https://logfire.pydantic.dev/docs/how-to-guides/distributed-tracing/#manual-context-propagation
|
| 30 |
"""
|
| 31 |
+
span_name = f"{page_title} - {number_behind} {units_behind}"
|
| 32 |
with logfire.span(span_name) as span:
|
| 33 |
span.__enter__()
|
| 34 |
context = logfire.get_context()
|
|
|
|
| 94 |
</colgroup>
|
| 95 |
<tr>
|
| 96 |
<td>
|
| 97 |
+
<i class="fa-brands fa-wikipedia-w"></i> Compare current and old revisions of a Wikipedia article<br>
|
| 98 |
+
π
You choose the number of revisions or days behind
|
| 99 |
</td>
|
| 100 |
<td>
|
| 101 |
+
β β΄ β Two classifier models and a judge predict the noteworthiness of the differences
|
| 102 |
</td>
|
| 103 |
<td>
|
| 104 |
+
<i class="fa-brands fa-github"></i> The <a href="https://github.com/jedick/noteworthy-differences">GitHub repository</a> describes how the judge was aligned with human preferences<br>
|
| 105 |
+
π₯ The <a href="https://huggingface.co/datasets/jedick/noteworthy-differences-feedback">feedback dataset</a> holds all user feedback collected to date
|
| 106 |
</td>
|
| 107 |
</tr>
|
| 108 |
</table>
|
|
|
|
| 204 |
thumbs_down_btn = gr.Button(
|
| 205 |
"π Disagree", size="md", variant="primary"
|
| 206 |
)
|
| 207 |
+
with gr.Accordion("Find Interesting Example", open=True):
|
| 208 |
+
special_random_btn = gr.Button(
|
| 209 |
+
"π² Special Random", size="md", variant="secondary"
|
| 210 |
+
)
|
| 211 |
+
gr.Markdown(
|
| 212 |
+
"""
|
| 213 |
+
*Click to find an interesting example
|
| 214 |
+
by running the model on random pages
|
| 215 |
+
until we get a confidence score that is not High,
|
| 216 |
+
up to 20 tries*"""
|
| 217 |
+
)
|
| 218 |
|
| 219 |
# States to store boolean values
|
| 220 |
heuristic_noteworthy = gr.State()
|
|
|
|
| 348 |
api_name=False,
|
| 349 |
)
|
| 350 |
|
| 351 |
+
# Special random button handler
|
| 352 |
+
special_random_btn.click(
|
| 353 |
+
fn=find_interesting_example,
|
| 354 |
+
inputs=[number_behind, units_behind],
|
| 355 |
+
outputs=[
|
| 356 |
+
page_title,
|
| 357 |
+
new_revision,
|
| 358 |
+
new_timestamp,
|
| 359 |
+
old_revision,
|
| 360 |
+
old_timestamp,
|
| 361 |
+
heuristic_noteworthy,
|
| 362 |
+
fewshot_noteworthy,
|
| 363 |
+
judge_noteworthy,
|
| 364 |
+
heuristic_rationale,
|
| 365 |
+
fewshot_rationale,
|
| 366 |
+
judge_reasoning,
|
| 367 |
+
noteworthy_text,
|
| 368 |
+
confidence_score,
|
| 369 |
+
],
|
| 370 |
+
api_name=False,
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
if __name__ == "__main__":
|
| 374 |
|
| 375 |
# Setup theme without background image
|
app_functions.py
CHANGED
|
@@ -4,6 +4,7 @@ from wiki_data_fetcher import (
|
|
| 4 |
get_wikipedia_introduction,
|
| 5 |
extract_revision_info,
|
| 6 |
get_revisions_behind,
|
|
|
|
| 7 |
)
|
| 8 |
from models import classifier, judge
|
| 9 |
import gradio as gr
|
|
@@ -259,3 +260,108 @@ def _run_judge(
|
|
| 259 |
)
|
| 260 |
|
| 261 |
return noteworthy, noteworthy_text, reasoning, confidence
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
get_wikipedia_introduction,
|
| 5 |
extract_revision_info,
|
| 6 |
get_revisions_behind,
|
| 7 |
+
get_random_wikipedia_title,
|
| 8 |
)
|
| 9 |
from models import classifier, judge
|
| 10 |
import gradio as gr
|
|
|
|
| 260 |
)
|
| 261 |
|
| 262 |
return noteworthy, noteworthy_text, reasoning, confidence
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
@logfire.instrument("π² Special Random")
|
| 266 |
+
def find_interesting_example(number_behind: int, units_behind: str):
|
| 267 |
+
"""
|
| 268 |
+
Find an interesting example by repeatedly getting random pages and running the model
|
| 269 |
+
until we find one with a confidence score that is not High, up to 20 tries.
|
| 270 |
+
"""
|
| 271 |
+
max_tries = 20
|
| 272 |
+
|
| 273 |
+
for attempt in range(max_tries):
|
| 274 |
+
# Get random page title
|
| 275 |
+
page_title = get_random_wikipedia_title()
|
| 276 |
+
if not page_title:
|
| 277 |
+
continue
|
| 278 |
+
|
| 279 |
+
gr.Info(f"Page title {attempt + 1}: {page_title}", duration=20)
|
| 280 |
+
|
| 281 |
+
try:
|
| 282 |
+
# Initialize Logfire span
|
| 283 |
+
span_name = f"{page_title} - {number_behind} {units_behind}"
|
| 284 |
+
with logfire.span(span_name):
|
| 285 |
+
|
| 286 |
+
# Fetch current revision
|
| 287 |
+
new_revision, new_timestamp = _fetch_current_revision(page_title)
|
| 288 |
+
if not new_revision:
|
| 289 |
+
continue
|
| 290 |
+
|
| 291 |
+
# Fetch previous revision
|
| 292 |
+
old_revision, old_timestamp = _fetch_previous_revision(
|
| 293 |
+
page_title, number_behind, units_behind, new_revision
|
| 294 |
+
)
|
| 295 |
+
if not old_revision:
|
| 296 |
+
continue
|
| 297 |
+
|
| 298 |
+
# Run heuristic classifier
|
| 299 |
+
heuristic_noteworthy, heuristic_rationale = _run_heuristic_classifier(
|
| 300 |
+
old_revision, new_revision
|
| 301 |
+
)
|
| 302 |
+
if heuristic_rationale is None:
|
| 303 |
+
continue
|
| 304 |
+
|
| 305 |
+
# Run few-shot classifier
|
| 306 |
+
fewshot_noteworthy, fewshot_rationale = _run_fewshot_classifier(
|
| 307 |
+
old_revision, new_revision
|
| 308 |
+
)
|
| 309 |
+
if fewshot_rationale is None:
|
| 310 |
+
continue
|
| 311 |
+
|
| 312 |
+
# Run judge
|
| 313 |
+
judge_noteworthy, noteworthy_text, judge_reasoning, confidence_score = (
|
| 314 |
+
_run_judge(
|
| 315 |
+
old_revision,
|
| 316 |
+
new_revision,
|
| 317 |
+
heuristic_noteworthy,
|
| 318 |
+
fewshot_noteworthy,
|
| 319 |
+
heuristic_rationale,
|
| 320 |
+
fewshot_rationale,
|
| 321 |
+
)
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
# Check if confidence score is not High
|
| 325 |
+
if confidence_score and confidence_score != "High":
|
| 326 |
+
# Found an interesting example
|
| 327 |
+
gr.Success(
|
| 328 |
+
"Interesting example - ready for your feedback", duration=None
|
| 329 |
+
)
|
| 330 |
+
return (
|
| 331 |
+
page_title,
|
| 332 |
+
new_revision,
|
| 333 |
+
new_timestamp,
|
| 334 |
+
old_revision,
|
| 335 |
+
old_timestamp,
|
| 336 |
+
heuristic_noteworthy,
|
| 337 |
+
fewshot_noteworthy,
|
| 338 |
+
judge_noteworthy,
|
| 339 |
+
heuristic_rationale,
|
| 340 |
+
fewshot_rationale,
|
| 341 |
+
judge_reasoning,
|
| 342 |
+
noteworthy_text,
|
| 343 |
+
confidence_score,
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
except Exception:
|
| 347 |
+
# If there's an error, continue to next attempt
|
| 348 |
+
continue
|
| 349 |
+
|
| 350 |
+
# If we get here, all 20 tries had High confidence
|
| 351 |
+
gr.Warning("No interesting examples found - try again", duration=None)
|
| 352 |
+
# Return empty values
|
| 353 |
+
return (
|
| 354 |
+
"",
|
| 355 |
+
"",
|
| 356 |
+
"",
|
| 357 |
+
"",
|
| 358 |
+
"",
|
| 359 |
+
None,
|
| 360 |
+
None,
|
| 361 |
+
None,
|
| 362 |
+
"",
|
| 363 |
+
"",
|
| 364 |
+
"",
|
| 365 |
+
"",
|
| 366 |
+
"",
|
| 367 |
+
)
|