Tonic commited on
Commit
8252047
Β·
unverified Β·
1 Parent(s): ab48ce6

adds HF_TOKEN and loads the model

Browse files
Files changed (1) hide show
  1. app.py +58 -13
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import json
3
  import torch
 
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
  import spaces
6
 
@@ -12,31 +13,59 @@ model = None
12
  tokenizer = None
13
 
14
  def load_model():
15
- """Load the Osmosis Structure model and tokenizer"""
16
  global model, tokenizer
17
 
18
  try:
19
  print("Loading Osmosis Structure model...")
20
 
21
- # Load tokenizer
 
 
 
 
 
 
 
 
 
 
22
  tokenizer = AutoTokenizer.from_pretrained(
23
  MODEL_NAME,
24
- trust_remote_code=True
 
 
25
  )
26
 
27
- # Load model
 
28
  model = AutoModelForCausalLM.from_pretrained(
29
  MODEL_NAME,
30
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
31
  device_map="auto" if torch.cuda.is_available() else None,
32
- trust_remote_code=True
 
 
33
  )
34
 
35
  print("βœ… Osmosis Structure model loaded successfully!")
36
  return True
37
 
38
  except Exception as e:
39
- print(f"❌ Error loading model: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  return False
41
 
42
  @spaces.GPU
@@ -45,7 +74,7 @@ def text_to_json(input_text, max_tokens=512, temperature=0.6, top_p=0.95, top_k=
45
  global model, tokenizer
46
 
47
  if model is None or tokenizer is None:
48
- return "❌ Model not loaded. Please wait for model initialization."
49
 
50
  try:
51
  # Create a structured prompt for JSON conversion
@@ -150,8 +179,10 @@ def create_demo():
150
 
151
  Convert unstructured text into well-formatted JSON using the Osmosis Structure 0.6B model.
152
  This model is specifically trained for structured data extraction and format conversion.
 
 
153
  """)
154
-
155
  gr.Markdown("""
156
  ### ℹ️ About Osmosis Structure
157
 
@@ -159,8 +190,16 @@ def create_demo():
159
  - **Architecture**: Qwen3 (specialized for structured data)
160
  - **Purpose**: Converting unstructured text to structured JSON format
161
  - **Optimizations**: Fine-tuned for data extraction and format conversion tasks
 
162
 
163
  The model automatically identifies key information in your text and organizes it into logical JSON structures.
 
 
 
 
 
 
 
164
  """)
165
 
166
  with gr.Row():
@@ -237,6 +276,7 @@ def create_demo():
237
  label="Click on any example to try it"
238
  )
239
 
 
240
  # Event handlers
241
  convert_btn.click(
242
  fn=text_to_json,
@@ -259,17 +299,22 @@ def create_demo():
259
  if __name__ == "__main__":
260
  print("🌊 Initializing Osmosis Structure Demo...")
261
 
 
 
 
 
 
 
 
262
  # Load model at startup
263
  if load_model():
264
  print("πŸš€ Creating Gradio interface...")
265
  demo = create_demo()
266
  demo.launch(
267
- share=True,
268
  show_error=True,
269
  show_tips=True,
270
- enable_queue=True,
271
- ssr_mode=False,
272
- mcp_server=True
273
  )
274
  else:
275
- print("❌ Failed to load model. Please check your setup.")
 
1
  import gradio as gr
2
  import json
3
  import torch
4
+ import os
5
  from transformers import AutoModelForCausalLM, AutoTokenizer
6
  import spaces
7
 
 
13
  tokenizer = None
14
 
15
  def load_model():
16
+ """Load the Osmosis Structure model and tokenizer with HF token for gated repos"""
17
  global model, tokenizer
18
 
19
  try:
20
  print("Loading Osmosis Structure model...")
21
 
22
+ # Get HF token from environment variables
23
+ hf_token = os.environ.get("HF_KEY")
24
+ if not hf_token:
25
+ print("⚠️ Warning: HF_KEY not found in environment variables")
26
+ print("Attempting to load without token...")
27
+ hf_token = None
28
+ else:
29
+ print("βœ… HF token found, accessing gated repository...")
30
+
31
+ # Load tokenizer with token
32
+ print("Loading tokenizer...")
33
  tokenizer = AutoTokenizer.from_pretrained(
34
  MODEL_NAME,
35
+ trust_remote_code=True,
36
+ token=hf_token,
37
+ use_auth_token=hf_token # Backward compatibility
38
  )
39
 
40
+ print("Loading model...")
41
+ # Load model with token
42
  model = AutoModelForCausalLM.from_pretrained(
43
  MODEL_NAME,
44
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
45
  device_map="auto" if torch.cuda.is_available() else None,
46
+ trust_remote_code=True,
47
+ token=hf_token,
48
+ use_auth_token=hf_token # Backward compatibility
49
  )
50
 
51
  print("βœ… Osmosis Structure model loaded successfully!")
52
  return True
53
 
54
  except Exception as e:
55
+ error_msg = f"❌ Error loading model: {e}"
56
+ print(error_msg)
57
+
58
+ # Provide helpful error messages for common issues
59
+ if "401" in str(e) or "authentication" in str(e).lower():
60
+ print("πŸ’‘ This appears to be an authentication error.")
61
+ print("Please ensure:")
62
+ print("1. HF_KEY is set correctly in your Space secrets")
63
+ print("2. Your token has access to the gated repository")
64
+ print("3. You have accepted the model's license agreement")
65
+ elif "404" in str(e) or "not found" in str(e).lower():
66
+ print("πŸ’‘ Model repository not found.")
67
+ print("Please check if the model name is correct and accessible")
68
+
69
  return False
70
 
71
  @spaces.GPU
 
74
  global model, tokenizer
75
 
76
  if model is None or tokenizer is None:
77
+ return "❌ Model not loaded. Please check the console for loading errors."
78
 
79
  try:
80
  # Create a structured prompt for JSON conversion
 
179
 
180
  Convert unstructured text into well-formatted JSON using the Osmosis Structure 0.6B model.
181
  This model is specifically trained for structured data extraction and format conversion.
182
+
183
+ > **Note**: This model requires authentication. Ensure your HF_KEY is properly configured in Space secrets.
184
  """)
185
+
186
  gr.Markdown("""
187
  ### ℹ️ About Osmosis Structure
188
 
 
190
  - **Architecture**: Qwen3 (specialized for structured data)
191
  - **Purpose**: Converting unstructured text to structured JSON format
192
  - **Optimizations**: Fine-tuned for data extraction and format conversion tasks
193
+ - **Access**: Requires HF authentication token for gated repository
194
 
195
  The model automatically identifies key information in your text and organizes it into logical JSON structures.
196
+
197
+ ### πŸ” Authentication Setup
198
+
199
+ To use this model, ensure you have:
200
+ 1. Set `HF_KEY` in your Space secrets with a valid Hugging Face token
201
+ 2. Accepted the model's license agreement on Hugging Face
202
+ 3. Ensured your token has access to the gated repository
203
  """)
204
 
205
  with gr.Row():
 
276
  label="Click on any example to try it"
277
  )
278
 
279
+
280
  # Event handlers
281
  convert_btn.click(
282
  fn=text_to_json,
 
299
  if __name__ == "__main__":
300
  print("🌊 Initializing Osmosis Structure Demo...")
301
 
302
+ # Check HF token availability
303
+ hf_token = os.environ.get("HF_KEY")
304
+ if hf_token:
305
+ print("βœ… HF_KEY found in environment")
306
+ else:
307
+ print("⚠️ HF_KEY not found - this may cause issues with gated repositories")
308
+
309
  # Load model at startup
310
  if load_model():
311
  print("πŸš€ Creating Gradio interface...")
312
  demo = create_demo()
313
  demo.launch(
314
+ share=False,
315
  show_error=True,
316
  show_tips=True,
317
+ enable_queue=True
 
 
318
  )
319
  else:
320
+ print("❌ Failed to load model. Please check your HF_KEY and model access permissions.")