Skip to content

Commit 95f0f89

Browse files
Merge pull request #18 from lab68dev/copilot/sub-pr-17
[WIP] WIP Address feedback on AI model training feature implementation
2 parents 68b2710 + e0f5f8a commit 95f0f89

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

.gitignore

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ yarn-error.log*
2424

2525
# typescript
2626
*.tsbuildinfo
27-
next-env.d.ts
27+
next-env.d.ts
28+
29+
# Python
30+
__pycache__/
31+
*.py[cod]
32+
*$py.class
33+
*.so
34+
.Python
35+
venv/
36+
ENV/
37+
.venv

ai-model/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ python train.py
3434
### 4. Run Inference Server
3535

3636
```bash
37+
# Set allowed CORS origins (optional, defaults to http://localhost:3000)
38+
export ALLOWED_ORIGINS="http://localhost:3000,http://localhost:3001"
39+
40+
# Run the server
3741
python inference/server.py
3842
```
3943

44+
#### Environment Variables
45+
46+
- `ALLOWED_ORIGINS`: Comma-separated list of allowed CORS origins (default: `http://localhost:3000`)
47+
- Example: `http://localhost:3000,https://example.com`
48+
- For production, set this to your specific domain(s) for security
49+
4050
## Hardware Requirements
4151

4252
- **Minimum:** RTX 4060 (8GB VRAM)

ai-model/inference/server.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python3
22
'''FastAPI Inference Server for Lab68Dev AI Model'''
3+
import os
34
import yaml
45
import torch
6+
import logging
57
from pathlib import Path
68
from typing import Optional
79
from fastapi import FastAPI, HTTPException
@@ -10,9 +12,22 @@
1012
from transformers import AutoModelForCausalLM, AutoTokenizer
1113
from peft import PeftModel
1214

15+
logging.basicConfig(level=logging.INFO)
16+
1317
app = FastAPI(title='Lab68Dev AI API', version='1.0.0')
14-
app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_credentials=True, allow_methods=['*'], allow_headers=['*'])
1518

19+
# CORS configuration - use environment variable for allowed origins
20+
allowed_origins_str = os.getenv('ALLOWED_ORIGINS', 'http://localhost:3000')
21+
allowed_origins = [origin.strip() for origin in allowed_origins_str.split(',') if origin.strip()]
22+
app.add_middleware(
23+
CORSMiddleware,
24+
allow_origins=allowed_origins,
25+
allow_credentials=True,
26+
allow_methods=['*'],
27+
allow_headers=['*']
28+
)
29+
30+
# Global variables - set once during startup, read-only during inference
1631
model, tokenizer = None, None
1732

1833
class GenerateRequest(BaseModel):
@@ -28,6 +43,7 @@ def load_config():
2843
config_path = Path(__file__).parent.parent / 'config' / 'training_config.yaml'
2944
if config_path.exists():
3045
with open(config_path) as f: return yaml.safe_load(f)
46+
logging.warning(f'Configuration file not found at {config_path}, using default values')
3147
return {}
3248

3349
@app.on_event('startup')
@@ -36,7 +52,7 @@ async def startup():
3652
config = load_config()
3753
model_path = config.get('training', {}).get('output_dir', './models/lab68dev-assistant')
3854
base_model = config.get('model', {}).get('name', 'TinyLlama/TinyLlama-1.1B-Chat-v1.0')
39-
print('Loading model...')
55+
logging.info('Loading model...')
4056
if Path(model_path).exists():
4157
tokenizer = AutoTokenizer.from_pretrained(model_path)
4258
model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.float16, device_map='auto')
@@ -46,7 +62,7 @@ async def startup():
4662
model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.float16, device_map='auto')
4763
tokenizer.pad_token = tokenizer.eos_token
4864
model.eval()
49-
print('Model loaded!')
65+
logging.info('Model loaded successfully!')
5066

5167
@app.get('/health')
5268
async def health():

0 commit comments

Comments
 (0)