Spaces:
Sleeping
Sleeping
hanhaonan02 Claude Fable 5.1 commited on
Commit ·
033094c
1
Parent(s): 77e6466
One request per ZeroGPU step; keep small-caps titles intact
Browse files- The ZeroGPU proxy token is bound to the HTTP request; parsing plus several 120 s steps in one
request failed with 'Expired ZeroGPU proxy token'. Generate is now a chain: /analyze (CPU) then
up to 10 chained /step events, one GPU step each; Continue re-enters the chain.
- Title extraction joins touching spans and takes whole title lines, so ICLR small-caps titles no
longer collapse to initials; falls back to PDF metadata when the result looks broken.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Files changed (3)
- README.md+2
- demo_ui.py+53-38
- pdf_pipeline.py+16-5
20 unmodified lines212223242526272820 unmodified lines- Model: RL checkpoint-240, BF16, no quantization, 16K context.- Generation runs in ZeroGPU steps of up to 120 s; a full poster usually takes 3–6 steps and the demo chains them automatically. Sign in to Hugging Face to use your GPU quota.- API: call `/analyze` once (PDF + settings), then `/step` repeatedly until the status starts with `Done`, `Paused` or `Stopped`; each step is its own request so the ZeroGPU token stays valid.- The examples on the page were generated by the same checkpoint with the default instruction and settings.- Research demo: scanned PDFs are not supported, uploads are temporary, and every statement, number and caption should be checked before a poster is used.13 unmodified lines141516171718192072 unmodified lines939495969798991001011021031041051061071081091101111121131141151161171181191209697989910012212310110210310412610510610710810913113213313411011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314413614514614713914014814915015144 unmodified lines19619719819119219920020120220320420520620720820921013 unmodified linesROOT = Path(__file__).resolve().parentARTIFACTS = Path(tempfile.mkdtemp(prefix='rap_demo_'))WINDOW = 16384MAX_CHUNKS = 8MAX_STEPS = 10 # ZeroGPU steps of <=120 s per run
CSS = '''.gradio-container {max-width: 1360px !important; margin: 0 auto;}72 unmodified lines del inputs, output return result
def generate_loop(p, gallery, progress): """ZeroGPU chunks until the HTML is complete; yields (state, status, png, html, zip, gallery, spec, raw, continue-button).""" tokens = '' for k in range(1, MAX_CHUNKS + 1): progress(min(.15 + .1 * k, .9), desc=f'Generating poster HTML (step {k})…') try: r = gpu_chunk(p) except Exception as e: # noqa: BLE001 (quota exhausted, timeout, …) yield (p, f"Paused after step {k - 1}: {str(e)[:200]}", None, None, None, gallery, p['spec'], p['raw'], gr.update(visible=bool(p.get('continuation_ids')))) return p = dict(p, continuation_ids=r['ids'], raw=r['raw'], done=r['done']) tokens = f"{len(r['ids']):,} tokens · {r['new_tokens'] / max(r['seconds'], .01):.0f} tok/s" if r['complete'] or p['done']: break yield (p, f"Step {k} done · {tokens} · continuing…", None, None, None, gallery, p['spec'], p['raw'], gr.update(visible=False)) progress(.95, desc='Rendering…') try: png, html, archive, meta = render(p['raw'], p) except Exception as e: # noqa: BLE001 yield (p, f"The model stopped before a complete poster ({str(e)[:160]}).", None, None, None, gallery, p['spec'], p['raw'], gr.update(visible=not p.get('done'))) return note = '' if meta['complete_html'] else ' · output incomplete' yield (dict(p, done=True), f"Done · {tokens}{note}", png, fileout(html), fileout(archive), gallery, p['spec'], p['raw'], gr.update(visible=False)) HIDE = gr.update(value=None, visible=False)
def gallery_of(p): return [(a['thumb'], f"{a['id']} · page {a['page']}") for a in p['assets']]
def run(pdf, instruction, canvas, accent, columns, custom_spec, progress=gr.Progress()): """One click: parse the PDF, then generate until the poster is complete.""" def analyze_evt(pdf, instruction, canvas, accent, columns, custom_spec, progress=gr.Progress()): """Request 1 of a run: parse the PDF on CPU. GPU steps run in their own requests (see step).""" if not pdf: raise gr.Error('Upload a paper PDF first.') progress(.05, desc='Reading the paper (LayoutLMv3 on CPU)…') progress(.1, desc='Reading the paper (LayoutLMv3 on CPU, up to a few minutes)…') try: p = analyze(pdf, instruction, canvas, accent, columns, custom_spec) except (ValueError, RuntimeError) as e: raise gr.Error(str(e)) from None gallery = [(a['thumb'], f"{a['id']} · page {a['page']}") for a in p['assets']] yield (p, f"Parsed **{p['title'][:90]}** · {len(p['assets'])} figures/tables found · generating…", None, None, None, gallery, p['spec'], '', gr.update(visible=False)) yield from generate_loop(p, gallery, progress) p = dict(p, step=0, paused=False) return (p, f"Parsed **{p['title'][:90]}** · {len(p['assets'])} figures/tables found · generating (step 1)…", None, HIDE, HIDE, gallery_of(p), p['spec'], '', gr.update(visible=False))
def step(p, progress=gr.Progress()): """One ZeroGPU step per request: the ZeroGPU proxy token is tied to the request and expires after a few minutes, so a single long request (parse + several 120 s steps) fails with 'Expired ZeroGPU proxy token'.""" if not p or p.get('done') or p.get('paused'): return tuple(gr.skip() for _ in range(9)) k = p.get('step', 0) + 1 gallery = gallery_of(p) if k > MAX_STEPS: return (dict(p, paused=True), f"Stopped after {MAX_STEPS} steps without a complete poster.", None, HIDE, HIDE, gallery, p['spec'], p['raw'], gr.update(visible=True)) progress(min(.1 + .12 * k, .9), desc=f'Generating poster HTML (step {k})…') try: r = gpu_chunk(p) except Exception as e: # noqa: BLE001 (quota exhausted, timeout, …) return (dict(p, paused=True), f"Paused after step {k - 1}: {str(e)[:200]}", None, HIDE, HIDE, gallery, p['spec'], p['raw'], gr.update(visible=bool(p.get('continuation_ids')))) p = dict(p, continuation_ids=r['ids'], raw=r['raw'], done=r['done'], step=k) tokens = f"{len(r['ids']):,} tokens · {r['new_tokens'] / max(r['seconds'], .01):.0f} tok/s" if not (r['complete'] or p['done']): return (p, f"Step {k} done · {tokens} · continuing (step {k + 1})…", None, HIDE, HIDE, gallery, p['spec'], p['raw'], gr.update(visible=False)) progress(.95, desc='Rendering…') try: png, html, archive, meta = render(p['raw'], p) except Exception as e: # noqa: BLE001 return (dict(p, paused=True), f"The model stopped before a complete poster ({str(e)[:160]}).", None, HIDE, HIDE, gallery, p['spec'], p['raw'], gr.update(visible=not p.get('done'))) note = '' if meta['complete_html'] else ' · output incomplete' return (dict(p, done=True), f"Done · {tokens}{note}", png, fileout(html), fileout(archive), gallery, p['spec'], p['raw'], gr.update(visible=False))
def cont(p, progress=gr.Progress()): def resume(p): if not p or p.get('done'): raise gr.Error('Nothing to continue. Click Generate poster.') gallery = [(a['thumb'], f"{a['id']} · page {a['page']}") for a in p['assets']] yield from generate_loop(p, gallery, progress) return dict(p, paused=False)
examples = load_examples() def pick_example(evt: gr.SelectData):44 unmodified lines '[Source and third-party notices](https://huggingface.co/spaces/VincentHancoder/RAP/tree/main).') state = gr.State() outs = [state, status, poster, html_download, bundle, gallery, spec_view, code, cont_btn] run_btn.click(run, [pdf, instruction, canvas, accent, columns, custom_spec], outs, api_name='generate', concurrency_limit=1) cont_btn.click(cont, [state], outs, api_name='continue', concurrency_limit=1) # Chain: one request for parsing, then one request per GPU step (fresh ZeroGPU token each time). # API clients: call /analyze once, then /step until the status starts with "Done", "Paused" or "Stopped". ev = run_btn.click(analyze_evt, [pdf, instruction, canvas, accent, columns, custom_spec], outs, api_name='analyze', concurrency_limit=1) for k in range(MAX_STEPS): ev = ev.then(step, [state], outs, api_name='step' if k == 0 else False, concurrency_limit=1) ev = cont_btn.click(resume, [state], [state], api_name=False) for _ in range(MAX_STEPS): ev = ev.then(step, [state], outs, api_name=False, concurrency_limit=1) if examples: ex_gallery.select(pick_example, None, [pdf, instruction, canvas, accent, columns, status, poster, html_download, bundle], api_name=False) return demo, ARTIFACTS67 unmodified lines68697071727374757172737475767778798081828384858687888967 unmodified lines chosen=select_for_prompt(registry['assets'],[],16,{'figure':10,'table':5,'formula':3}) with fitz.open(pdf) as doc: first=doc[0].get_text('dict') spans=[s for b in first['blocks'] if 'lines' in b for l in b['lines'] if abs(l.get('dir',(1,0))[1])<.1 for s in l['spans'] if s['text'].strip() and not re.search(r'arxiv:|preprint|under review',s['text'],re.I)] largest=max((s['size'] for s in spans),default=0) title=clean_text(' '.join(s['text'] for s in spans if s['size']>=largest*.85 and s['bbox'][1]<doc[0].rect.height*.45))[:400] lines=[[s for s in l['spans'] if s['text'].strip() and not re.search(r'arxiv:|preprint|under review',s['text'],re.I)] for b in first['blocks'] if 'lines' in b for l in b['lines'] if abs(l.get('dir',(1,0))[1])<.1] lines=[l for l in lines if l] largest=max((s['size'] for l in lines for s in l),default=0) # whole lines whose biggest span is title-sized: keeps small-caps titles (ICLR) intact instead of 'B F G L' def join_line(l): # small-caps titles come as 'B'+'AYESIAN' spans: no space when the spans touch out='' for prev,s in zip([None]+l[:-1],l): gap=s['bbox'][0]-prev['bbox'][2] if prev else 0 out+=('' if prev and gap<s['size']*.15 and not out.endswith(' ') and not s['text'].startswith(' ') else ' ')+s['text'] return out title=clean_text(' '.join(join_line(l) for l in lines if max(s['size'] for s in l)>=largest*.85 and l[0]['bbox'][1]<doc[0].rect.height*.45))[:400] meta_title=clean_text(doc.metadata.get('title','')) if meta_title and len(meta_title)>8 and (len(title)<8 or len(title.split())>3 and sum(len(w) for w in title.split())/len(title.split())<2.5): title=meta_title[:400] authors=clean_text(doc.metadata.get('author',''))[:500] asset_lines=[];assets=[] for a in chosen:README.md+2
demo_ui.py+53-38
pdf_pipeline.py+16-5