rewrwdff/hf / deploy_hf_hermes3.py
rewrwdff's picture
download
raw
10.8 kB
#!/usr/bin/env python3
"""
πŸš€ FANTASYBOT + HERMES-3 EN HUGGING FACE
Instalador interactivo para HF Spaces / Model Hub / Datasets / Endpoints
"""
import os
import sys
import subprocess
from pathlib import Path
class Colors:
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
END = '\033[0m'
def header():
print(f"{Colors.BLUE}")
print("╔════════════════════════════════════════════════════════════════╗")
print("β•‘ πŸš€ FantasyBot + Hermes-3 en Hugging Face β•‘")
print("β•‘ (Spaces / Model Hub / Datasets / Endpoints) β•‘")
print("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•")
print(f"{Colors.END}")
def choose_hf_option():
print(f"\n{Colors.BLUE}Elige dΓ³nde desplegar en Hugging Face:{Colors.END}\n")
options = {
"1": ("HF Spaces + Hermes-3 Local (RECOMENDADO)", "spaces"),
"2": ("Model Hub (Guardar modelo GGUF)", "model"),
"3": ("Datasets (Guardar estado del agente)", "dataset"),
"4": ("Endpoints (API Inference)", "endpoints"),
}
for key, (name, _) in options.items():
print(f" {key}. {name}")
choice = input("\nElecciΓ³n (1-4): ").strip()
if choice not in options:
print(f"{Colors.RED}❌ Opción invÑlida{Colors.END}")
sys.exit(1)
return options[choice][1]
def get_hf_credentials():
print(f"\n{Colors.YELLOW}Credenciales de Hugging Face:{Colors.END}\n")
from getpass import getpass
hf_user = input("Tu usuario de HF: ").strip()
hf_token = getpass("Token HF (write): ")
laliga_refresh = input("LALIGA REFRESH_TOKEN: ").strip()
laliga_access = input("LALIGA ACCESS_TOKEN: ").strip()
return {
"hf_user": hf_user,
"hf_token": hf_token,
"laliga_refresh": laliga_refresh,
"laliga_access": laliga_access,
}
def setup_hf_spaces(creds):
print(f"\n{Colors.YELLOW}Configurando HF Spaces + Hermes-3...{Colors.END}\n")
space_name = input("Nombre del Space (ej: fantasybot-hermes): ").strip()
print(f"\n{Colors.GREEN}Paso 1: Crear Space en Hugging Face{Colors.END}")
print(f" 1. Ve a: https://huggingface.co/spaces")
print(f" 2. 'Create new Space'")
print(f" 3. Space name: {space_name}")
print(f" 4. SDK: Docker")
print(f" 5. Visibility: Private")
print(f" 6. Click 'Create Space'\n")
input("Presiona Enter cuando hayas creado el Space...")
print(f"\n{Colors.GREEN}Paso 2: Clonar y configurar{Colors.END}\n")
dockerfile = f"""FROM python:3.11-slim
WORKDIR /app
RUN apt-get update && apt-get install -y git curl && rm -rf /var/lib/apt/lists/*
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir transformers torch
RUN mkdir -p .state .cache .logs hermes
ENV STREAMLIT_SERVER_PORT=7860
ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0
ENV STREAMLIT_SERVER_HEADLESS=true
CMD ["streamlit", "run", "app.py"]
"""
requirements = """streamlit>=1.40.0
requests>=2.31.0
python-dotenv>=1.0.0
transformers>=4.35.0
torch>=2.0.0
click>=8.1.0
pydantic>=2.0.0
"""
print("Ejecuta estos comandos:\n")
print(f"{Colors.YELLOW}1. Clonar:{Colors.END}")
print(f""" git clone https://huggingface.co/spaces/{creds['hf_user']}/{space_name}.git
cd {space_name}
git clone https://github.com/jonortega20/fantasybot.git temp
cp temp/app.py temp/requirements.txt .
rm -rf temp\n""")
print(f"{Colors.YELLOW}2. Crear Dockerfile:{Colors.END}")
print(f" Copia este contenido en Dockerfile:")
print(f"{Colors.BLUE}{dockerfile}{Colors.END}\n")
print(f"{Colors.YELLOW}3. Crear hermes/config.yaml:{Colors.END}")
print(f""" mkdir -p hermes
cat > hermes/config.yaml << 'EOF'
model:
provider: "transformers"
model_id: "NousResearch/Hermes-3-Llama-3.1-8B"
device: "cuda"
dtype: "float16"
laliga:
refresh_token: "{creds['laliga_refresh']}"
access_token: "{creds['laliga_access']}"
EOF\n""")
print(f"{Colors.YELLOW}4. Crear .env:{Colors.END}")
print(f""" cat > .env << 'EOF'
REFRESH_TOKEN={creds['laliga_refresh']}
ACCESS_TOKEN={creds['laliga_access']}
EOF\n""")
print(f"{Colors.YELLOW}5. Push a HF Spaces:{Colors.END}")
print(f""" git add .
git commit -m "Deploy FantasyBot + Hermes-3"
git push\n""")
print(f"{Colors.YELLOW}6. AΓ±adir secrets en HF Spaces:{Colors.END}")
print(f""" Ve a: https://huggingface.co/spaces/{creds['hf_user']}/{space_name}/settings
Repository secrets:
- REFRESH_TOKEN = {creds['laliga_refresh']}
- ACCESS_TOKEN = {creds['laliga_access']}\n""")
print(f"{Colors.GREEN}βœ… Espera 5-10 minutos a que compile.{Colors.END}")
print(f"Tu Space estarΓ‘ en: https://huggingface.co/spaces/{creds['hf_user']}/{space_name}\n")
def setup_hf_model(creds):
print(f"\n{Colors.YELLOW}Configurando Model Hub..{Colors.END}\n")
print(f"{Colors.YELLOW}1. Crear repo en HF:{Colors.END}")
print(f""" huggingface-cli repo create fantasybot-hermes-3 --type model\n""")
print(f"{Colors.YELLOW}2. Clonar y descargar modelo:{Colors.END}")
print(f""" git clone https://huggingface.co/{creds['hf_user']}/fantasybot-hermes-3
cd fantasybot-hermes-3
# Descargar GGUF (mΓ‘s pequeΓ±o)
huggingface-cli download TheBloke/Hermes-3-Llama-3.1-8B-GGUF \\
hermes-3-llama-3.1-8b.Q4_K_M.gguf \\
--local-dir ./models\n""")
print(f"{Colors.YELLOW}3. Crear README.md:{Colors.END}")
print(f""" cat > README.md << 'EOF'
# FantasyBot con Hermes-3-Llama
Modelo GGUF cuantizado para FantasyBot
## Uso
```python
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("{creds['hf_user']}/fantasybot-hermes-3")
model = AutoModelForCausalLM.from_pretrained("{creds['hf_user']}/fantasybot-hermes-3")
```
## Specifications
- Modelo: Hermes-3-Llama-3.1-8B
- Formato: GGUF Q4_K_M
- TamaΓ±o: ~5GB
EOF\n""")
print(f"{Colors.YELLOW}4. Push a HF Model Hub:{Colors.END}")
print(f""" git add .
git commit -m "Add Hermes-3 GGUF"
git push\n""")
print(f"{Colors.GREEN}βœ… Tu modelo estarΓ‘ en:{Colors.END}")
print(f"https://huggingface.co/{creds['hf_user']}/fantasybot-hermes-3\n")
def setup_hf_dataset(creds):
print(f"\n{Colors.YELLOW}Configurando Datasets..{Colors.END}\n")
print(f"{Colors.YELLOW}1. Crear dataset:{Colors.END}")
print(f""" https://huggingface.co/new-dataset
Nombre: fantasybot-states\n""")
print(f"{Colors.YELLOW}2. CΓ³digo para guardar estado en HF:{Colors.END}")
print(f""" from datasets import Dataset
from huggingface_hub import HfApi
def save_state(state_dict, token):
dataset = Dataset.from_dict(state_dict)
dataset.push_to_hub(
"{creds['hf_user']}/fantasybot-states",
private=True,
token=token
)
# Uso
save_state(agent_state, "{creds['hf_token']}")\n""")
print(f"{Colors.GREEN}βœ… Tu dataset estarΓ‘ en:{Colors.END}")
print(f"https://huggingface.co/datasets/{creds['hf_user']}/fantasybot-states\n")
def setup_hf_endpoints(creds):
print(f"\n{Colors.YELLOW}Configurando Endpoints..{Colors.END}\n")
print(f"{Colors.YELLOW}1. Crear Endpoint en HF:{Colors.END}")
print(f""" https://ui.endpoints.huggingface.co
- Modelo: NousResearch/Hermes-3-Llama-3.1-8B
- Tipo: Protected
- Deploy\n""")
print(f"{Colors.YELLOW}2. CΓ³digo para usar el API:{Colors.END}")
print(f""" import requests
def query_hermes(prompt):
API_URL = "https://api-inference.huggingface.co/models/NousResearch/Hermes-3-Llama-3.1-8B"
headers = {{"Authorization": "Bearer {creds['hf_token']}"}}
response = requests.post(
API_URL,
headers=headers,
json={{
"inputs": prompt,
"parameters": {{"max_new_tokens": 2048}}
}}
)
return response.json()
# Uso
result = query_hermes("Analiza el mercado de LALIGA")\n""")
print(f"{Colors.YELLOW}3. Usar en FantasyBot:{Colors.END}")
print(f""" cat >> hermes/config.yaml << 'EOF'
model:
provider: "hf_endpoints"
api_url: "https://api-inference.huggingface.co/models/NousResearch/Hermes-3-Llama-3.1-8B"
api_token: "{creds['hf_token']}"
EOF\n""")
print(f"{Colors.GREEN}βœ… Tu endpoint estarΓ‘ disponible en:{Colors.END}")
print(f"https://ui.endpoints.huggingface.co\n")
def comparison():
print(f"\n{Colors.BLUE}{'='*70}{Colors.END}")
print(f"{Colors.YELLOW}πŸ“Š COMPARACIΓ“N: OPCIONES DE HF{Colors.END}")
print(f"{Colors.BLUE}{'='*70}{Colors.END}\n")
comparison_data = """
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OpciΓ³n β”‚ Setup β”‚ Costo β”‚ GPU β”‚ Mejor para β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Spaces β”‚ 10 min β”‚ Gratis β”‚ βœ… β”‚ UI + Agente β”‚
β”‚ Model Hub β”‚ 5 min β”‚ Gratis β”‚ ❌ β”‚ Guardar modelo β”‚
β”‚ Datasets β”‚ 5 min β”‚ Gratis β”‚ ❌ β”‚ Guardar estado β”‚
β”‚ Endpoints β”‚ 2 min β”‚ $9/mo β”‚ βœ… β”‚ API Inference β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
"""
print(comparison_data)
def main():
header()
option = choose_hf_option()
creds = get_hf_credentials()
comparison()
if option == "spaces":
setup_hf_spaces(creds)
elif option == "model":
setup_hf_model(creds)
elif option == "dataset":
setup_hf_dataset(creds)
elif option == "endpoints":
setup_hf_endpoints(creds)
print(f"\n{Colors.GREEN}{'='*70}{Colors.END}")
print(f"{Colors.GREEN}βœ… ConfiguraciΓ³n completada{Colors.END}")
print(f"{Colors.GREEN}{'='*70}{Colors.END}\n")
print(f"Usuario HF: {creds['hf_user']}")
print(f"DocumentaciΓ³n: HF_BUCKET_HERMES3.txt\n")
if __name__ == "__main__":
main()

Xet Storage Details

Size:
10.8 kB
Β·
Xet hash:
e3dc59da9ef3b2ff43f3f8134abae63b7f5bf39275f85825f26a13aaaa473c1d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.