ciyidogan commited on
Commit
343f61b
·
verified ·
1 Parent(s): d40867b

Delete config_models.py

Browse files
Files changed (1) hide show
  1. config_models.py +0 -353
config_models.py DELETED
@@ -1,353 +0,0 @@
1
- """
2
- Configuration Models for Flare Platform
3
- """
4
- from pydantic import BaseModel, Field, field_serializer
5
- from datetime import datetime
6
- from typing import Optional, List, Dict, Any
7
-
8
- class BaseModelWithDatetime(BaseModel):
9
- """Base model with consistent datetime serialization"""
10
-
11
- class Config:
12
- # Datetime'ları her zaman ISO 8601 formatında serialize et
13
- json_encoders = {
14
- datetime: lambda v: v.isoformat() if v else None
15
- }
16
-
17
- # ===================== User & Auth =====================
18
- class UserConfig(BaseModelWithDatetime):
19
- username: str
20
- password_hash: str
21
- salt: Optional[str] = None
22
-
23
-
24
- # ===================== Provider Models =====================
25
- class ProviderDefinition(BaseModelWithDatetime):
26
- type: str # llm, tts, stt
27
- name: str
28
- display_name: str
29
- requires_endpoint: bool
30
- requires_api_key: bool
31
- requires_repo_info: Optional[bool] = False
32
- description: str
33
- features: Optional[Dict[str, Any]] = Field(default_factory=dict)
34
-
35
- def has_feature(self, feature_name: str) -> bool:
36
- """Check if provider has a specific feature"""
37
- return feature_name in self.features if self.features else False
38
-
39
- def get_feature(self, feature_name: str, default: Any = None) -> Any:
40
- """Get feature value with default"""
41
- if not self.features:
42
- return default
43
- return self.features.get(feature_name, default)
44
-
45
- def get_feature_bool(self, feature_name: str, default: bool = False) -> bool:
46
- """Get boolean feature value"""
47
- if not self.features:
48
- return default
49
- value = self.features.get(feature_name, default)
50
- if isinstance(value, bool):
51
- return value
52
- if isinstance(value, str):
53
- return value.lower() in ('true', '1', 'yes', 'on')
54
- return bool(value)
55
-
56
- def get_feature_int(self, feature_name: str, default: int = 0) -> int:
57
- """Get integer feature value"""
58
- if not self.features:
59
- return default
60
- value = self.features.get(feature_name, default)
61
- try:
62
- return int(value)
63
- except (ValueError, TypeError):
64
- return default
65
-
66
- def get_feature_str(self, feature_name: str, default: str = "") -> str:
67
- """Get string feature value"""
68
- if not self.features:
69
- return default
70
- value = self.features.get(feature_name, default)
71
- return str(value) if value is not None else default
72
-
73
-
74
- class ProviderSettings(BaseModelWithDatetime):
75
- name: str
76
- api_key: Optional[str] = None
77
- endpoint: Optional[str] = None
78
- settings: Optional[Dict[str, Any]] = Field(default_factory=dict)
79
-
80
- # ===================== Global Config =====================
81
- class GlobalConfig(BaseModelWithDatetime):
82
- llm_provider: ProviderSettings = Field(
83
- default_factory=lambda: ProviderSettings(
84
- name="spark_cloud",
85
- api_key="",
86
- endpoint="http://localhost:8080",
87
- settings={}
88
- )
89
- )
90
- tts_provider: ProviderSettings = Field(
91
- default_factory=lambda: ProviderSettings(
92
- name="no_tts",
93
- api_key="",
94
- endpoint=None,
95
- settings={}
96
- )
97
- )
98
- stt_provider: ProviderSettings = Field(
99
- default_factory=lambda: ProviderSettings(
100
- name="no_stt",
101
- api_key="",
102
- endpoint=None,
103
- settings={}
104
- )
105
- )
106
- providers: List[ProviderDefinition] = Field(default_factory=list)
107
- users: List[UserConfig] = Field(default_factory=list)
108
- last_update_date: Optional[str] = None
109
- last_update_user: Optional[str] = None
110
-
111
- def is_cloud_mode(self) -> bool:
112
- """Check if running in cloud mode"""
113
- import os
114
- return bool(os.environ.get("SPACE_ID"))
115
-
116
- def get_provider_config(self, provider_type: str, provider_name: str) -> Optional[ProviderDefinition]:
117
- """Get provider definition by type and name"""
118
- return next(
119
- (p for p in self.providers if p.type == provider_type and p.name == provider_name),
120
- None
121
- )
122
-
123
-
124
- # ===================== Localization Models =====================
125
- class LocalizedExample(BaseModelWithDatetime):
126
- locale_code: str
127
- example: str
128
-
129
-
130
- class LocalizedCaption(BaseModelWithDatetime):
131
- locale_code: str
132
- caption: str
133
-
134
-
135
- # ===================== Parameter Models =====================
136
- class ParameterConfig(BaseModelWithDatetime):
137
- name: str
138
- caption: List[LocalizedCaption]
139
- type: str # str, int, float, bool, date
140
- required: bool = True
141
- variable_name: str
142
- extraction_prompt: Optional[str] = None
143
- validation_regex: Optional[str] = None
144
- invalid_prompt: Optional[str] = None
145
- type_error_prompt: Optional[str] = None
146
-
147
- def canonical_type(self) -> str:
148
- """Get canonical type name"""
149
- return self.type.lower()
150
-
151
- def get_caption_for_locale(self, locale: str) -> str:
152
- """Get caption for specific locale"""
153
- for cap in self.caption:
154
- if cap.locale_code == locale:
155
- return cap.caption
156
- # Fallback to first caption
157
- return self.caption[0].caption if self.caption else self.name
158
-
159
-
160
- # ===================== Intent Models =====================
161
- class IntentConfig(BaseModelWithDatetime):
162
- name: str
163
- caption: str
164
- requiresApproval: Optional[bool] = False
165
- dependencies: List[str] = Field(default_factory=list)
166
- examples: List[LocalizedExample] = Field(default_factory=list)
167
- detection_prompt: str
168
- parameters: List[ParameterConfig] = Field(default_factory=list)
169
- action: str
170
- fallback_timeout_prompt: Optional[str] = None
171
- fallback_error_prompt: Optional[str] = None
172
-
173
- def get_examples_for_locale(self, locale: str) -> List[str]:
174
- """Get examples for specific locale"""
175
- examples = []
176
- for ex in self.examples:
177
- if ex.locale_code == locale:
178
- examples.append(ex.example)
179
-
180
- # Fallback to any available examples if locale not found
181
- if not examples and self.examples:
182
- # Try language part only (tr-TR -> tr)
183
- if '-' in locale:
184
- lang_code = locale.split('-')[0]
185
- for ex in self.examples:
186
- if ex.locale_code.startswith(lang_code):
187
- examples.append(ex.example)
188
-
189
- # If still no examples, return all examples
190
- if not examples:
191
- examples = [ex.example for ex in self.examples]
192
-
193
- return examples
194
-
195
- def get_examples_for_locale(self, locale: str) -> List[str]:
196
- """Get examples for specific locale"""
197
- examples = []
198
- for ex in self.examples:
199
- if ex.locale_code == locale:
200
- examples.append(ex.example)
201
-
202
- # Fallback to any available examples if locale not found
203
- if not examples and self.examples:
204
- # Try language part only (tr-TR -> tr)
205
- if '-' in locale:
206
- lang_code = locale.split('-')[0]
207
- for ex in self.examples:
208
- if ex.locale_code.startswith(lang_code):
209
- examples.append(ex.example)
210
-
211
- # If still no examples, return all examples
212
- if not examples:
213
- examples = [ex.example for ex in self.examples]
214
-
215
- return examples
216
-
217
-
218
- # ===================== LLM Configuration =====================
219
- class GenerationConfig(BaseModelWithDatetime):
220
- max_new_tokens: int = 512
221
- temperature: float = 0.7
222
- top_p: float = 0.9
223
- top_k: Optional[int] = None
224
- repetition_penalty: Optional[float] = None
225
- do_sample: Optional[bool] = True
226
- num_beams: Optional[int] = None
227
- length_penalty: Optional[float] = None
228
- early_stopping: Optional[bool] = None
229
-
230
-
231
- class LLMConfiguration(BaseModelWithDatetime):
232
- repo_id: str
233
- generation_config: GenerationConfig = Field(default_factory=GenerationConfig)
234
- use_fine_tune: bool = False
235
- fine_tune_zip: Optional[str] = ""
236
-
237
-
238
- # ===================== Version Models =====================
239
- class VersionConfig(BaseModelWithDatetime):
240
- no: int
241
- caption: str
242
- description: Optional[str] = None
243
- published: bool = False
244
- deleted: bool = False
245
- general_prompt: str
246
- welcome_prompt: Optional[str] = None
247
- llm: LLMConfiguration
248
- intents: List[IntentConfig] = Field(default_factory=list)
249
- created_date: str
250
- created_by: str
251
- publish_date: Optional[str] = None
252
- published_by: Optional[str] = None
253
- last_update_date: Optional[str] = None
254
- last_update_user: Optional[str] = None
255
-
256
- # ===================== Project Models =====================
257
- class ProjectConfig(BaseModelWithDatetime):
258
- id: int
259
- name: str
260
- caption: str
261
- icon: Optional[str] = "folder"
262
- description: Optional[str] = None
263
- enabled: bool = True
264
- default_locale: str = "tr"
265
- supported_locales: List[str] = Field(default_factory=lambda: ["tr"])
266
- timezone: str = "Europe/Istanbul"
267
- region: str = "tr-TR"
268
- versions: List[VersionConfig] = Field(default_factory=list)
269
- version_id_counter: int = 1
270
- deleted: bool = False
271
- created_date: str
272
- created_by: str
273
- last_update_date: Optional[str] = None
274
- last_update_user: Optional[str] = None
275
-
276
-
277
- # ===================== API Models =====================
278
- class RetryConfig(BaseModelWithDatetime):
279
- retry_count: int = 3
280
- backoff_seconds: int = 2
281
- strategy: str = "static" # static, exponential
282
-
283
-
284
- class AuthConfig(BaseModelWithDatetime):
285
- enabled: bool
286
- token_endpoint: Optional[str] = None
287
- response_token_path: Optional[str] = None
288
- token_request_body: Optional[Dict[str, Any]] = None
289
- token_refresh_endpoint: Optional[str] = None
290
- token_refresh_body: Optional[Dict[str, Any]] = None
291
-
292
-
293
- class ResponseMapping(BaseModelWithDatetime):
294
- variable_name: str
295
- type: str # str, int, float, bool, date
296
- json_path: str
297
- caption: List[LocalizedCaption]
298
-
299
-
300
- class APIConfig(BaseModelWithDatetime):
301
- name: str
302
- url: str
303
- method: str = "POST"
304
- headers: Dict[str, str] = Field(default_factory=dict)
305
- body_template: Dict[str, Any] = Field(default_factory=dict)
306
- timeout_seconds: int = 10
307
- retry: RetryConfig = Field(default_factory=RetryConfig)
308
- proxy: Optional[str] = None
309
- auth: Optional[AuthConfig] = None
310
- response_prompt: Optional[str] = None
311
- response_mappings: List[ResponseMapping] = Field(default_factory=list)
312
- deleted: bool = False
313
- created_date: Optional[str] = None
314
- created_by: Optional[str] = None
315
- last_update_date: Optional[str] = None
316
- last_update_user: Optional[str] = None
317
-
318
-
319
- # ===================== Activity Log =====================
320
- class ActivityLogEntry(BaseModelWithDatetime):
321
- id: Optional[int] = None
322
- timestamp: str
323
- username: str
324
- action: str
325
- entity_type: str
326
- entity_name: Optional[str] = None
327
- details: Optional[str] = None
328
-
329
- # ===================== Root Configuration =====================
330
- class ServiceConfig(BaseModelWithDatetime):
331
- global_config: GlobalConfig = Field(alias="config")
332
- projects: List[ProjectConfig] = Field(default_factory=list)
333
- apis: List[APIConfig] = Field(default_factory=list)
334
- activity_log: List[ActivityLogEntry] = Field(default_factory=list)
335
- project_id_counter: int = 1
336
- last_update_date: Optional[str] = None
337
- last_update_user: Optional[str] = None
338
-
339
- class Config:
340
- populate_by_name = True
341
-
342
- def build_index(self) -> None:
343
- """Build indexes for quick lookup"""
344
- # This method can be extended to build various indexes
345
- pass
346
-
347
- def get_api(self, api_name: str) -> Optional[APIConfig]:
348
- """Get API config by name"""
349
- return next((api for api in self.apis if api.name == api_name), None)
350
-
351
-
352
- # For backward compatibility - alias
353
- GlobalConfiguration = GlobalConfig