Spaces:
Paused
Paused
Commit
·
cbb7bf7
1
Parent(s):
142c3be
This should work then
Browse files- Dockerfile +3 -3
- main/app.py +3 -107
- main/test_locally.py +56 -0
- main/utils/validation.py +34 -1
Dockerfile
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
FROM python:3.
|
| 2 |
|
| 3 |
RUN useradd -m -u 1000 user
|
| 4 |
USER user
|
|
@@ -14,5 +14,5 @@ COPY --chown=user main/ /app/main
|
|
| 14 |
|
| 15 |
EXPOSE 7860
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
CMD ["
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
|
| 3 |
RUN useradd -m -u 1000 user
|
| 4 |
USER user
|
|
|
|
| 14 |
|
| 15 |
EXPOSE 7860
|
| 16 |
|
| 17 |
+
# We run the app object in the app.py file in the main folder.
|
| 18 |
+
CMD ["uvicorn", "main.app:app", "--host", "0.0.0.0", "--port", "7860"]
|
main/app.py
CHANGED
|
@@ -2,44 +2,11 @@ import yaml
|
|
| 2 |
import sys
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
-
import uvicorn
|
| 6 |
-
from .api import LLMApi
|
| 7 |
from .routes import router, init_router
|
| 8 |
from .utils.logging import setup_logger
|
| 9 |
-
from
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
from dotenv import load_dotenv
|
| 12 |
-
import os
|
| 13 |
|
| 14 |
-
def validate_hf():
|
| 15 |
-
"""
|
| 16 |
-
Validate Hugging Face authentication.
|
| 17 |
-
Checks for .env file, loads environment variables, and attempts HF login if token exists.
|
| 18 |
-
"""
|
| 19 |
-
logger = setup_logger(config, "hf_validation")
|
| 20 |
|
| 21 |
-
# Check for .env file
|
| 22 |
-
env_path = Path('.env')
|
| 23 |
-
if env_path.exists():
|
| 24 |
-
logger.info("Found .env file, loading environment variables")
|
| 25 |
-
load_dotenv()
|
| 26 |
-
else:
|
| 27 |
-
logger.warning("No .env file found. Fine if you're on Huggingface, but you need one to run locally on your PC.")
|
| 28 |
-
|
| 29 |
-
# Check for HF token
|
| 30 |
-
hf_token = os.getenv('HF_TOKEN')
|
| 31 |
-
if not hf_token:
|
| 32 |
-
logger.error("No HF_TOKEN found in environment variables")
|
| 33 |
-
return False
|
| 34 |
-
|
| 35 |
-
try:
|
| 36 |
-
# Attempt login
|
| 37 |
-
login(token=hf_token)
|
| 38 |
-
logger.info("Successfully authenticated with Hugging Face")
|
| 39 |
-
return True
|
| 40 |
-
except Exception as e:
|
| 41 |
-
logger.error(f"Failed to authenticate with Hugging Face: {str(e)}")
|
| 42 |
-
return False
|
| 43 |
|
| 44 |
def load_config():
|
| 45 |
"""Load configuration from yaml file"""
|
|
@@ -49,6 +16,7 @@ def load_config():
|
|
| 49 |
def create_app():
|
| 50 |
config = load_config()
|
| 51 |
logger = setup_logger(config, "main")
|
|
|
|
| 52 |
logger.info("Starting LLM API server")
|
| 53 |
|
| 54 |
app = FastAPI(
|
|
@@ -74,76 +42,4 @@ def create_app():
|
|
| 74 |
logger.info("FastAPI application created successfully")
|
| 75 |
return app
|
| 76 |
|
| 77 |
-
|
| 78 |
-
"""Run local tests for development and debugging"""
|
| 79 |
-
config = load_config()
|
| 80 |
-
logger = setup_logger(config, "test")
|
| 81 |
-
logger.info("Starting local tests")
|
| 82 |
-
|
| 83 |
-
api = LLMApi(config)
|
| 84 |
-
model_name = config["model"]["defaults"]["model_name"]
|
| 85 |
-
|
| 86 |
-
logger.info(f"Testing with model: {model_name}")
|
| 87 |
-
|
| 88 |
-
# Test download
|
| 89 |
-
logger.info("Testing model download...")
|
| 90 |
-
api.download_model(model_name)
|
| 91 |
-
logger.info("Download complete")
|
| 92 |
-
|
| 93 |
-
# Test initialization
|
| 94 |
-
logger.info("Initializing model...")
|
| 95 |
-
api.initialize_model(model_name)
|
| 96 |
-
logger.info("Model initialized")
|
| 97 |
-
|
| 98 |
-
# Test embedding
|
| 99 |
-
test_text = "Dette er en test av embeddings generering fra en teknisk tekst om HMS rutiner på arbeidsplassen."
|
| 100 |
-
logger.info("Testing embedding generation...")
|
| 101 |
-
embedding = api.generate_embedding(test_text)
|
| 102 |
-
logger.info(f"Generated embedding of length: {len(embedding)}")
|
| 103 |
-
logger.info(f"First few values: {embedding[:5]}")
|
| 104 |
-
|
| 105 |
-
# Test generation
|
| 106 |
-
test_prompts = [
|
| 107 |
-
"Tell me what happens in a nuclear reactor.",
|
| 108 |
-
]
|
| 109 |
-
|
| 110 |
-
# Test regular generation
|
| 111 |
-
logger.info("Testing regular generation:")
|
| 112 |
-
for prompt in test_prompts:
|
| 113 |
-
logger.info(f"Prompt: {prompt}")
|
| 114 |
-
response = api.generate_response(
|
| 115 |
-
prompt=prompt,
|
| 116 |
-
system_message="You are a helpful assistant."
|
| 117 |
-
)
|
| 118 |
-
logger.info(f"Response: {response}")
|
| 119 |
-
|
| 120 |
-
# Test streaming generation
|
| 121 |
-
logger.info("Testing streaming generation:")
|
| 122 |
-
logger.info(f"Prompt: {test_prompts[0]}")
|
| 123 |
-
for chunk in api.generate_stream(
|
| 124 |
-
prompt=test_prompts[0],
|
| 125 |
-
system_message="You are a helpful assistant."
|
| 126 |
-
):
|
| 127 |
-
print(chunk, end="", flush=True)
|
| 128 |
-
print("\n")
|
| 129 |
-
|
| 130 |
-
logger.info("Local tests completed")
|
| 131 |
-
|
| 132 |
-
app = create_app()
|
| 133 |
-
|
| 134 |
-
if __name__ == "__main__":
|
| 135 |
-
config = load_config()
|
| 136 |
-
#validate_hf()
|
| 137 |
-
if len(sys.argv) > 1 and sys.argv[1] == "test":
|
| 138 |
-
test_locally()
|
| 139 |
-
else:
|
| 140 |
-
uvicorn.run(
|
| 141 |
-
"main.app:app",
|
| 142 |
-
host=config["server"]["host"],
|
| 143 |
-
port=config["server"]["port"],
|
| 144 |
-
log_level="trace",
|
| 145 |
-
reload=True,
|
| 146 |
-
workers=1,
|
| 147 |
-
access_log=False,
|
| 148 |
-
use_colors=True
|
| 149 |
-
)
|
|
|
|
| 2 |
import sys
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
| 5 |
from .routes import router, init_router
|
| 6 |
from .utils.logging import setup_logger
|
| 7 |
+
from .utils.validation import validate_hf
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def load_config():
|
| 12 |
"""Load configuration from yaml file"""
|
|
|
|
| 16 |
def create_app():
|
| 17 |
config = load_config()
|
| 18 |
logger = setup_logger(config, "main")
|
| 19 |
+
validate_hf(setup_logger, config)
|
| 20 |
logger.info("Starting LLM API server")
|
| 21 |
|
| 22 |
app = FastAPI(
|
|
|
|
| 42 |
logger.info("FastAPI application created successfully")
|
| 43 |
return app
|
| 44 |
|
| 45 |
+
app = create_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main/test_locally.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
def test_locally(load_config, setup_logger, LLMApi):
|
| 4 |
+
"""Run local tests for development and debugging"""
|
| 5 |
+
config = load_config()
|
| 6 |
+
logger = setup_logger(config, "test")
|
| 7 |
+
logger.info("Starting local tests")
|
| 8 |
+
|
| 9 |
+
api = LLMApi(config)
|
| 10 |
+
model_name = config["model"]["defaults"]["model_name"]
|
| 11 |
+
|
| 12 |
+
logger.info(f"Testing with model: {model_name}")
|
| 13 |
+
|
| 14 |
+
# Test download
|
| 15 |
+
logger.info("Testing model download...")
|
| 16 |
+
api.download_model(model_name)
|
| 17 |
+
logger.info("Download complete")
|
| 18 |
+
|
| 19 |
+
# Test initialization
|
| 20 |
+
logger.info("Initializing model...")
|
| 21 |
+
api.initialize_model(model_name)
|
| 22 |
+
logger.info("Model initialized")
|
| 23 |
+
|
| 24 |
+
# Test embedding
|
| 25 |
+
test_text = "Dette er en test av embeddings generering fra en teknisk tekst om HMS rutiner på arbeidsplassen."
|
| 26 |
+
logger.info("Testing embedding generation...")
|
| 27 |
+
embedding = api.generate_embedding(test_text)
|
| 28 |
+
logger.info(f"Generated embedding of length: {len(embedding)}")
|
| 29 |
+
logger.info(f"First few values: {embedding[:5]}")
|
| 30 |
+
|
| 31 |
+
# Test generation
|
| 32 |
+
test_prompts = [
|
| 33 |
+
"Tell me what happens in a nuclear reactor.",
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
# Test regular generation
|
| 37 |
+
logger.info("Testing regular generation:")
|
| 38 |
+
for prompt in test_prompts:
|
| 39 |
+
logger.info(f"Prompt: {prompt}")
|
| 40 |
+
response = api.generate_response(
|
| 41 |
+
prompt=prompt,
|
| 42 |
+
system_message="You are a helpful assistant."
|
| 43 |
+
)
|
| 44 |
+
logger.info(f"Response: {response}")
|
| 45 |
+
|
| 46 |
+
# Test streaming generation
|
| 47 |
+
logger.info("Testing streaming generation:")
|
| 48 |
+
logger.info(f"Prompt: {test_prompts[0]}")
|
| 49 |
+
for chunk in api.generate_stream(
|
| 50 |
+
prompt=test_prompts[0],
|
| 51 |
+
system_message="You are a helpful assistant."
|
| 52 |
+
):
|
| 53 |
+
print(chunk, end="", flush=True)
|
| 54 |
+
print("\n")
|
| 55 |
+
|
| 56 |
+
logger.info("Local tests completed")
|
main/utils/validation.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def validate_model_path(model_path: Path) -> bool:
|
| 5 |
"""Validate that a model path exists and contains necessary files"""
|
|
@@ -20,4 +23,34 @@ def validate_generation_params(params: Dict[str, Any]) -> Dict[str, Any]:
|
|
| 20 |
if 'max_new_tokens' in validated:
|
| 21 |
validated['max_new_tokens'] = max(1, min(4096, validated['max_new_tokens']))
|
| 22 |
|
| 23 |
-
return validated
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
from pathlib import Path
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from huggingface_hub import login
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
def validate_model_path(model_path: Path) -> bool:
|
| 8 |
"""Validate that a model path exists and contains necessary files"""
|
|
|
|
| 23 |
if 'max_new_tokens' in validated:
|
| 24 |
validated['max_new_tokens'] = max(1, min(4096, validated['max_new_tokens']))
|
| 25 |
|
| 26 |
+
return validated
|
| 27 |
+
|
| 28 |
+
def validate_hf(setup_logger, config):
|
| 29 |
+
"""
|
| 30 |
+
Validate Hugging Face authentication.
|
| 31 |
+
Checks for .env file, loads environment variables, and attempts HF login if token exists.
|
| 32 |
+
"""
|
| 33 |
+
logger = setup_logger(config, "hf_validation")
|
| 34 |
+
|
| 35 |
+
# Check for .env file
|
| 36 |
+
env_path = Path('.env')
|
| 37 |
+
if env_path.exists():
|
| 38 |
+
logger.info("Found .env file, loading environment variables")
|
| 39 |
+
load_dotenv()
|
| 40 |
+
else:
|
| 41 |
+
logger.warning("No .env file found. Fine if you're on Huggingface, but you need one to run locally on your PC.")
|
| 42 |
+
|
| 43 |
+
# Check for HF token
|
| 44 |
+
hf_token = os.getenv('HF_TOKEN')
|
| 45 |
+
if not hf_token:
|
| 46 |
+
logger.error("No HF_TOKEN found in environment variables")
|
| 47 |
+
return False
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
# Attempt login
|
| 51 |
+
login(token=hf_token)
|
| 52 |
+
logger.info("Successfully authenticated with Hugging Face")
|
| 53 |
+
return True
|
| 54 |
+
except Exception as e:
|
| 55 |
+
logger.error(f"Failed to authenticate with Hugging Face: {str(e)}")
|
| 56 |
+
return False
|