Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,109 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import yaml
|
| 4 |
-
import
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Load config
|
| 10 |
-
with open('all_config.yaml', 'r') as f:
|
| 11 |
-
config = yaml.safe_load(f)
|
| 12 |
-
|
| 13 |
-
# Load checkpoint
|
| 14 |
-
checkpoint = torch.load('pytorch_model.bin', map_location='cpu')
|
| 15 |
-
|
| 16 |
-
return config, checkpoint, "✅ Model loaded successfully!"
|
| 17 |
-
except Exception as e:
|
| 18 |
-
return None, None, f"❌ Error loading model: {str(e)}"
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
**Model Purpose**: Grant Abstract Optimization
|
| 32 |
-
|
| 33 |
-
**Training Details**:
|
| 34 |
-
- Steps: 492,500 (final checkpoint)
|
| 35 |
-
- Batch Size: {config['global_batch_size']}
|
| 36 |
-
- Learning Rate: {config['lr']}
|
| 37 |
-
"""
|
| 38 |
-
return info
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
"""
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
gr.Markdown(model_info)
|
| 73 |
-
|
| 74 |
-
with gr.Tab("Test Interface"):
|
| 75 |
-
gr.Markdown("## Abstract Optimization Demo")
|
| 76 |
-
gr.Markdown("*Note: This is a demonstration interface. Full inference requires integration with the training pipeline.*")
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
optimize_btn.click(
|
| 101 |
-
fn=placeholder_inference,
|
| 102 |
-
inputs=[draft_input, grant_type],
|
| 103 |
-
outputs=output
|
| 104 |
-
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import yaml
|
| 4 |
+
import json
|
| 5 |
+
from tokenizers import Tokenizer
|
| 6 |
|
| 7 |
+
# --- 1. Load Custom Model Code ---
|
| 8 |
+
# This dynamically loads your corrected HRM source code
|
| 9 |
+
from models.hrm.hrm_act_v1 import HierarchicalReasoningModel_ACTV1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# --- 2. Load Artifacts ---
|
| 12 |
+
print("Loading artifacts...")
|
| 13 |
+
# Load the tokenizer
|
| 14 |
+
tokenizer = Tokenizer.from_file("tokenizer.json")
|
| 15 |
+
# Load the model configuration
|
| 16 |
+
with open('config.yaml', 'r') as f:
|
| 17 |
+
config_data = yaml.safe_load(f)
|
| 18 |
+
model_config = config_data['arch']
|
| 19 |
+
# Load the grant type mapping
|
| 20 |
+
with open('activity_code_map.json', 'r') as f:
|
| 21 |
+
activity_code_map = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# --- 3. Initialize the Model ---
|
| 24 |
+
print("Initializing model...")
|
| 25 |
+
# The model expects a dict, so we pass the Pydantic model's dict representation
|
| 26 |
+
# We also need to add other required keys from the root of the config
|
| 27 |
+
model_config.update({
|
| 28 |
+
'batch_size': config_data['global_batch_size'],
|
| 29 |
+
'seq_len': 512, # You may need to get this from your dataset metadata
|
| 30 |
+
'num_puzzle_identifiers': len(activity_code_map) + 1,
|
| 31 |
+
'vocab_size': tokenizer.get_vocab_size()
|
| 32 |
+
})
|
| 33 |
+
model = HierarchicalReasoningModel_ACTV1(config_dict=model_config)
|
| 34 |
+
# Load the fine-tuned weights
|
| 35 |
+
model.load_state_dict(torch.load('pytorch_model.bin', map_location='cpu'))
|
| 36 |
+
model.eval() # Set the model to evaluation mode
|
| 37 |
+
print("Model loaded successfully!")
|
| 38 |
+
|
| 39 |
+
# --- 4. Define the Inference Function ---
|
| 40 |
+
def optimize_abstract(draft_abstract, grant_type):
|
| 41 |
+
"""
|
| 42 |
+
Takes a draft abstract and grant type, runs the model, and returns the optimized text.
|
| 43 |
"""
|
| 44 |
+
if not draft_abstract or not grant_type:
|
| 45 |
+
return "Please provide both a draft abstract and a grant type."
|
| 46 |
|
| 47 |
+
try:
|
| 48 |
+
# Prepare inputs
|
| 49 |
+
tokenizer.enable_padding(length=512)
|
| 50 |
+
tokenizer.enable_truncation(max_length=512)
|
| 51 |
+
|
| 52 |
+
input_ids = tokenizer.encode(draft_abstract).ids
|
| 53 |
+
grant_type_id = activity_code_map.get(grant_type, 0) # Default to 0 if unknown
|
| 54 |
|
| 55 |
+
# Convert to PyTorch tensors
|
| 56 |
+
input_tensor = torch.tensor([input_ids], dtype=torch.long)
|
| 57 |
+
grant_tensor = torch.tensor([grant_type_id], dtype=torch.long)
|
| 58 |
+
|
| 59 |
+
# Create the batch dictionary that the model expects
|
| 60 |
+
batch = {
|
| 61 |
+
"inputs": input_tensor,
|
| 62 |
+
"puzzle_identifiers": grant_tensor,
|
| 63 |
+
# The model requires a 'labels' field, even for inference, so we provide a dummy one
|
| 64 |
+
"labels": torch.zeros_like(input_tensor)
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Run inference
|
| 68 |
+
with torch.no_grad():
|
| 69 |
+
carry = model.initial_carry(batch)
|
| 70 |
+
# The model runs in a loop; for inference, we run it for the max steps
|
| 71 |
+
for _ in range(model_config['halt_max_steps']):
|
| 72 |
+
carry, _ = model(carry=carry, batch=batch)
|
| 73 |
+
|
| 74 |
+
# Get the final logits from the carry state
|
| 75 |
+
final_logits = model.inner.lm_head(carry.inner_carry.z_H)[:, model.inner.puzzle_emb_len:]
|
| 76 |
+
predicted_ids = torch.argmax(final_logits, dim=-1).squeeze().tolist()
|
| 77 |
|
| 78 |
+
# Decode the output
|
| 79 |
+
optimized_text = tokenizer.decode(predicted_ids, skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
return optimized_text
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"An error occurred during inference: {e}")
|
| 85 |
+
return f"Error: Could not process the abstract. Details: {e}"
|
| 86 |
+
|
| 87 |
+
# --- 5. Create the Gradio Interface ---
|
| 88 |
+
grant_type_choices = list(activity_code_map.keys())
|
| 89 |
+
|
| 90 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 91 |
+
gr.Markdown("# 🚀 HRM Grant Abstract Optimizer")
|
| 92 |
+
gr.Markdown("Enter a draft abstract and select the grant type to get a version optimized by the fine-tuned Hierarchical Reasoning Model.")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column():
|
| 96 |
+
draft_input = gr.Textbox(label="Draft Abstract", lines=15, placeholder="Paste your draft abstract here...")
|
| 97 |
+
grant_type = gr.Dropdown(label="Grant Type", choices=grant_type_choices, value=grant_type_choices[0] if grant_type_choices else None)
|
| 98 |
+
optimize_btn = gr.Button("Optimize Abstract", variant="primary")
|
| 99 |
+
with gr.Column():
|
| 100 |
+
output_text = gr.Textbox(label="Optimized Abstract", lines=17, interactive=False)
|
| 101 |
|
| 102 |
+
optimize_btn.click(
|
| 103 |
+
fn=optimize_abstract,
|
| 104 |
+
inputs=[draft_input, grant_type],
|
| 105 |
+
outputs=output_text
|
| 106 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
demo.launch()
|