jejunepixels commited on
Commit
5ecb30e
·
verified ·
1 Parent(s): 69ff5a5

Create hub_ui.py

Browse files
Files changed (1) hide show
  1. hub_ui.py +91 -0
hub_ui.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Hugging Face loading screen, keeping SDK download work off the UI thread."""
2
+ from concurrent.futures import ThreadPoolExecutor
3
+ from queue import Empty, Queue
4
+
5
+ import streamlit as st
6
+
7
+ from hub_models import build_plan, discover_models, download_model, format_size, friendly_hub_error
8
+ from hub_models import CACHE_ROOT
9
+ from app_environment import hosted_mode, hosted_download_limit, new_session_directory, PRIVATE_CACHE_ROOT
10
+
11
+
12
+ def render_hub_loader(activate_model):
13
+ st.caption('Paste a model repository ID or Hugging Face URL, then choose its weights.')
14
+ repo = st.text_input('Hugging Face repository or URL', placeholder='owner/model or https://huggingface.co/owner/model', key='hf_repo')
15
+ with st.expander('Revision & access token (optional)'):
16
+ revision = st.text_input('Revision', placeholder='main, branch, tag or commit', key='hf_revision')
17
+ token = st.text_input('Hugging Face read token', type='password', key='hf_token',
18
+ help='For private or gated models. Sent to this app server and Hugging Face for this session; it is not saved as a login.')
19
+ identity = (repo.strip(), revision.strip())
20
+ if st.button('Find model files', key='hf_find_files', type='primary', disabled=not repo.strip(), use_container_width=True):
21
+ st.session_state.pop('hf_catalog', None)
22
+ try:
23
+ with st.spinner('Reading Hugging Face repository…'):
24
+ st.session_state.hf_catalog = discover_models(repo, revision, token)
25
+ st.session_state.hf_catalog_identity = identity
26
+ except Exception as error:
27
+ st.error(friendly_hub_error(error, token))
28
+ catalog = st.session_state.get('hf_catalog')
29
+ if catalog is None or st.session_state.get('hf_catalog_identity') != identity:
30
+ return
31
+ st.link_button('View repository on Hugging Face', f'https://huggingface.co/{catalog.repo_id}')
32
+ if not catalog.choices:
33
+ st.info('No supported weights found. Choose a single-file GGUF, standalone safetensors, or a safetensors index. Split GGUF and PyTorch .bin files are not supported.')
34
+ return
35
+ preferred = catalog.choices.index(catalog.preferred) if catalog.preferred else 0
36
+ selection = st.selectbox('Model weights', catalog.choices, index=preferred,
37
+ key=f'hf_weights:{catalog.repo_id}:{catalog.commit}:{catalog.preferred}')
38
+ private = hosted_mode() and bool(token)
39
+ if private and 'private_hub_cache' not in st.session_state:
40
+ st.session_state.private_hub_cache = new_session_directory(PRIVATE_CACHE_ROOT)
41
+ cache_root = st.session_state.private_hub_cache if private else CACHE_ROOT
42
+ plan_key = (catalog.repo_id, catalog.commit, selection, str(cache_root))
43
+ plans = st.session_state.setdefault('hf_plans', {})
44
+ try:
45
+ if plan_key not in plans:
46
+ with st.spinner('Checking required model files…'):
47
+ plans[plan_key] = build_plan(catalog, selection, token, cache_root=cache_root)
48
+ plan = plans[plan_key]
49
+ except Exception as error:
50
+ st.error(friendly_hub_error(error, token))
51
+ return
52
+ st.caption(f'{len(plan.weight_files)} weights file(s) · {format_size(plan.size)} total · revision {catalog.commit[:12]}')
53
+ if plan.embeddings_only:
54
+ st.caption('Only shards containing recognised token embeddings will be downloaded.')
55
+ elif selection.endswith('.index.json'):
56
+ st.caption('Embedding names were not recognised in this index; all listed weight shards will be downloaded.')
57
+ with st.expander('Files to download'):
58
+ for filename in plan.files:
59
+ st.text(f'{filename} · {format_size(catalog.files.get(filename))}')
60
+ limit = hosted_download_limit()
61
+ if limit is not None and (plan.size is None or plan.size > limit):
62
+ st.info('This hosted Space supports downloads up to 4 GB with a known size. Choose smaller weights, upload embedding shards, or run the app locally for larger models.')
63
+ return
64
+ if st.button('Download & load', key='hf_download', type='primary', use_container_width=True):
65
+ events = Queue()
66
+ progress = st.progress(0, text='Preparing download…')
67
+ try:
68
+ with ThreadPoolExecutor(max_workers=1) as executor:
69
+ future = executor.submit(download_model, plan, token, lambda done, text: events.put((done, text)))
70
+ while not future.done():
71
+ try:
72
+ done, text = events.get(timeout=.15)
73
+ # Drop outdated progress events without touching Streamlit on a worker.
74
+ while not events.empty():
75
+ done, text = events.get_nowait()
76
+ progress.progress(done, text=text)
77
+ except Empty:
78
+ pass
79
+ model_path, tokenizer_path = future.result()
80
+ progress.progress(1., text='Download complete. Reading embeddings…')
81
+ activate_model(model_path, tokenizer_path, f'{catalog.repo_id} · {selection}')
82
+ st.session_state.clear_hub_token = True
83
+ st.rerun()
84
+ except Exception as error:
85
+ st.error(friendly_hub_error(error, token))
86
+ finally:
87
+ progress.empty()
88
+ if private:
89
+ st.caption('Token-authenticated downloads use a private session folder, removed when you clear the model. The Space owner operates this server.')
90
+ else:
91
+ st.caption('Downloads are cached and reused. Clearing a model keeps public downloaded files. Space restarts may clear the cache.' if hosted_mode() else 'Downloads are cached in this project’s .model-cache folder and reused. Clearing a model keeps its downloaded files.')