37-AN
commited on
Commit
·
4fe6054
1
Parent(s):
780b542
Initial commit for Hugging Face Space deployment
Browse files- DEPLOYMENT.md +126 -0
- README-HF.md +41 -0
- README.md +34 -1
- check_git_status.py +141 -0
- deploy_to_hf.py +279 -0
- space.py +0 -49
DEPLOYMENT.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Deploying Your Personal AI Assistant to Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
This guide provides detailed instructions for deploying your Personal AI Assistant with RAG to Hugging Face Spaces.
|
| 4 |
+
|
| 5 |
+
## Prerequisites
|
| 6 |
+
|
| 7 |
+
Before you start, make sure you have:
|
| 8 |
+
|
| 9 |
+
1. **A Hugging Face Account**: Sign up at [https://huggingface.co/join](https://huggingface.co/join) if you don't have one
|
| 10 |
+
2. **Hugging Face API Token**: Create a token at [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
|
| 11 |
+
3. **Git**: Installed and configured on your machine
|
| 12 |
+
4. **Python 3.8+**: With pip for installing packages
|
| 13 |
+
|
| 14 |
+
## Step 1: Install Dependencies
|
| 15 |
+
|
| 16 |
+
First, install all required packages:
|
| 17 |
+
|
| 18 |
+
```bash
|
| 19 |
+
pip install -r requirements.txt
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
Make sure you have the huggingface_hub package installed:
|
| 23 |
+
|
| 24 |
+
```bash
|
| 25 |
+
pip install huggingface_hub
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
## Step 2: Check Your Git Configuration
|
| 29 |
+
|
| 30 |
+
Run the check script to ensure your Git is properly configured:
|
| 31 |
+
|
| 32 |
+
```bash
|
| 33 |
+
python check_git_status.py
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
This will:
|
| 37 |
+
- Verify Git is installed
|
| 38 |
+
- Check your Git configuration
|
| 39 |
+
- Confirm your repository status
|
| 40 |
+
- Verify Hugging Face configuration
|
| 41 |
+
|
| 42 |
+
Follow any prompts to fix issues that are detected.
|
| 43 |
+
|
| 44 |
+
## Step 3: Deploy to Hugging Face Spaces
|
| 45 |
+
|
| 46 |
+
Run the deployment script:
|
| 47 |
+
|
| 48 |
+
```bash
|
| 49 |
+
python deploy_to_hf.py
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
You will be prompted to enter:
|
| 53 |
+
1. Your Hugging Face username
|
| 54 |
+
2. Your Hugging Face API token
|
| 55 |
+
3. A name for your Space (or accept the default: personal-rag-assistant)
|
| 56 |
+
|
| 57 |
+
The script will:
|
| 58 |
+
- Configure your environment
|
| 59 |
+
- Create a new Hugging Face Space
|
| 60 |
+
- Set up Git for pushing to Hugging Face
|
| 61 |
+
- Push your code to the Space
|
| 62 |
+
|
| 63 |
+
## Step 4: Monitor the Deployment
|
| 64 |
+
|
| 65 |
+
After pushing your code:
|
| 66 |
+
|
| 67 |
+
1. Visit [https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME](https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME)
|
| 68 |
+
2. You'll see a "Building" status while Hugging Face builds your Docker container
|
| 69 |
+
3. This may take 5-10 minutes for the first build
|
| 70 |
+
|
| 71 |
+
## Step 5: Using Your Deployed Assistant
|
| 72 |
+
|
| 73 |
+
Once deployment is complete:
|
| 74 |
+
|
| 75 |
+
1. Your app will be accessible at [https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME](https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME)
|
| 76 |
+
2. Upload documents through the sidebar
|
| 77 |
+
3. Start asking questions about your documents
|
| 78 |
+
4. The application automatically saves your conversation history
|
| 79 |
+
|
| 80 |
+
## Troubleshooting
|
| 81 |
+
|
| 82 |
+
### Authentication Issues
|
| 83 |
+
|
| 84 |
+
If you encounter Git authentication issues:
|
| 85 |
+
|
| 86 |
+
```
|
| 87 |
+
remote: HTTP Basic: Access denied
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
This usually means:
|
| 91 |
+
- Your Hugging Face token is invalid or expired
|
| 92 |
+
- You haven't configured Git to use your token
|
| 93 |
+
|
| 94 |
+
Solution: Re-run the deployment script to update your credentials.
|
| 95 |
+
|
| 96 |
+
### Build Failures
|
| 97 |
+
|
| 98 |
+
If your Space shows a build failure:
|
| 99 |
+
|
| 100 |
+
1. Check the build logs in the Hugging Face Space UI
|
| 101 |
+
2. Common issues include:
|
| 102 |
+
- Missing dependencies in requirements.txt
|
| 103 |
+
- Issues with the Dockerfile
|
| 104 |
+
- Errors in your Python code
|
| 105 |
+
|
| 106 |
+
### Space Creation Errors
|
| 107 |
+
|
| 108 |
+
If the Space creation fails:
|
| 109 |
+
|
| 110 |
+
1. Try creating the Space manually in the Hugging Face UI
|
| 111 |
+
2. Then run the deployment script again, which will detect the existing Space
|
| 112 |
+
|
| 113 |
+
## Managing Your Space
|
| 114 |
+
|
| 115 |
+
After deployment, you can:
|
| 116 |
+
|
| 117 |
+
- **Update Your App**: Make changes locally, then run `python deploy_to_hf.py` again
|
| 118 |
+
- **Change Hardware**: Upgrade to a more powerful instance in the Hugging Face UI
|
| 119 |
+
- **Monitor Usage**: View usage statistics in the Hugging Face UI
|
| 120 |
+
- **Share Your App**: Share the URL with others to use your assistant
|
| 121 |
+
|
| 122 |
+
## Additional Resources
|
| 123 |
+
|
| 124 |
+
- [Hugging Face Spaces Documentation](https://huggingface.co/docs/hub/spaces)
|
| 125 |
+
- [Streamlit Documentation](https://docs.streamlit.io/)
|
| 126 |
+
- [LangChain Documentation](https://python.langchain.com/docs/)
|
README-HF.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Personal AI Assistant with RAG
|
| 3 |
+
emoji: 🤗
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: true
|
| 9 |
+
license: mit
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Personal AI Assistant with RAG
|
| 13 |
+
|
| 14 |
+
A powerful personal AI assistant that uses Retrieval-Augmented Generation (RAG) to provide responses based on your documents and notes.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
|
| 18 |
+
- Uses free Hugging Face models for language processing and embeddings
|
| 19 |
+
- Stores and retrieves information in a vector database
|
| 20 |
+
- Upload PDF, TXT, and CSV files to expand the knowledge base
|
| 21 |
+
- Add direct text input to your knowledge base
|
| 22 |
+
- View sources for AI responses
|
| 23 |
+
- Conversation history tracking
|
| 24 |
+
|
| 25 |
+
## How to Use
|
| 26 |
+
|
| 27 |
+
1. **Upload Documents**: Use the sidebar to upload files (PDF, TXT, CSV)
|
| 28 |
+
2. **Add Text**: Enter text directly into the knowledge base
|
| 29 |
+
3. **Ask Questions**: Chat with the assistant about your documents
|
| 30 |
+
4. **View Sources**: See where information is coming from
|
| 31 |
+
|
| 32 |
+
## Built With
|
| 33 |
+
|
| 34 |
+
- Hugging Face Models
|
| 35 |
+
- LLM: google/flan-t5-large
|
| 36 |
+
- Embeddings: sentence-transformers/all-MiniLM-L6-v2
|
| 37 |
+
- LangChain for orchestration
|
| 38 |
+
- Qdrant for vector storage
|
| 39 |
+
- Streamlit for UI
|
| 40 |
+
|
| 41 |
+
Created by [p3rc03](https://huggingface.co/p3rc03)
|
README.md
CHANGED
|
@@ -29,6 +29,35 @@ A powerful personal AI assistant that uses Retrieval-Augmented Generation (RAG)
|
|
| 29 |
3. **Ask Questions**: Chat with the assistant about your documents
|
| 30 |
4. **View Sources**: See where information is coming from
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
## Built With
|
| 33 |
|
| 34 |
- Hugging Face Models
|
|
@@ -38,4 +67,8 @@ A powerful personal AI assistant that uses Retrieval-Augmented Generation (RAG)
|
|
| 38 |
- Qdrant for vector storage
|
| 39 |
- Streamlit for UI
|
| 40 |
|
| 41 |
-
Created by [p3rc03](https://huggingface.co/p3rc03)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
3. **Ask Questions**: Chat with the assistant about your documents
|
| 30 |
4. **View Sources**: See where information is coming from
|
| 31 |
|
| 32 |
+
## Deployment
|
| 33 |
+
|
| 34 |
+
### Local Deployment
|
| 35 |
+
|
| 36 |
+
To run the app locally:
|
| 37 |
+
|
| 38 |
+
1. Clone this repository
|
| 39 |
+
2. Install requirements: `pip install -r requirements.txt`
|
| 40 |
+
3. Run the Streamlit UI: `python run.py --ui`
|
| 41 |
+
4. Or run the API server: `python run.py --api`
|
| 42 |
+
|
| 43 |
+
### Deploying to Hugging Face Spaces
|
| 44 |
+
|
| 45 |
+
This application can be easily deployed to Hugging Face Spaces for free hosting:
|
| 46 |
+
|
| 47 |
+
1. Make sure you have a Hugging Face account
|
| 48 |
+
2. Create a Hugging Face API token at https://huggingface.co/settings/tokens
|
| 49 |
+
3. Run the deployment script: `python deploy_to_hf.py`
|
| 50 |
+
4. Follow the prompts to enter your username, token, and space name
|
| 51 |
+
5. Wait for the deployment to complete
|
| 52 |
+
|
| 53 |
+
If you encounter any issues during deployment, run `python check_git_status.py` to diagnose and fix common problems.
|
| 54 |
+
|
| 55 |
+
The deployment process:
|
| 56 |
+
- Creates a Hugging Face Space using the Spaces SDK
|
| 57 |
+
- Configures git for pushing to Hugging Face
|
| 58 |
+
- Pushes your code to the Space
|
| 59 |
+
- Builds and deploys the Docker container automatically
|
| 60 |
+
|
| 61 |
## Built With
|
| 62 |
|
| 63 |
- Hugging Face Models
|
|
|
|
| 67 |
- Qdrant for vector storage
|
| 68 |
- Streamlit for UI
|
| 69 |
|
| 70 |
+
Created by [p3rc03](https://huggingface.co/p3rc03)
|
| 71 |
+
|
| 72 |
+
## License
|
| 73 |
+
|
| 74 |
+
MIT License - See LICENSE file for details
|
check_git_status.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""
|
| 3 |
+
Check Git status and configuration for Hugging Face Space deployment.
|
| 4 |
+
This script helps troubleshoot git issues when deploying to Hugging Face Spaces.
|
| 5 |
+
"""
|
| 6 |
+
import subprocess
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
def run_cmd(cmd, description, show_output=True):
|
| 11 |
+
"""Run a command and return the result."""
|
| 12 |
+
print(f"\n{description}:")
|
| 13 |
+
try:
|
| 14 |
+
if show_output:
|
| 15 |
+
subprocess.run(cmd, check=True)
|
| 16 |
+
return True
|
| 17 |
+
else:
|
| 18 |
+
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
|
| 19 |
+
return result.stdout.strip()
|
| 20 |
+
except subprocess.CalledProcessError as e:
|
| 21 |
+
print(f"Error: {e}")
|
| 22 |
+
if e.stderr:
|
| 23 |
+
print(f"Details: {e.stderr}")
|
| 24 |
+
return False
|
| 25 |
+
|
| 26 |
+
def check_git_installation():
|
| 27 |
+
"""Check if git is installed and properly configured."""
|
| 28 |
+
print("="*50)
|
| 29 |
+
print("Checking Git Installation and Configuration")
|
| 30 |
+
print("="*50)
|
| 31 |
+
|
| 32 |
+
# Check git version
|
| 33 |
+
if not run_cmd(["git", "--version"], "Git version"):
|
| 34 |
+
print("Git is not installed. Please install Git and try again.")
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
# Check git configuration
|
| 38 |
+
user_name = run_cmd(["git", "config", "user.name"], "Git user name", False)
|
| 39 |
+
user_email = run_cmd(["git", "config", "user.email"], "Git user email", False)
|
| 40 |
+
|
| 41 |
+
if not user_name or not user_email:
|
| 42 |
+
print("\nGit user name or email is not configured. Please set them:")
|
| 43 |
+
print(" git config --global user.name \"Your Name\"")
|
| 44 |
+
print(" git config --global user.email \"[email protected]\"")
|
| 45 |
+
|
| 46 |
+
# Prompt to set them now
|
| 47 |
+
if input("\nDo you want to set them now? (y/n): ").lower() == 'y':
|
| 48 |
+
name = input("Enter your name: ")
|
| 49 |
+
email = input("Enter your email: ")
|
| 50 |
+
run_cmd(["git", "config", "--global", "user.name", name], "Setting Git user name", False)
|
| 51 |
+
run_cmd(["git", "config", "--global", "user.email", email], "Setting Git user email", False)
|
| 52 |
+
print("Git configuration updated.")
|
| 53 |
+
else:
|
| 54 |
+
return False
|
| 55 |
+
else:
|
| 56 |
+
print(f"\nGit user name: {user_name}")
|
| 57 |
+
print(f"Git user email: {user_email}")
|
| 58 |
+
|
| 59 |
+
return True
|
| 60 |
+
|
| 61 |
+
def check_git_repository():
|
| 62 |
+
"""Check the Git repository status."""
|
| 63 |
+
print("\n" + "="*50)
|
| 64 |
+
print("Checking Git Repository Status")
|
| 65 |
+
print("="*50)
|
| 66 |
+
|
| 67 |
+
# Check if this is a git repository
|
| 68 |
+
is_git_repo = os.path.exists(".git")
|
| 69 |
+
print(f"\nIs Git repository: {'Yes' if is_git_repo else 'No'}")
|
| 70 |
+
|
| 71 |
+
if not is_git_repo:
|
| 72 |
+
print("This is not a git repository. You can initialize it with 'git init'")
|
| 73 |
+
|
| 74 |
+
# Prompt to initialize git
|
| 75 |
+
if input("\nDo you want to initialize git now? (y/n): ").lower() == 'y':
|
| 76 |
+
run_cmd(["git", "init"], "Initializing Git repository")
|
| 77 |
+
print("Git repository initialized.")
|
| 78 |
+
else:
|
| 79 |
+
return False
|
| 80 |
+
|
| 81 |
+
# Check git status
|
| 82 |
+
run_cmd(["git", "status"], "Git status")
|
| 83 |
+
|
| 84 |
+
# Check remote repositories
|
| 85 |
+
run_cmd(["git", "remote", "-v"], "Git remotes")
|
| 86 |
+
|
| 87 |
+
return True
|
| 88 |
+
|
| 89 |
+
def check_huggingface_configuration():
|
| 90 |
+
"""Check Hugging Face-specific configurations."""
|
| 91 |
+
print("\n" + "="*50)
|
| 92 |
+
print("Checking Hugging Face Configuration")
|
| 93 |
+
print("="*50)
|
| 94 |
+
|
| 95 |
+
# Check if huggingface_hub is installed
|
| 96 |
+
try:
|
| 97 |
+
import huggingface_hub
|
| 98 |
+
print(f"\nhuggingface_hub version: {huggingface_hub.__version__}")
|
| 99 |
+
except ImportError:
|
| 100 |
+
print("\nhuggingface_hub is not installed. Installing...")
|
| 101 |
+
subprocess.run([sys.executable, "-m", "pip", "install", "huggingface_hub"], check=True)
|
| 102 |
+
print("huggingface_hub installed.")
|
| 103 |
+
|
| 104 |
+
# Check environment variables
|
| 105 |
+
hf_username = os.environ.get("HF_USERNAME")
|
| 106 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 107 |
+
|
| 108 |
+
print(f"\nHF_USERNAME is {'set' if hf_username else 'not set'}")
|
| 109 |
+
print(f"HF_TOKEN is {'set' if hf_token else 'not set'}")
|
| 110 |
+
|
| 111 |
+
if not hf_username or not hf_token:
|
| 112 |
+
print("\nPlease set HF_USERNAME and HF_TOKEN environment variables.")
|
| 113 |
+
print("You can run the deploy_to_hf.py script to do this.")
|
| 114 |
+
|
| 115 |
+
return True
|
| 116 |
+
|
| 117 |
+
def main():
|
| 118 |
+
"""Main function to check git status."""
|
| 119 |
+
# Check git installation
|
| 120 |
+
if not check_git_installation():
|
| 121 |
+
print("\nGit installation or configuration issue. Please fix and try again.")
|
| 122 |
+
return
|
| 123 |
+
|
| 124 |
+
# Check git repository
|
| 125 |
+
if not check_git_repository():
|
| 126 |
+
print("\nGit repository issue. Please fix and try again.")
|
| 127 |
+
return
|
| 128 |
+
|
| 129 |
+
# Check Hugging Face configuration
|
| 130 |
+
if not check_huggingface_configuration():
|
| 131 |
+
print("\nHugging Face configuration issue. Please fix and try again.")
|
| 132 |
+
return
|
| 133 |
+
|
| 134 |
+
print("\n" + "="*50)
|
| 135 |
+
print("Git status check completed successfully!")
|
| 136 |
+
print("="*50)
|
| 137 |
+
print("\nYou are ready to deploy to Hugging Face Spaces.")
|
| 138 |
+
print("Run 'python deploy_to_hf.py' to begin deployment.")
|
| 139 |
+
|
| 140 |
+
if __name__ == "__main__":
|
| 141 |
+
main()
|
deploy_to_hf.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""
|
| 3 |
+
Helper script to deploy to Hugging Face Spaces.
|
| 4 |
+
This script will help you set environment variables and deploy your app.
|
| 5 |
+
"""
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
import subprocess
|
| 9 |
+
from getpass import getpass
|
| 10 |
+
from huggingface_hub import HfApi, SpaceHardware, SpaceStage
|
| 11 |
+
|
| 12 |
+
def setup_deployment():
|
| 13 |
+
"""Set up deployment environment variables and authenticate."""
|
| 14 |
+
print("="*50)
|
| 15 |
+
print("Hugging Face Spaces Deployment Setup")
|
| 16 |
+
print("="*50)
|
| 17 |
+
|
| 18 |
+
# Get user credentials
|
| 19 |
+
username = input("Enter your Hugging Face username: ")
|
| 20 |
+
token = getpass("Enter your Hugging Face token (from https://huggingface.co/settings/tokens): ")
|
| 21 |
+
space_name = input("Enter your Space name (default: personal-rag-assistant): ") or "personal-rag-assistant"
|
| 22 |
+
|
| 23 |
+
# Set environment variables
|
| 24 |
+
os.environ["HF_USERNAME"] = username
|
| 25 |
+
os.environ["HF_TOKEN"] = token
|
| 26 |
+
os.environ["SPACE_NAME"] = space_name
|
| 27 |
+
|
| 28 |
+
# Write credentials to .env file
|
| 29 |
+
with open(".env", "w") as f:
|
| 30 |
+
f.write(f"HF_API_KEY={token}\n")
|
| 31 |
+
f.write(f"HF_USERNAME={username}\n")
|
| 32 |
+
f.write(f"SPACE_NAME={space_name}\n")
|
| 33 |
+
f.write("LLM_MODEL=google/flan-t5-large\n")
|
| 34 |
+
f.write("EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2\n")
|
| 35 |
+
f.write("VECTOR_DB_PATH=./data/vector_db\n")
|
| 36 |
+
f.write("COLLECTION_NAME=personal_assistant\n")
|
| 37 |
+
f.write("DEFAULT_TEMPERATURE=0.7\n")
|
| 38 |
+
f.write("CHUNK_SIZE=1000\n")
|
| 39 |
+
f.write("CHUNK_OVERLAP=200\n")
|
| 40 |
+
f.write("MAX_TOKENS=512\n")
|
| 41 |
+
|
| 42 |
+
# Set up git credential helper for Hugging Face
|
| 43 |
+
try:
|
| 44 |
+
# Configure git to use credential store
|
| 45 |
+
subprocess.run(["git", "config", "--global", "credential.helper", "store"], check=True)
|
| 46 |
+
|
| 47 |
+
# Create .git-credentials file with token
|
| 48 |
+
home_dir = os.path.expanduser("~")
|
| 49 |
+
credentials_path = os.path.join(home_dir, ".git-credentials")
|
| 50 |
+
|
| 51 |
+
# Check if credentials already exist
|
| 52 |
+
credentials_exist = os.path.exists(credentials_path)
|
| 53 |
+
|
| 54 |
+
with open(credentials_path, "a" if credentials_exist else "w") as f:
|
| 55 |
+
f.write(f"https://{username}:{token}@huggingface.co\n")
|
| 56 |
+
|
| 57 |
+
# Make sure credentials file has correct permissions
|
| 58 |
+
if sys.platform != "win32": # Skip on Windows
|
| 59 |
+
os.chmod(credentials_path, 0o600)
|
| 60 |
+
|
| 61 |
+
print("Git credentials configured for Hugging Face.")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Warning: Could not set up git credentials: {e}")
|
| 64 |
+
print("You may need to enter your credentials manually during push.")
|
| 65 |
+
|
| 66 |
+
print(f"\nEnvironment variables set for {username}/{space_name}")
|
| 67 |
+
return username, token, space_name
|
| 68 |
+
|
| 69 |
+
def create_space(username, token, space_name):
|
| 70 |
+
"""Create a Hugging Face Space directly using the HfApi."""
|
| 71 |
+
print("\nCreating Hugging Face Space...")
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
# Initialize the API
|
| 75 |
+
api = HfApi(token=token)
|
| 76 |
+
|
| 77 |
+
# Check if space exists
|
| 78 |
+
try:
|
| 79 |
+
spaces = api.list_spaces(author=username)
|
| 80 |
+
exists = any(space.id == f"{username}/{space_name}" for space in spaces)
|
| 81 |
+
if exists:
|
| 82 |
+
print(f"Space {username}/{space_name} exists.")
|
| 83 |
+
else:
|
| 84 |
+
print(f"Space {username}/{space_name} does not exist. Creating...")
|
| 85 |
+
# Create the space
|
| 86 |
+
api.create_space(
|
| 87 |
+
name=space_name,
|
| 88 |
+
organization=None, # Use None for personal account
|
| 89 |
+
private=False,
|
| 90 |
+
sdk="docker",
|
| 91 |
+
hardware=SpaceHardware.CPU_BASIC,
|
| 92 |
+
storage=1,
|
| 93 |
+
sleep_time=3600, # 1 hour of inactivity before sleep
|
| 94 |
+
status=SpaceStage.RUNNING,
|
| 95 |
+
)
|
| 96 |
+
print(f"Space created successfully.")
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"Error checking/creating space: {e}")
|
| 99 |
+
print("You may need to create the Space manually in the Hugging Face UI.")
|
| 100 |
+
print(f"Visit: https://huggingface.co/spaces")
|
| 101 |
+
return False
|
| 102 |
+
|
| 103 |
+
print(f"Space URL: https://huggingface.co/spaces/{username}/{space_name}")
|
| 104 |
+
return True
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"Error creating space: {e}")
|
| 107 |
+
print("\nTrying to proceed anyway, as the space might already exist.")
|
| 108 |
+
return True
|
| 109 |
+
|
| 110 |
+
def prepare_git_push(username, space_name):
|
| 111 |
+
"""Prepare git for pushing to Hugging Face Space."""
|
| 112 |
+
print("\nPreparing to push code to Hugging Face Space...")
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
# Initialize git if not already done
|
| 116 |
+
if not os.path.exists(".git"):
|
| 117 |
+
subprocess.run(["git", "init"], check=True)
|
| 118 |
+
print("Git repository initialized.")
|
| 119 |
+
|
| 120 |
+
# Configure git remote
|
| 121 |
+
remote_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
| 122 |
+
|
| 123 |
+
# Check all remotes
|
| 124 |
+
result = subprocess.run(["git", "remote"], capture_output=True, text=True)
|
| 125 |
+
remotes = result.stdout.strip().split('\n') if result.stdout else []
|
| 126 |
+
|
| 127 |
+
print(f"Existing remotes: {remotes}")
|
| 128 |
+
|
| 129 |
+
# Check if 'hf' remote exists
|
| 130 |
+
if "hf" not in remotes:
|
| 131 |
+
# Add the remote
|
| 132 |
+
print("Adding 'hf' remote...")
|
| 133 |
+
try:
|
| 134 |
+
add_result = subprocess.run(["git", "remote", "add", "hf", remote_url], capture_output=True, text=True)
|
| 135 |
+
if add_result.returncode != 0:
|
| 136 |
+
print(f"Error adding remote: {add_result.stderr}")
|
| 137 |
+
return False
|
| 138 |
+
print("Successfully added 'hf' remote")
|
| 139 |
+
except Exception as e:
|
| 140 |
+
print(f"Error adding remote: {e}")
|
| 141 |
+
return False
|
| 142 |
+
else:
|
| 143 |
+
# Update existing remote
|
| 144 |
+
print("Updating 'hf' remote...")
|
| 145 |
+
try:
|
| 146 |
+
update_result = subprocess.run(["git", "remote", "set-url", "hf", remote_url], capture_output=True, text=True)
|
| 147 |
+
if update_result.returncode != 0:
|
| 148 |
+
print(f"Error updating remote: {update_result.stderr}")
|
| 149 |
+
return False
|
| 150 |
+
print("Successfully updated 'hf' remote")
|
| 151 |
+
except Exception as e:
|
| 152 |
+
print(f"Error updating remote: {e}")
|
| 153 |
+
return False
|
| 154 |
+
|
| 155 |
+
# Verify remote was added/updated
|
| 156 |
+
verify_remote = subprocess.run(["git", "remote", "-v"], capture_output=True, text=True)
|
| 157 |
+
print(f"Remote verification: {verify_remote.stdout}")
|
| 158 |
+
|
| 159 |
+
# Make sure we have user details
|
| 160 |
+
try:
|
| 161 |
+
user_name = subprocess.run(["git", "config", "user.name"], capture_output=True, text=True).stdout.strip()
|
| 162 |
+
user_email = subprocess.run(["git", "config", "user.email"], capture_output=True, text=True).stdout.strip()
|
| 163 |
+
|
| 164 |
+
if not user_name or not user_email:
|
| 165 |
+
# Set default values if not configured
|
| 166 |
+
name = input("Enter your name for git config: ") or username
|
| 167 |
+
email = input("Enter your email for git config: ") or f"{username}@example.com"
|
| 168 |
+
|
| 169 |
+
subprocess.run(["git", "config", "--global", "user.name", name], check=True)
|
| 170 |
+
subprocess.run(["git", "config", "--global", "user.email", email], check=True)
|
| 171 |
+
print("Git user configuration updated.")
|
| 172 |
+
except subprocess.CalledProcessError:
|
| 173 |
+
print("Warning: Could not check git user configuration.")
|
| 174 |
+
|
| 175 |
+
# Add and commit files
|
| 176 |
+
subprocess.run(["git", "add", "."], check=True)
|
| 177 |
+
try:
|
| 178 |
+
subprocess.run(["git", "commit", "-m", "Initial commit for Hugging Face Space deployment"], check=True)
|
| 179 |
+
except subprocess.CalledProcessError:
|
| 180 |
+
# Check if there are changes to commit
|
| 181 |
+
status = subprocess.run(["git", "status", "--porcelain"], capture_output=True, text=True, check=True).stdout.strip()
|
| 182 |
+
if not status:
|
| 183 |
+
print("No changes to commit.")
|
| 184 |
+
else:
|
| 185 |
+
print("Error making commit. Check git configuration.")
|
| 186 |
+
return False
|
| 187 |
+
|
| 188 |
+
print("\nGit repository prepared for pushing")
|
| 189 |
+
except Exception as e:
|
| 190 |
+
print(f"Error preparing git: {e}")
|
| 191 |
+
return False
|
| 192 |
+
|
| 193 |
+
return True
|
| 194 |
+
|
| 195 |
+
def push_to_space(username, token):
|
| 196 |
+
"""Push code to Hugging Face Space."""
|
| 197 |
+
print("\nPushing code to Hugging Face Space...")
|
| 198 |
+
print("This may take a few minutes...")
|
| 199 |
+
|
| 200 |
+
try:
|
| 201 |
+
# Set git credentials environment variables for this push
|
| 202 |
+
env = os.environ.copy()
|
| 203 |
+
env["GIT_USERNAME"] = username
|
| 204 |
+
env["GIT_PASSWORD"] = token
|
| 205 |
+
|
| 206 |
+
# Determine current branch
|
| 207 |
+
current_branch = subprocess.run(
|
| 208 |
+
["git", "branch", "--show-current"],
|
| 209 |
+
capture_output=True, text=True
|
| 210 |
+
).stdout.strip()
|
| 211 |
+
|
| 212 |
+
if not current_branch:
|
| 213 |
+
current_branch = "master" # Default to master if no branch is returned
|
| 214 |
+
|
| 215 |
+
# Push code - force push to override any existing content
|
| 216 |
+
print(f"Pushing from branch {current_branch} to main...")
|
| 217 |
+
cmd = ["git", "push", "-f", "hf", f"{current_branch}:main"]
|
| 218 |
+
|
| 219 |
+
print("\nRunning git push command...")
|
| 220 |
+
print(f"Pushing to Space as user: {username}")
|
| 221 |
+
|
| 222 |
+
# Try to push
|
| 223 |
+
try:
|
| 224 |
+
subprocess.run(cmd, check=True, env=env)
|
| 225 |
+
except subprocess.CalledProcessError as e:
|
| 226 |
+
print(f"Error during push: {e}")
|
| 227 |
+
|
| 228 |
+
# Try direct URL push as alternative
|
| 229 |
+
print("\nTrying alternative direct URL push...")
|
| 230 |
+
direct_url = f"https://{username}:{token}@huggingface.co/spaces/{username}/{os.environ.get('SPACE_NAME')}"
|
| 231 |
+
alt_cmd = ["git", "push", "-f", direct_url, f"{current_branch}:main"]
|
| 232 |
+
|
| 233 |
+
try:
|
| 234 |
+
subprocess.run(alt_cmd, check=True, env=env)
|
| 235 |
+
except subprocess.CalledProcessError as e:
|
| 236 |
+
print(f"Direct URL push also failed: {e}")
|
| 237 |
+
raise
|
| 238 |
+
|
| 239 |
+
print("\nCode pushed to Hugging Face Space successfully!")
|
| 240 |
+
except subprocess.CalledProcessError as e:
|
| 241 |
+
print(f"Error pushing code: {e}")
|
| 242 |
+
print("\nTroubleshooting git push issues:")
|
| 243 |
+
print("1. Ensure your Hugging Face token has write access")
|
| 244 |
+
print("2. Try manually setting up git credentials:")
|
| 245 |
+
print(f" git config --global credential.helper store")
|
| 246 |
+
print(f" echo 'https://{username}:{token}@huggingface.co' > ~/.git-credentials")
|
| 247 |
+
print("3. Try pushing directly with:")
|
| 248 |
+
print(f" git push -f https://{username}:{token}@huggingface.co/spaces/{username}/{os.environ.get('SPACE_NAME')} main")
|
| 249 |
+
return False
|
| 250 |
+
|
| 251 |
+
return True
|
| 252 |
+
|
| 253 |
+
def main():
|
| 254 |
+
"""Main function to run the deployment process."""
|
| 255 |
+
username, token, space_name = setup_deployment()
|
| 256 |
+
|
| 257 |
+
# Create the Space
|
| 258 |
+
if not create_space(username, token, space_name):
|
| 259 |
+
print("Failed to create Space. Attempting to continue anyway.")
|
| 260 |
+
|
| 261 |
+
# Prepare git for pushing
|
| 262 |
+
if not prepare_git_push(username, space_name):
|
| 263 |
+
print("Failed to prepare git. Exiting.")
|
| 264 |
+
return
|
| 265 |
+
|
| 266 |
+
# Push code to Space
|
| 267 |
+
if not push_to_space(username, token):
|
| 268 |
+
print("Failed to push code. Exiting.")
|
| 269 |
+
return
|
| 270 |
+
|
| 271 |
+
print("\n" + "="*50)
|
| 272 |
+
print(f"Deployment completed! Your app should be available at:")
|
| 273 |
+
print(f"https://huggingface.co/spaces/{username}/{space_name}")
|
| 274 |
+
print("="*50)
|
| 275 |
+
print("\nNote: It may take a few minutes for the Space to build and deploy your app.")
|
| 276 |
+
print("You can monitor the build progress on the Space page.")
|
| 277 |
+
|
| 278 |
+
if __name__ == "__main__":
|
| 279 |
+
main()
|
space.py
DELETED
|
@@ -1,49 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python
|
| 2 |
-
"""
|
| 3 |
-
Register this app with Hugging Face Spaces SDK.
|
| 4 |
-
This file is used for deploying the app to Hugging Face Spaces.
|
| 5 |
-
"""
|
| 6 |
-
import os
|
| 7 |
-
import subprocess
|
| 8 |
-
import sys
|
| 9 |
-
from huggingface_hub import SpaceHardware, SpaceStage, SpaceSDK
|
| 10 |
-
|
| 11 |
-
def create_space():
|
| 12 |
-
"""Create or update a Hugging Face Space."""
|
| 13 |
-
# Get the Space name or use a default
|
| 14 |
-
space_name = os.environ.get("SPACE_NAME", "personal-rag-assistant")
|
| 15 |
-
owner = os.environ.get("HF_USERNAME")
|
| 16 |
-
|
| 17 |
-
if not owner:
|
| 18 |
-
print("Please set the HF_USERNAME environment variable to your Hugging Face username.")
|
| 19 |
-
sys.exit(1)
|
| 20 |
-
|
| 21 |
-
# Initialize the SDK
|
| 22 |
-
sdk = SpaceSDK(
|
| 23 |
-
space_id=f"{owner}/{space_name}",
|
| 24 |
-
token=os.environ.get("HF_TOKEN")
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# Check if space exists, if not create it
|
| 28 |
-
try:
|
| 29 |
-
space_info = sdk.get_space_runtime()
|
| 30 |
-
print(f"Space {owner}/{space_name} exists.")
|
| 31 |
-
exists = True
|
| 32 |
-
except Exception:
|
| 33 |
-
exists = False
|
| 34 |
-
|
| 35 |
-
# Create or update the space
|
| 36 |
-
if not exists:
|
| 37 |
-
print(f"Creating new space: {owner}/{space_name}")
|
| 38 |
-
sdk.create_space(
|
| 39 |
-
space_hardware=SpaceHardware.CPU_BASIC,
|
| 40 |
-
space_storage=1,
|
| 41 |
-
space_sleep_time=3600, # 1 hour of inactivity before sleep
|
| 42 |
-
space_stage=SpaceStage.RUNNING,
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
print(f"Space URL: https://huggingface.co/spaces/{owner}/{space_name}")
|
| 46 |
-
return sdk
|
| 47 |
-
|
| 48 |
-
if __name__ == "__main__":
|
| 49 |
-
create_space()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|