AurelioAguirre commited on
Commit
9174d0d
1 Parent(s): 78ba59d

Trying Python 3.9, again

Browse files
Files changed (4) hide show
  1. Dockerfile +2 -7
  2. main/_app.py +0 -149
  3. main/app.py +140 -33
  4. requirements.txt +49 -2
Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM python:3.10-slim
2
 
3
  # Set working directory
4
  WORKDIR /app
@@ -7,9 +7,7 @@ WORKDIR /app
7
  COPY requirements.txt .
8
 
9
  # Install dependencies and clean cache
10
- RUN pip install --no-cache-dir -r requirements.txt && \
11
- rm -rf /root/.cache/huggingface && \
12
- rm -rf /root/.cache/pip
13
 
14
  # Copy only what's needed
15
  COPY main/ ./main/
@@ -18,9 +16,6 @@ COPY main/ ./main/
18
  ENV PYTHONPATH=/app
19
  ENV PYTHONUNBUFFERED=1
20
 
21
- # Clear any cache that might have been created
22
- RUN rm -rf ~/.cache/huggingface
23
-
24
  # Expose the port
25
  EXPOSE 7680
26
 
 
1
+ FROM python:3.9
2
 
3
  # Set working directory
4
  WORKDIR /app
 
7
  COPY requirements.txt .
8
 
9
  # Install dependencies and clean cache
10
+ RUN pip install --no-cache-dir -r requirements.txt
 
 
11
 
12
  # Copy only what's needed
13
  COPY main/ ./main/
 
16
  ENV PYTHONPATH=/app
17
  ENV PYTHONUNBUFFERED=1
18
 
 
 
 
19
  # Expose the port
20
  EXPOSE 7680
21
 
main/_app.py DELETED
@@ -1,149 +0,0 @@
1
- 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 huggingface_hub import login
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"""
46
- with open("main/config.yaml", "r") as f:
47
- return yaml.safe_load(f)
48
-
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(
55
- title="LLM API",
56
- description="API for Large Language Model operations",
57
- version=config["api"]["version"]
58
- )
59
-
60
- # Add CORS middleware
61
- app.add_middleware(
62
- CORSMiddleware,
63
- allow_origins=config["api"]["cors"]["origins"],
64
- allow_credentials=config["api"]["cors"]["credentials"],
65
- allow_methods=["*"],
66
- allow_headers=["*"],
67
- )
68
-
69
- # Initialize routes with config
70
- init_router(config)
71
-
72
- app.include_router(router, prefix=f"{config['api']['prefix']}/{config['api']['version']}")
73
-
74
- logger.info("FastAPI application created successfully")
75
- return app
76
-
77
- def test_locally():
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
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
main/app.py CHANGED
@@ -1,42 +1,149 @@
 
1
  import sys
2
- print("Python Version:", sys.version)
3
- print("Starting application...")
4
-
5
  from fastapi import FastAPI
6
- print("FastAPI imported successfully")
 
 
 
 
 
 
 
 
7
 
8
- app = FastAPI(
9
- title="Minimal Test API",
10
- version="1.0.0",
11
- default_response_class=None # Disable automatic response serialization
12
- )
13
- print("FastAPI app created")
14
 
15
- @app.get("/")
16
- async def root():
17
- return {"message": "Server is running"}
 
 
 
 
18
 
19
- @app.on_event("startup")
20
- async def startup_event():
21
- print("FastAPI startup event triggered")
 
 
22
 
23
- print("Routes defined")
 
 
 
 
 
 
 
24
 
25
- if __name__ == "__main__":
26
- print("Starting uvicorn server...")
27
- import uvicorn
28
-
29
- config = uvicorn.Config(
30
- "main.app:app",
31
- host="0.0.0.0",
32
- port=7680,
33
- workers=1,
34
- log_level="info",
35
- reload=False,
36
- proxy_headers=False,
37
- server_header=False,
38
- date_header=False
 
 
 
 
 
 
 
 
 
39
  )
40
 
41
- server = uvicorn.Server(config)
42
- server.run()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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 huggingface_hub import login
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"""
46
+ with open("main/config.yaml", "r") as f:
47
+ return yaml.safe_load(f)
48
+
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(
55
+ title="LLM API",
56
+ description="API for Large Language Model operations",
57
+ version=config["api"]["version"]
58
+ )
59
+
60
+ # Add CORS middleware
61
+ app.add_middleware(
62
+ CORSMiddleware,
63
+ allow_origins=config["api"]["cors"]["origins"],
64
+ allow_credentials=config["api"]["cors"]["credentials"],
65
+ allow_methods=["*"],
66
+ allow_headers=["*"],
67
  )
68
 
69
+ # Initialize routes with config
70
+ init_router(config)
71
+
72
+ app.include_router(router, prefix=f"{config['api']['prefix']}/{config['api']['version']}")
73
+
74
+ logger.info("FastAPI application created successfully")
75
+ return app
76
+
77
+ def test_locally():
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
+ )
requirements.txt CHANGED
@@ -1,2 +1,49 @@
1
- fastapi==0.115.5
2
- uvicorn==0.32.1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-types==0.7.0
2
+ anyio==4.6.2.post1
3
+ certifi==2024.8.30
4
+ charset-normalizer==3.4.0
5
+ click==8.1.7
6
+ exceptiongroup==1.2.2
7
+ fastapi==0.115.6
8
+ filelock==3.16.1
9
+ fsspec==2024.10.0
10
+ h11==0.14.0
11
+ huggingface-hub==0.26.3
12
+ idna==3.10
13
+ Jinja2==3.1.4
14
+ MarkupSafe==3.0.2
15
+ mpmath==1.3.0
16
+ networkx==3.2.1
17
+ numpy==2.0.2
18
+ nvidia-cublas-cu12==12.4.5.8
19
+ nvidia-cuda-cupti-cu12==12.4.127
20
+ nvidia-cuda-nvrtc-cu12==12.4.127
21
+ nvidia-cuda-runtime-cu12==12.4.127
22
+ nvidia-cudnn-cu12==9.1.0.70
23
+ nvidia-cufft-cu12==11.2.1.3
24
+ nvidia-curand-cu12==10.3.5.147
25
+ nvidia-cusolver-cu12==11.6.1.9
26
+ nvidia-cusparse-cu12==12.3.1.170
27
+ nvidia-nccl-cu12==2.21.5
28
+ nvidia-nvjitlink-cu12==12.4.127
29
+ nvidia-nvtx-cu12==12.4.127
30
+ packaging==24.2
31
+ psutil==6.1.0
32
+ pydantic==2.10.3
33
+ pydantic_core==2.27.1
34
+ python-dotenv==1.0.1
35
+ PyYAML==6.0.2
36
+ regex==2024.11.6
37
+ requests==2.32.3
38
+ safetensors==0.4.5
39
+ sniffio==1.3.1
40
+ starlette==0.41.3
41
+ sympy==1.13.1
42
+ tokenizers==0.20.3
43
+ torch==2.5.1
44
+ tqdm==4.67.1
45
+ transformers==4.46.3
46
+ triton==3.1.0
47
+ typing_extensions==4.12.2
48
+ urllib3==2.2.3
49
+ uvicorn==0.32.1