Spaces:
Running
Running
File size: 11,017 Bytes
0f93e9d 2e57c93 8159d06 6b8ac6b 1575ad0 8159d06 1575ad0 a42e4cc 8159d06 a42e4cc 8159d06 a42e4cc cf6c44b a42e4cc cf6c44b 3a65c07 b1a8675 3a65c07 b1a8675 a29ef8b b1a8675 3a65c07 b1a8675 7827bc1 4f531f0 3a65c07 6b8ac6b b1a8675 6b8ac6b b1a8675 6b8ac6b b1a8675 6b8ac6b d5da2b2 c27741d 2e57c93 3595c1e 2e57c93 d5da2b2 1ca2b19 b47770f b733726 b47770f 72f65c8 8792435 af6d411 8792435 af6d411 8792435 72f65c8 8792435 1ca2b19 3a0af00 7e9a6d2 8792435 7e9a6d2 8792435 72f65c8 7e9a6d2 72f65c8 7e9a6d2 72f65c8 7e9a6d2 72f65c8 7e9a6d2 72f65c8 7e9a6d2 1ca2b19 54ca3cf c562673 e79eedb c562673 e79eedb c562673 e79eedb c562673 e79eedb c562673 e79eedb c562673 e79eedb c562673 5a6f1a5 b628df0 e6ceacc 3b79d57 3a0af00 3b79d57 e6ceacc 3b79d57 e6ceacc 3b79d57 e6ceacc 3b79d57 e6ceacc 3b79d57 e6ceacc 3b79d57 e6ceacc 3b79d57 3a0af00 1ca2b19 3a65c07 8792435 9dafd74 8792435 1ca2b19 8792435 1ca2b19 0bb77b3 c8bbed1 a29ef8b ec0b4e0 a29ef8b ec0b4e0 5a536b8 a29ef8b ec0b4e0 a29ef8b ec0b4e0 a29ef8b ec0b4e0 a29ef8b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
import os
import tempfile
import logging
from huggingface_hub import HfApi
from dotenv import load_dotenv
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
# Get token with fallback
HF_TOKEN = os.getenv('HUGGINGFACE_TOKEN')
if not HF_TOKEN:
raise ValueError("HUGGINGFACE_TOKEN not found in environment variables")
# Validate token format
if not HF_TOKEN.startswith('hf_'):
raise ValueError("Invalid Hugging Face token format")
print(f"Token loaded successfully: {HF_TOKEN[:5]}...")
# API Configuration
API_CONFIG = {
"inference_endpoint": "https://api-inference.huggingface.co",
"token": HF_TOKEN,
"is_paid_tier": False, # Принудительно устанавливаем бесплатный режим
"timeout": 15,
"max_retries": 1,
"headers": {
"X-Use-Cache": "true", # Включаем кэширование для бесплатного тарифа
"Content-Type": "application/json",
"Authorization": f"Bearer {HF_TOKEN}"
}
}
def check_account_type():
"""
Simplified account check for free tier
Returns:
tuple: (is_pro: bool, account_type: str)
"""
return False, "free"
# Устанавливаем базовые настройки для free tier
IS_PRO_ACCOUNT, ACCOUNT_TYPE = False, "free"
DEFAULT_MODEL = "zephyr-7b" # Устанавливаем дефолтную бесплатную модель
# Dataset configuration
DATASET_ID = "Rulga/status-law-knowledge-base"
# Dataset paths
DATASET_CHAT_HISTORY_PATH = "chat_history"
DATASET_VECTOR_STORE_PATH = "vector_store"
DATASET_FINE_TUNED_PATH = "fine_tuned_models"
DATASET_ANNOTATIONS_PATH = "annotations"
DATASET_ERROR_LOGS_PATH = "error_logs"
DATASET_PREFERENCES_PATH = "preferences/user_preferences.json"
# Adding training data paths
DATASET_TRAINING_DATA_PATH = "training_data"
DATASET_TRAINING_LOGS_PATH = "training_logs"
# Temporary storage (using system temp directory)
TEMP_DIR = tempfile.gettempdir()
TEMP_ROOT = os.path.join(TEMP_DIR, "status_law_kb")
CHAT_HISTORY_PATH = os.path.join(TEMP_ROOT, "chat_history")
VECTOR_STORE_PATH = os.path.join(TEMP_ROOT, "vector_store")
FINE_TUNED_PATH = os.path.join(TEMP_ROOT, "fine_tuned_models")
MODELS_REGISTRY_PATH = os.path.join(TEMP_ROOT, "models_registry.json")
# Create temporary directories
for path in [CHAT_HISTORY_PATH, VECTOR_STORE_PATH, FINE_TUNED_PATH]:
os.makedirs(path, exist_ok=True)
# Paths configuration
MODEL_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "models")
TRAINING_OUTPUT_DIR = os.path.join(CHAT_HISTORY_PATH, FINE_TUNED_PATH)
# Create necessary directories if they don't exist
os.makedirs(MODEL_PATH, exist_ok=True)
os.makedirs(TRAINING_OUTPUT_DIR, exist_ok=True)
# Models configuration with detailed information
MODELS = {
"zephyr-7b": {
"id": "HuggingFaceH4/zephyr-7b-beta",
"name": "Zephyr 7B",
"description": "A state-of-the-art 7B parameter language model",
"type": "base",
"parameters": {
"max_length": 2048,
"temperature": 0.7,
"top_p": 0.9,
"repetition_penalty": 1.1,
},
"training": {
"base_model_path": "HuggingFaceH4/zephyr-7b-beta",
"fine_tuned_path": os.path.join(TRAINING_OUTPUT_DIR, "zephyr-7b-beta-tuned"),
"lora_config": {
"r": 16,
"lora_alpha": 32,
"lora_dropout": 0.05,
"target_modules": ["q_proj", "v_proj", "k_proj", "o_proj"]
}
},
"details": {
"full_name": "HuggingFaceH4 Zephyr 7B Beta",
"capabilities": [
"High performance on instruction-following tasks",
"Good response accuracy",
"Advanced reasoning capabilities",
"Excellent text generation quality"
],
"limitations": [
"May require paid API for usage",
"Limited support for languages other than English",
"Less optimization for legal topics compared to specialized models"
],
"use_cases": [
"Complex legal reasoning",
"Case analysis",
"Legal research",
"Structured legal text generation"
],
"documentation": "https://huggingface.co/HuggingFaceH4/zephyr-7b-beta"
}
},
"llama-7b": {
"id": "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
"name": "TinyLlama 1.1B Chat",
"description": "Lightweight chat model with excellent performance for its size",
"type": "base",
"parameters": {
"max_length": 2048,
"temperature": 0.7,
"top_p": 0.9,
"repetition_penalty": 1.1,
},
"training": {
"base_model_path": "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
"fine_tuned_path": os.path.join(TRAINING_OUTPUT_DIR, "tinyllama-1.1b-chat-tuned"),
"lora_config": {
"r": 16,
"lora_alpha": 32,
"lora_dropout": 0.05,
"target_modules": ["q_proj", "v_proj", "k_proj", "o_proj"]
}
},
"details": {
"full_name": "TinyLlama 1.1B Chat v1.0",
"capabilities": [
"Efficient resource usage",
"Fast inference speed",
"Good for basic chat interactions",
"Can run on CPU",
"Works on devices with limited RAM",
"Open source and free to use"
],
"limitations": [
"Lower capacity compared to larger models",
"May struggle with complex reasoning",
"Limited context window",
"Less specialized knowledge"
],
"use_cases": [
"Quick legal queries",
"Basic document analysis",
"Resource-constrained environments",
"Mobile applications",
"Edge devices"
],
"documentation": "https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0"
}
},
"mixtral-8x7b": {
"id": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"name": "Mixtral 8x7B Instruct",
"description": "Powerful mixture-of-experts model with strong multilingual capabilities",
"type": "base",
"parameters": {
"max_length": 2048,
"temperature": 0.7,
"top_p": 0.9,
"repetition_penalty": 1.1,
},
"training": {
"base_model_path": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"fine_tuned_path": os.path.join(TRAINING_OUTPUT_DIR, "mixtral-8x7b-instruct-tuned"),
"lora_config": {
"r": 16,
"lora_alpha": 32,
"lora_dropout": 0.05,
"target_modules": ["q_proj", "v_proj", "k_proj", "o_proj"]
}
},
"details": {
"full_name": "Mixtral 8x7B Instruct v0.1",
"capabilities": [
"Excellent multilingual support",
"Strong reasoning capabilities",
"Superior instruction following",
"High-quality text generation"
],
"limitations": [
"Larger model size",
"May need domain-specific prompting",
"Higher resource requirements"
],
"use_cases": [
"Complex legal analysis",
"Multilingual consultation",
"Advanced document processing",
"Cross-lingual communication"
],
"documentation": "https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1"
}
},
"neural-mistral": { # заменяем phi-2
"id": "teknium/Neural-Mistral-7B-v0.1",
"name": "Neural Mistral 7B",
"description": "Enhanced version of Mistral with improved reasoning and instruction following",
"type": "base",
"parameters": {
"max_length": 2048,
"temperature": 0.7,
"top_p": 0.9,
"repetition_penalty": 1.1,
},
"training": {
"base_model_path": "teknium/Neural-Mistral-7B-v0.1",
"fine_tuned_path": os.path.join(TRAINING_OUTPUT_DIR, "neural-mistral-7b-tuned"),
"lora_config": {
"r": 16,
"lora_alpha": 32,
"lora_dropout": 0.05,
"target_modules": ["q_proj", "v_proj", "k_proj", "o_proj"]
}
},
"details": {
"full_name": "Neural Mistral 7B v0.1",
"capabilities": [
"Enhanced reasoning capabilities",
"Improved instruction following",
"Strong multilingual support",
"Better context understanding",
"Advanced problem-solving abilities",
"Consistent output quality"
],
"limitations": [
"Requires more GPU memory",
"May be slower than smaller models",
"Resource intensive for fine-tuning"
],
"use_cases": [
"Complex legal analysis",
"Advanced reasoning tasks",
"Detailed document processing",
"Professional consultation",
"Research assistance"
],
"documentation": "https://huggingface.co/teknium/Neural-Mistral-7B-v0.1"
}
}
}
# Update MODELS configuration
for model in MODELS.values():
model["endpoint"] = API_CONFIG["inference_endpoint"]
# Default model
DEFAULT_MODEL = "zephyr-7b" # Changed from "llama-7b" to "zephyr-7b"
ACTIVE_MODEL = MODELS[DEFAULT_MODEL]
# Embedding model for vector store
EMBEDDING_MODEL = "intfloat/multilingual-e5-large"
# Request settings
USER_AGENT = "Status-Law-Assistant/1.0"
# Add these constants to settings.py
RATING_FIELDS = {
"accuracy": "Точность ответа",
"completeness": "Полнота информации",
"relevance": "Релевантность ответу",
"clarity": "Ясность изложения",
"legal_correctness": "Юридическая корректность"
}
CHAT_HISTORY_SCHEMA = {
"conversation_id": str,
"timestamp": str, # ISO format
"history": [
{
"role": str, # "user" or "assistant"
"content": str,
"timestamp": str # ISO format
}
]
}
ANNOTATION_SCHEMA = {
"conversation_id": str,
"timestamp": str,
"question": str,
"original_answer": str,
"improved_answer": str,
"ratings": {field: int for field in RATING_FIELDS}, # все оценки 1-5
"notes": str
}
|