Personaz1 commited on
Commit
947e2b2
·
1 Parent(s): e6525c4

ΔΣ::TORI - Integrate huggingface_hub for proper LLM API access

Browse files
Files changed (4) hide show
  1. SETUP.md +25 -1
  2. config.py +5 -1
  3. llm_integration.py +33 -58
  4. requirements.txt +2 -1
SETUP.md CHANGED
@@ -1 +1,25 @@
1
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ΔΣ::TORI - Настройка API Токена
2
+
3
+ ## Для работы LLM анализа необходимо настроить API токен
4
+
5
+ ### 1. В Hugging Face Space Settings:
6
+ 1. Перейдите в https://huggingface.co/spaces/stephansolncev/TORI/settings
7
+ 2. В разделе "Repository secrets" добавьте:
8
+ - **Name**: `HF_TOKEN`
9
+ - **Value**: [Ваш Hugging Face API токен]
10
+
11
+ ### 2. Локальная разработка:
12
+ ```bash
13
+ export HF_TOKEN=[ваш_токен]
14
+ ```
15
+
16
+ ### 3. Проверка:
17
+ После настройки токена система будет:
18
+ - ✅ Выполнять LLM анализ состояния сознания через microsoft/phi-1_5
19
+ - ✅ Генерировать рекомендации по саморегуляции
20
+ - ✅ Анализировать феноменологические метрики
21
+
22
+ ### 4. Без токена:
23
+ - ⚠️ Система работает в fallback режиме с улучшенным анализом
24
+ - ⚠️ LLM анализ недоступен
25
+ - ✅ Все остальные функции работают нормально
config.py CHANGED
@@ -5,7 +5,11 @@ Configuration for TORI Consciousness System
5
  import os
6
 
7
  # Hugging Face API Configuration
8
- HF_API_TOKEN = os.getenv("HF_API_TOKEN", "hf_test_token")
 
 
 
 
9
 
10
  # Logging Configuration
11
  LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
 
5
  import os
6
 
7
  # Hugging Face API Configuration
8
+ # Токен должен быть установлен как переменная окружения HF_TOKEN
9
+ HF_API_TOKEN = os.getenv("HF_TOKEN")
10
+ if not HF_API_TOKEN:
11
+ print("⚠️ Warning: HF_TOKEN not set. LLM analysis will use fallback mode.")
12
+ HF_API_TOKEN = "hf_test_token"
13
 
14
  # Logging Configuration
15
  LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
llm_integration.py CHANGED
@@ -11,6 +11,7 @@ import time
11
  import logging
12
  from typing import Dict, List, Optional, Any
13
  import os
 
14
  from config import HF_API_TOKEN, MODEL_URL
15
 
16
  # Настройка логирования
@@ -34,12 +35,19 @@ class LLMAnalyzer:
34
  """
35
  # Используем конфигурацию
36
  self.api_token = api_token or HF_API_TOKEN
37
- self.model_url = MODEL_URL
38
  logger.info(f"Initialized LLM analyzer with API token: {self.api_token[:10]}...")
39
- self.headers = {
40
- "Authorization": f"Bearer {self.api_token}",
41
- "Content-Type": "application/json"
42
- }
 
 
 
 
 
 
 
43
 
44
  # Кэш для анализа
45
  self.analysis_cache = {}
@@ -131,75 +139,42 @@ class LLMAnalyzer:
131
 
132
  def _make_llm_request(self, prompt: str) -> Dict[str, Any]:
133
  """
134
- Выполнение запроса к LLM API.
135
  """
136
  try:
137
  self.request_count += 1
138
- logger.info(f"Making LLM request #{self.request_count} to {self.model_url}")
139
  logger.debug(f"Request payload: {prompt[:200]}...")
140
 
141
- # Проверяем доступность модели
142
- health_check = requests.get(self.model_url, headers=self.headers, timeout=10)
143
- if health_check.status_code != 200:
144
- logger.warning(f"Model not available, using fallback analysis")
145
  return {
146
  "success": False,
147
- "error": f"Model not available: {health_check.status_code}"
148
  }
149
 
150
- payload = {
151
- "inputs": prompt,
152
- "parameters": {
153
- "max_new_tokens": 512,
154
- "temperature": 0.7,
155
- "top_p": 0.9,
156
- "do_sample": True,
157
- "return_full_text": False
158
- }
159
- }
160
-
161
- response = requests.post(
162
- self.model_url,
163
- headers=self.headers,
164
- json=payload,
165
- timeout=30
166
  )
167
 
168
- if response.status_code == 200:
169
- result = response.json()
170
- logger.info(f"LLM request successful, response length: {len(str(result))}")
171
- if isinstance(result, list) and len(result) > 0:
172
- return {
173
- "success": True,
174
- "text": result[0].get("generated_text", "")
175
- }
176
- else:
177
- logger.warning(f"Unexpected response format: {result}")
178
- return {
179
- "success": False,
180
- "error": f"Unexpected response format: {result}"
181
- }
182
- else:
183
- logger.error(f"LLM API error {response.status_code}: {response.text}")
184
- return {
185
- "success": False,
186
- "error": f"API Error {response.status_code}: {response.text}"
187
- }
188
-
189
- except requests.exceptions.Timeout:
190
- return {
191
- "success": False,
192
- "error": "Request timeout"
193
- }
194
- except requests.exceptions.RequestException as e:
195
  return {
196
- "success": False,
197
- "error": f"Request failed: {str(e)}"
198
  }
 
199
  except Exception as e:
 
200
  return {
201
  "success": False,
202
- "error": f"Unexpected error: {str(e)}"
203
  }
204
 
205
  def _parse_llm_response(self, response_text: str, phenomenological_data: Dict[str, float]) -> Dict[str, Any]:
 
11
  import logging
12
  from typing import Dict, List, Optional, Any
13
  import os
14
+ from huggingface_hub import InferenceClient
15
  from config import HF_API_TOKEN, MODEL_URL
16
 
17
  # Настройка логирования
 
35
  """
36
  # Используем конфигурацию
37
  self.api_token = api_token or HF_API_TOKEN
38
+ self.model_name = "microsoft/phi-1_5" # Используем оригинальную модель
39
  logger.info(f"Initialized LLM analyzer with API token: {self.api_token[:10]}...")
40
+
41
+ # Инициализируем Inference Client
42
+ try:
43
+ self.client = InferenceClient(
44
+ provider="featherless-ai",
45
+ api_key=self.api_token
46
+ )
47
+ logger.info("InferenceClient initialized successfully")
48
+ except Exception as e:
49
+ logger.warning(f"Failed to initialize InferenceClient: {e}")
50
+ self.client = None
51
 
52
  # Кэш для анализа
53
  self.analysis_cache = {}
 
139
 
140
  def _make_llm_request(self, prompt: str) -> Dict[str, Any]:
141
  """
142
+ Выполнение запроса к LLM API через huggingface_hub.
143
  """
144
  try:
145
  self.request_count += 1
146
+ logger.info(f"Making LLM request #{self.request_count} to {self.model_name}")
147
  logger.debug(f"Request payload: {prompt[:200]}...")
148
 
149
+ # Проверяем доступность клиента
150
+ if self.client is None:
151
+ logger.warning("InferenceClient not available, using fallback analysis")
 
152
  return {
153
  "success": False,
154
+ "error": "InferenceClient not initialized"
155
  }
156
 
157
+ # Выполняем запрос через huggingface_hub
158
+ result = self.client.text_generation(
159
+ prompt,
160
+ model=self.model_name,
161
+ max_new_tokens=512,
162
+ temperature=0.7,
163
+ top_p=0.9,
164
+ do_sample=True
 
 
 
 
 
 
 
 
165
  )
166
 
167
+ logger.info(f"LLM request successful, response length: {len(str(result))}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  return {
169
+ "success": True,
170
+ "text": result
171
  }
172
+
173
  except Exception as e:
174
+ logger.error(f"LLM request failed: {str(e)}")
175
  return {
176
  "success": False,
177
+ "error": f"Request failed: {str(e)}"
178
  }
179
 
180
  def _parse_llm_response(self, response_text: str, phenomenological_data: Dict[str, float]) -> Dict[str, Any]:
requirements.txt CHANGED
@@ -12,4 +12,5 @@ matplotlib>=3.5.0
12
  seaborn>=0.11.0
13
  plotly>=5.10.0
14
  pandas>=1.4.0
15
- scipy>=1.9.0
 
 
12
  seaborn>=0.11.0
13
  plotly>=5.10.0
14
  pandas>=1.4.0
15
+ scipy>=1.9.0
16
+ huggingface_hub>=0.19.0