Nottybro commited on
Commit
08b8e5d
Β·
verified Β·
1 Parent(s): 08b60f5

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import json, time, threading, requests
3
+ import gradio as gr
4
+
5
+ WORKER_URL = 'https://Nottybro-pmc-worker.hf.space'
6
+ POLL_SECS = 60
7
+
8
+ all_sessions = {}
9
+ worker_health = {'last_ok': None, 'failures': 0}
10
+ state_lock = threading.Lock()
11
+
12
+ def poll():
13
+ while True:
14
+ try:
15
+ r = requests.post(f'{WORKER_URL}/api/predict',
16
+ json={'fn_index': 0, 'data': []}, timeout=25)
17
+ r.raise_for_status()
18
+ parsed = json.loads(r.json()['data'][0])
19
+ with state_lock:
20
+ all_sessions.update(parsed)
21
+ worker_health['last_ok'] = time.strftime('%H:%M:%S UTC', time.gmtime())
22
+ worker_health['failures'] = 0
23
+ except Exception as e:
24
+ with state_lock:
25
+ worker_health['failures'] += 1
26
+ print(f'Poll error: {e}')
27
+ time.sleep(POLL_SECS)
28
+
29
+ threading.Thread(target=poll, daemon=True).start()
30
+
31
+ BADGE = {'idle':'βšͺ','pushing':'πŸ”„','queued':'🟑','inQueue':'🟑',
32
+ 'running':'🟒','complete':'βœ…','done':'🏁',
33
+ 'error':'πŸ”΄','push_failed':'πŸ”΄'}
34
+ def _b(st):
35
+ for k,v in BADGE.items():
36
+ if k in st: return f'{v} {st}'
37
+ return st
38
+
39
+ def sess_table():
40
+ with state_lock:
41
+ if not all_sessions:
42
+ return [['Waiting for worker data...']]
43
+ return [[
44
+ s['session_id'], s.get('username','')[:16],
45
+ _b(s.get('status','?')), s.get('run_number',0), s.get('success',0),
46
+ f'p{s.get("resume_page",0)}β†’p{s.get("slice_end",0)}',
47
+ (s.get('current_url') or 'β€”')[:55],
48
+ (s.get('last_check') or 'β€”')[-8:],
49
+ ] for _,s in sorted(all_sessions.items(), key=lambda x: int(x[0]))]
50
+
51
+ def summary():
52
+ with state_lock:
53
+ if not all_sessions:
54
+ return '⏳ Waiting for first poll from worker...'
55
+ vals = list(all_sessions.values())
56
+ total = len(vals)
57
+ done = sum(1 for s in vals if s.get('complete'))
58
+ run = sum(1 for s in vals if 'running' in s.get('status',''))
59
+ q = sum(1 for s in vals if 'queue' in s.get('status','').lower())
60
+ ok = sum(s.get('success',0) for s in vals)
61
+ runs = sum(s.get('run_number',0) for s in vals)
62
+ tp = sum(s.get('slice_end',0)-s.get('slice_start',0)+1 for s in vals)
63
+ dp = sum(max(0, s.get('resume_page',0)-s.get('slice_start',0)) for s in vals)
64
+ pct = dp/max(tp,1)*100
65
+ bar = 'β–ˆ'*int(pct/5) + 'β–‘'*(20-int(pct/5))
66
+ cf = worker_health['failures']
67
+ last = worker_health['last_ok'] or 'never'
68
+ return (
69
+ f'### PMC Master Dashboard\n'
70
+ f'`[{bar}] {pct:.1f}%` pages done\n\n'
71
+ f'**Sessions:** {done}/{total} complete | '
72
+ f'🟒 {run} running | 🟑 {q} queued | '
73
+ f'βœ… **{ok:,} builds** | πŸ”„ {runs} kernel runs\n\n'
74
+ f'Worker last seen: `{last}` | '
75
+ f'{"⚠️ Worker unreachable!" if cf>=3 else "Worker OK"}'
76
+ )
77
+
78
+ def all_links():
79
+ with state_lock:
80
+ lines=[]
81
+ for _,s in sorted(all_sessions.items(), key=lambda x: int(x[0])):
82
+ for h in s.get('history',[]):
83
+ lines.append(f's{s["session_id"]} r{h["run_number"]} '
84
+ f'p{h.get("resume_page","?")} | {h["url"]}')
85
+ return '\n'.join(lines) or 'No data yet.'
86
+
87
+ R = 60
88
+ with gr.Blocks(title='PMC Master') as app:
89
+ gr.Markdown('# PMC Distributed Scraper β€” Master Dashboard')
90
+ gr.Markdown(value=summary, every=R)
91
+ gr.Dataframe(
92
+ headers=['Sess','Account','Status','Runs','Built',
93
+ 'Progress','Current notebook','Last check'],
94
+ value=sess_table, every=R, interactive=False, wrap=True)
95
+ with gr.Tabs():
96
+ with gr.Tab('All notebook links'):
97
+ gr.Textbox(value=all_links, every=R, lines=35,
98
+ interactive=False, label='Every Kaggle notebook URL')
99
+ gr.Markdown(f'*Polls Worker every {POLL_SECS}s. Worker: [{WORKER_URL}]({WORKER_URL})*')
100
+
101
+ app.launch()