- Notifications
You must be signed in to change notification settings - Fork86
Feat/huggingface local model support#212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
kmurad-qlu wants to merge2 commits intobrowserbase:mainChoose a base branch fromkmurad-qlu:feat/huggingface-local-model-support
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Feat/huggingface local model support#212
kmurad-qlu wants to merge2 commits intobrowserbase:mainfromkmurad-qlu:feat/huggingface-local-model-support
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
- Add HuggingFaceLLMClient for local model inference- Support for 6 popular Hugging Face models (Llama 2, Mistral, Zephyr, etc.)- Add memory optimization with quantization support- Create comprehensive example and documentation- Add unit tests for Hugging Face integration- Update dependencies to include transformers, torch, accelerate
…imization## OverviewThis PR adds comprehensive support for running Stagehand with local Hugging Face models, enabling on-premises web automation without cloud dependencies. The implementation includes critical fixes for GPU memory management, JSON parsing, and empty result handling.## Key Features- **Local LLM Integration**: Full support for Hugging Face transformers with 4-bit quantization (~7GB VRAM)- **GPU Memory Optimization**: Prevents memory leaks by using shared model instances across multiple operations- **Robust JSON Extraction**: 5-strategy parsing pipeline with intelligent fallbacks for structured data- **Content Preservation**: Never loses content - wraps unparseable output in valid JSON structures- **Graceful Error Handling**: Comprehensive fallback mechanisms prevent empty results## Technical Improvements### 1. GPU Memory Management (examples/example_huggingface.py)- Removed model_name from StagehandConfig to prevent duplicate model loading- Implemented shared global model instance pattern- Added cleanup() between examples and full_cleanup() at program end- Result: Memory stays at ~7GB instead of accumulating to 23GB+### 2. Enhanced JSON Parsing (stagehand/llm/huggingface_client.py)- 5-strategy extraction pipeline: 1. Direct JSON parsing 2. Pattern matching for extraction fields 3. Markdown code block extraction 4. Flexible JSON object detection 5. Natural language to JSON conversion- Aggressive prompt engineering for JSON-only output- Input truncation to prevent CUDA OOM errors- Fallback responses when model unavailable### 3. Content Preservation (stagehand/llm/inference.py)- Critical fix: Wrap raw content in {"extraction": ...} on JSON parse failure- Prevents content loss during parsing errors- Ensures no empty results### 4. Lenient Schema Validation (stagehand/handlers/extract_handler.py)- Three-tier validation with fallbacks- Key normalization (camelCase ↔ snake_case)- Extracts any available string content for DefaultExtractSchema- Creates valid instances even from malformed data## Files Modified- examples/example_huggingface.py: Global model instance pattern- stagehand/llm/huggingface_client.py: Enhanced JSON parsing and memory management- stagehand/llm/inference.py: Content preservation on parse failures- stagehand/handlers/extract_handler.py: Lenient validation with fallbacks- stagehand/schemas.py: Schema compatibility improvements## TestingAll 7 examples run successfully:✅ Basic extraction✅ Data analysis✅ Content generation✅ Multi-step workflow✅ Dynamic content✅ Structured extraction✅ Complex multi-page workflow## Performance- Memory: ~7GB VRAM (with 4-bit quantization)- No CUDA OOM errors- Zero empty results- Graceful degradation on errors## DocumentationExisting HUGGINGFACE_SUPPORT.md provides comprehensive usage guide.Fixes issues with GPU memory exhaustion, empty extraction results, and JSON parsing failures in local model inference.Collaborator
miguelg719 commentedOct 3, 2025
Hi@kmurad-qlu |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Here's the revised content focusing on high-level achievements:
Why
Local Hugging Face model support enables privacy-focused, cost-effective, and offline-capable web automation. This PR enhances the robustness and production-readiness of local LLM inference by implementing comprehensive error handling, memory optimization, and intelligent content extraction strategies.
Key objectives:
What Changed
Core Enhancements
1. GPU Memory Optimization (
examples/example_huggingface.py)2. Intelligent JSON Extraction (
stagehand/llm/huggingface_client.py)3. Content Preservation (
stagehand/llm/inference.py) ⭐4. Flexible Schema Validation (
stagehand/handlers/extract_handler.py)5. Schema Compatibility (
stagehand/schemas.py)Test Plan
Comprehensive Example Coverage
All 7 production scenarios in
examples/example_huggingface.pyvalidated:Performance Metrics
Validation
Edge Cases Validated
Backwards Compatibility