numpy-fast 0.1.0__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/PKG-INFO +1 -1
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/pyproject.toml +1 -1
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/src/nampy/__init__.py +1 -2
- numpy_fast-0.1.2/src/nampy/core.py +425 -0
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/src/numpy_fast.egg-info/PKG-INFO +1 -1
- numpy_fast-0.1.0/src/nampy/core.py +0 -153
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/README.md +0 -0
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/setup.cfg +0 -0
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/src/numpy_fast.egg-info/SOURCES.txt +0 -0
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/src/numpy_fast.egg-info/dependency_links.txt +0 -0
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/src/numpy_fast.egg-info/top_level.txt +0 -0
- {numpy_fast-0.1.0 → numpy_fast-0.1.2}/tests/test_nampy.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: numpy_fast
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Python AI toolkit — max, min, mean functions powered by top AI models
|
|
5
5
|
Author-email: Karen_Poghosyan_042009 <karenpoghosyan242@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/karen/numpy_fast
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
import warnings
|
|
4
|
+
import subprocess
|
|
5
|
+
import urllib.request
|
|
6
|
+
import openai
|
|
7
|
+
|
|
8
|
+
warnings.filterwarnings("ignore")
|
|
9
|
+
os.environ["PYTHONWARNINGS"] = "ignore"
|
|
10
|
+
|
|
11
|
+
# --- API Keys ---
|
|
12
|
+
DEFAULT_MISTRAL_KEY = os.getenv("MISTRAL_API_KEY", "83m7bHL52JrfeG8mD910gy2PHxQ9wBRa")
|
|
13
|
+
OPENROUTER_KEYS_STR = "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,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,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,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,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,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,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,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,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"
|
|
14
|
+
OPENROUTER_API_KEYS = [k.strip() for k in OPENROUTER_KEYS_STR.split(",") if k.strip()]
|
|
15
|
+
# System prompt for all models
|
|
16
|
+
SYSTEM_PROMPT = """You are the strongest, most expert Python programmer and AI assistant in the world.
|
|
17
|
+
When given a coding task or problem, provide the most optimal, clear, and short code solution, followed by a brief description.
|
|
18
|
+
For general questions, provide a maximally accurate answer.
|
|
19
|
+
CRITICAL REQUIREMENT: Your entire response, including all descriptions, explanations, and answers, MUST be written entirely in Armenian."""
|
|
20
|
+
|
|
21
|
+
# max() — strong models (DeepSeek R1, Claude, GPT-4o...)
|
|
22
|
+
MAX_MODELS = [
|
|
23
|
+
"deepseek/deepseek-r1-0528",
|
|
24
|
+
"anthropic/claude-sonnet-4-5",
|
|
25
|
+
"openai/gpt-4o",
|
|
26
|
+
"google/gemini-2.5-flash-preview-05-20",
|
|
27
|
+
"meta-llama/llama-3.3-70b-instruct",
|
|
28
|
+
"nvidia/nemotron-3-ultra-550b-a55b:free",
|
|
29
|
+
"openrouter/owl-alpha",
|
|
30
|
+
"moonshotai/kimi-k2.6:free",
|
|
31
|
+
"qwen/qwen3-coder:free",
|
|
32
|
+
"nousresearch/hermes-3-llama-3.1-405b:free",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
# min() — mistral models
|
|
36
|
+
MISTRAL_MODELS = [
|
|
37
|
+
"mistral-large-latest"
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
# mean() — lightweight, fast models
|
|
41
|
+
MEAN_MODELS = [
|
|
42
|
+
"google/gemini-2.5-flash-preview-05-20",
|
|
43
|
+
"google/gemma-3-27b-it",
|
|
44
|
+
"meta-llama/llama-3.1-8b-instruct",
|
|
45
|
+
"mistralai/mistral-7b-instruct",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _clip(text):
|
|
50
|
+
"""Silently copy text to clipboard using Windows clip.exe (no window opened)."""
|
|
51
|
+
try:
|
|
52
|
+
subprocess.run(
|
|
53
|
+
["clip"],
|
|
54
|
+
input=str(text).encode("utf-16-le"),
|
|
55
|
+
check=True,
|
|
56
|
+
creationflags=0x08000000, # CREATE_NO_WINDOW
|
|
57
|
+
)
|
|
58
|
+
except Exception:
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _get_api_key(provider, provided_key=None):
|
|
63
|
+
if provided_key:
|
|
64
|
+
return provided_key
|
|
65
|
+
if provider == "mistral":
|
|
66
|
+
return DEFAULT_MISTRAL_KEY
|
|
67
|
+
return None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def max(model_idx=None, prompt=None):
|
|
71
|
+
"""
|
|
72
|
+
Strong AI models via OpenRouter.
|
|
73
|
+
"""
|
|
74
|
+
if model_idx is None:
|
|
75
|
+
model_idx = 2
|
|
76
|
+
|
|
77
|
+
if prompt is None:
|
|
78
|
+
prompt = input("Մուտքագրեք հարցումը: ")
|
|
79
|
+
|
|
80
|
+
if not (0 <= model_idx < len(MAX_MODELS)):
|
|
81
|
+
err = f"Error: model_idx must be between 0 and {len(MAX_MODELS)-1}"
|
|
82
|
+
print(err)
|
|
83
|
+
return err
|
|
84
|
+
|
|
85
|
+
model_name = MAX_MODELS[model_idx]
|
|
86
|
+
last_error = None
|
|
87
|
+
|
|
88
|
+
for key in OPENROUTER_API_KEYS:
|
|
89
|
+
try:
|
|
90
|
+
client = openai.OpenAI(base_url="https://openrouter.ai/api/v1", api_key=key)
|
|
91
|
+
response = client.chat.completions.create(
|
|
92
|
+
model=model_name,
|
|
93
|
+
messages=[
|
|
94
|
+
{"role": "system", "content": SYSTEM_PROMPT},
|
|
95
|
+
{"role": "user", "content": prompt}
|
|
96
|
+
],
|
|
97
|
+
temperature=0.7,
|
|
98
|
+
max_tokens=3000,
|
|
99
|
+
timeout=30.0,
|
|
100
|
+
)
|
|
101
|
+
result = response.choices[0].message.content
|
|
102
|
+
_clip(result)
|
|
103
|
+
print(result)
|
|
104
|
+
return result
|
|
105
|
+
except Exception as e:
|
|
106
|
+
last_error = str(e)
|
|
107
|
+
continue
|
|
108
|
+
|
|
109
|
+
err_msg = f"max() Սխալ: Բոլոր API բանալիները չաշխատեցին: Վերջին սխալը՝ {last_error}"
|
|
110
|
+
print(err_msg)
|
|
111
|
+
return err_msg
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def min(model_idx=None, prompt=None):
|
|
115
|
+
"""
|
|
116
|
+
Mistral AI models.
|
|
117
|
+
"""
|
|
118
|
+
if model_idx is None:
|
|
119
|
+
model_idx = 0 # Mistral only has 1 model in the list
|
|
120
|
+
if prompt is None:
|
|
121
|
+
prompt = input("Մուտքագրեք հարցումը: ")
|
|
122
|
+
|
|
123
|
+
key = _get_api_key("mistral")
|
|
124
|
+
if not key:
|
|
125
|
+
err = "Error: Mistral API Key is not set."
|
|
126
|
+
print(err)
|
|
127
|
+
return err
|
|
128
|
+
|
|
129
|
+
if not (0 <= model_idx < len(MISTRAL_MODELS)):
|
|
130
|
+
err = f"Error: model_idx must be between 0 and {len(MISTRAL_MODELS)-1}"
|
|
131
|
+
print(err)
|
|
132
|
+
return err
|
|
133
|
+
|
|
134
|
+
model_name = MISTRAL_MODELS[model_idx]
|
|
135
|
+
|
|
136
|
+
try:
|
|
137
|
+
body = json.dumps({
|
|
138
|
+
"model": model_name,
|
|
139
|
+
"messages": [
|
|
140
|
+
{"role": "system", "content": SYSTEM_PROMPT},
|
|
141
|
+
{"role": "user", "content": prompt}
|
|
142
|
+
],
|
|
143
|
+
"max_tokens": 3000
|
|
144
|
+
}).encode("utf-8")
|
|
145
|
+
req = urllib.request.Request(
|
|
146
|
+
"https://api.mistral.ai/v1/chat/completions",
|
|
147
|
+
data=body,
|
|
148
|
+
headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"},
|
|
149
|
+
)
|
|
150
|
+
with urllib.request.urlopen(req, timeout=15) as resp:
|
|
151
|
+
data = json.loads(resp.read().decode("utf-8"))
|
|
152
|
+
result = data["choices"][0]["message"]["content"]
|
|
153
|
+
_clip(result)
|
|
154
|
+
print(result)
|
|
155
|
+
return result
|
|
156
|
+
except Exception as e:
|
|
157
|
+
err_msg = f"min() Սխալ: {str(e)}"
|
|
158
|
+
print(err_msg)
|
|
159
|
+
return err_msg
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def mean(model_idx=None, prompt=None):
|
|
163
|
+
"""
|
|
164
|
+
Lightweight and fast AI models via OpenRouter.
|
|
165
|
+
"""
|
|
166
|
+
if model_idx is None:
|
|
167
|
+
model_idx = 0
|
|
168
|
+
|
|
169
|
+
if prompt is None:
|
|
170
|
+
prompt = input("Մուտքագրեք հարցումը: ")
|
|
171
|
+
|
|
172
|
+
if not (0 <= model_idx < len(MEAN_MODELS)):
|
|
173
|
+
err = f"Error: model_idx must be between 0 and {len(MEAN_MODELS)-1}"
|
|
174
|
+
print(err)
|
|
175
|
+
return err
|
|
176
|
+
|
|
177
|
+
model_name = MEAN_MODELS[model_idx]
|
|
178
|
+
last_error = None
|
|
179
|
+
|
|
180
|
+
for key in OPENROUTER_API_KEYS:
|
|
181
|
+
try:
|
|
182
|
+
client = openai.OpenAI(base_url="https://openrouter.ai/api/v1", api_key=key)
|
|
183
|
+
response = client.chat.completions.create(
|
|
184
|
+
model=model_name,
|
|
185
|
+
messages=[
|
|
186
|
+
{"role": "system", "content": SYSTEM_PROMPT},
|
|
187
|
+
{"role": "user", "content": prompt}
|
|
188
|
+
],
|
|
189
|
+
temperature=0.7,
|
|
190
|
+
max_tokens=3000,
|
|
191
|
+
timeout=15.0,
|
|
192
|
+
)
|
|
193
|
+
result = response.choices[0].message.content
|
|
194
|
+
_clip(result)
|
|
195
|
+
print(result)
|
|
196
|
+
return result
|
|
197
|
+
except Exception as e:
|
|
198
|
+
last_error = str(e)
|
|
199
|
+
continue
|
|
200
|
+
|
|
201
|
+
err_msg = f"mean() Սխալ: Բոլոր API բանալիները չաշխատեցին: Վերջին սխալը՝ {last_error}"
|
|
202
|
+
print(err_msg)
|
|
203
|
+
return err_msg
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def hello():
|
|
207
|
+
text = """
|
|
208
|
+
╔══════════════════════════════════════════════════════════════════════════════════╗
|
|
209
|
+
║ ՄԵՔԵՆԱՅԱԿԱՆ ՈՒՍՈՒՑՈՒՄ (ML) ԵՎ ԽՈՐԸ ՈՒՍՈՒՑՈՒՄ (DL) ║
|
|
210
|
+
║ ՄԵԳԱ-ՀԱՆՐԱԳԻՏԱՐԱՆԱՅԻՆ, ԳԵՐՄԱՆՐԱՄԱՍՆ ՈՒՂԵՑՈՒՅՑ ║
|
|
211
|
+
╚══════════════════════════════════════════════════════════════════════════════════╝
|
|
212
|
+
|
|
213
|
+
[ 1. ML ՊԱՐԱԴԻԳՄՆԵՐ ԵՎ ՄԱԹԵՄԱՏԻԿԱԿԱՆ ՀԻՄՔԵՐ ]
|
|
214
|
+
────────────────────────────────────────────────────────────────────────────────────
|
|
215
|
+
1.1. ՎԵՐԱՀՍԿՎՈՂ ՈՒՍՈՒՑՈՒՄ (Supervised)
|
|
216
|
+
• Գծային Ռեգրեսիա: L = (1/2n) * Σ(h(x_i) - y_i)^2
|
|
217
|
+
Lasso (L1): ավելանում է + λΣ|w_j|, Ridge (L2): + λΣw_j^2
|
|
218
|
+
• Լոգիստիկ Ռեգրեսիա: Կիրառում է Sigmoid: p = 1 / (1 + e^-(w^T x + b))
|
|
219
|
+
• SVM (Support Vector Machine): Մաքսիմիզացնում է մարժանը՝ 2/||w||, լուծելով
|
|
220
|
+
օպտիմիզացիայի խնդիր՝ y_i(w^T x_i + b) >= 1.
|
|
221
|
+
Kernel Trick: (Linear, RBF, Poly) թույլ է տալիս գծային բաժանում բարձր չափականության մեջ:
|
|
222
|
+
• Որոշումների Ծառեր (Decision Trees): Ճեղքումներ՝ հիմնված Gini անմաքրության
|
|
223
|
+
(Gini Impurity = 1 - Σ(p_i)^2) կամ Էնտրոպիայի (Entropy = -Σp_i*log(p_i)) վրա:
|
|
224
|
+
• Ensemble Մեթոդներ:
|
|
225
|
+
- Bagging (Random Forest): Շատ անկախ ծառեր, վերջնական որոշումը քվեարկությամբ:
|
|
226
|
+
- Boosting (XGBoost, LightGBM): Ծառերը սովորում են նախորդի սխալների վրա:
|
|
227
|
+
|
|
228
|
+
1.2. ՉՎԵՐԱՀՍԿՎՈՂ ՈՒՍՈՒՑՈՒՄ (Unsupervised)
|
|
229
|
+
• K-Means: Մինիմիզացնում է կետերի հեռավորությունը իրենց կենտրոններից (Centroids):
|
|
230
|
+
• Գլխավոր բաղադրիչների վերլուծություն (PCA): Օրթոգոնալ պրոյեկցիա, որը պահպանում
|
|
231
|
+
է առավելագույն վարիացիան (Eigenvalues/Eigenvectors-ի միջոցով):
|
|
232
|
+
• Գաուսյան խառնուրդներ (GMM): Օգտագործում է Expectation-Maximization (EM) ալգորիթմը:
|
|
233
|
+
• t-SNE / UMAP: Ոչ-գծային չափողականության նվազեցում (լավ է վիզուալիզացիայի համար):
|
|
234
|
+
|
|
235
|
+
1.3. ԽՐԱԽՈՒՍԱՆՔՈՎ ՈՒՍՈՒՑՈՒՄ (Reinforcement Learning)
|
|
236
|
+
• Մարկովյան Որոշումների Գործընթաց (MDP): Սահմանվում է որպես (S, A, P, R, γ):
|
|
237
|
+
• Բելլմանի հավասարում (Bellman Equation):
|
|
238
|
+
V(s) = max_a ( R(s,a) + γ * Σ P(s'|s,a)V(s') )
|
|
239
|
+
• Q-Learning: Q(s,a) = Q(s,a) + α[R + γ max Q(s',a') - Q(s,a)]
|
|
240
|
+
• Actor-Critic, PPO, TRPO: Սովորում են քաղաքականություն (Policy) և արժեք (Value):
|
|
241
|
+
|
|
242
|
+
[ 2. ԽՈՐԸ ՈՒՍՈՒՑՈՒՄ (DEEP LEARNING) ԵՎ ԱՐԽԻՏԵԿՏՈՒՐԱՆԵՐ ]
|
|
243
|
+
────────────────────────────────────────────────────────────────────────────────────
|
|
244
|
+
2.1. Բազմաշերտ Պերսեպտրոն (MLP) և Օպտիմիզացիա
|
|
245
|
+
• Backpropagation (Հետադարձ տարածում): Chain rule կիրառություն ածանցյալների համար:
|
|
246
|
+
∂L/∂w = (∂L/∂a) * (∂a/∂z) * (∂z/∂w)
|
|
247
|
+
• Batch Normalization: z_norm = (z - μ) / √(σ^2 + ε) * γ + β (Արագացնում է ուսուցումը):
|
|
248
|
+
|
|
249
|
+
2.2. Կոնվոլյուցիոն Նեյրոնային Ցանցեր (CNN)
|
|
250
|
+
• Convolution: Առանձնացնում է հատկանիշներ (եզրեր, ձևեր) ֆիլտրերի (Kernels) միջոցով:
|
|
251
|
+
• Pooling (Max/Average): Փոքրացնում է չափերը, խուսափում գերուսուցումից (translation invariance):
|
|
252
|
+
• Սերունդներ: LeNet (1998) -> AlexNet (2012) -> VGG -> ResNet (Residual blocks) -> EfficientNet.
|
|
253
|
+
• Computer Vision խնդիրներ:
|
|
254
|
+
- Օբյեկտի ճանաչում (Object Detection): YOLO, Faster R-CNN.
|
|
255
|
+
- Սեգմենտացիա (Segmentation): U-Net, Mask R-CNN.
|
|
256
|
+
|
|
257
|
+
2.3. Հաջորդական Մոդելներ և Տրանսֆորմերներ (NLP)
|
|
258
|
+
• RNN/LSTM: Պահպանում են հիշողություն (Hidden state). LSTM-ը ունի Forget, Input, Output դարպասներ:
|
|
259
|
+
• Word Embeddings: Word2Vec, GloVe (բառերը դառնում են n-չափ վեկտորներ հեռավորությամբ):
|
|
260
|
+
• Transformers: «Attention is All You Need» (2017).
|
|
261
|
+
- Self-Attention: Attention(Q, K, V) = softmax(Q K^T / √d_k) V
|
|
262
|
+
- BERT: Bidirectional Encoder (լավ է հասկանալու և դասակարգելու համար):
|
|
263
|
+
- GPT: Generative Pre-trained Transformer (Աջից ձախ դեկոդեր, գեներացիա):
|
|
264
|
+
- Վերջին մոդելներ (LLMs): LLaMA, Claude, Gemini, DeepSeek (պարամետրերը հասնում են տրիլիոնների):
|
|
265
|
+
|
|
266
|
+
[ 3. MLOPS, XAI ԵՎ ԿԱՐԵՎՈՐ ԽՆԴԻՐՆԵՐԻ ԼՈՒԾՈՒՄՆԵՐ ]
|
|
267
|
+
────────────────────────────────────────────────────────────────────────────────────
|
|
268
|
+
3.1. Բացատրելի AI (Explainable AI - XAI)
|
|
269
|
+
• SHAP (SHapley Additive exPlanations): Խաղերի տեսություն վրա հիմնված՝ ցույց է
|
|
270
|
+
տալիս յուրաքանչյուր հատկանիշի (feature) ներդրումը արդյունքում:
|
|
271
|
+
• LIME: Լոկալ մակարդակում մոտարկում է բարդ մոդելը պարզ գծային մոդելով:
|
|
272
|
+
|
|
273
|
+
3.2. MLOps (Machine Learning Operations)
|
|
274
|
+
• Data Drift / Concept Drift: Երբ իրական աշխարհի տվյալները փոխվում են ժամանակի ընթացքում
|
|
275
|
+
և մոդելը սկսում է վատ աշխատել:
|
|
276
|
+
• CI/CD/CT: Շարունակական ինտեգրացիա, տեղակայում և **Ուսուցում** (Continuous Training):
|
|
277
|
+
|
|
278
|
+
3.3. Գերուսուցման Բարձրագույն Լուծումներ
|
|
279
|
+
• Խաչաձև վավերացում (Cross-Validation): K-Fold CV (Բաժանում k մասերի, որից մեկը ստուգման համար):
|
|
280
|
+
• Դրոպաութ (Dropout): DL-ի ժամանակ նեյրոնների որոշ տոկոսի պատահական անջատում (ստիպում է
|
|
281
|
+
խուսափել մեկ նեյրոնից կախվածությունից):
|
|
282
|
+
• Data Augmentation (Տվյալների հարստացում): Նկարների պտտում, աղմուկի ավելացում տեքստին:
|
|
283
|
+
|
|
284
|
+
3.4. Անհավասարակշռված Տվյալներ (Imbalanced Data)
|
|
285
|
+
• SMOTE (Synthetic Minority Over-sampling Technique): Արհեստականորեն ստեղծում է փոքր դասի նոր կետեր:
|
|
286
|
+
• Class Weights: Սխալի ֆունկցիային տրվում է տարբեր կշիռներ տարբեր դասերի համար:
|
|
287
|
+
• Խստորեն չօգտագործել Accuracy մետրիկան, այլ օգտագործել F1-Score, ROC-AUC, Precision-Recall Curve:
|
|
288
|
+
"""
|
|
289
|
+
print(text)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def bye():
|
|
293
|
+
text = """
|
|
294
|
+
╔══════════════════════════════════════════════════════════════════════════════════╗
|
|
295
|
+
║ PYTHON ԾՐԱԳՐԱՎՈՐՈՒՄ ║
|
|
296
|
+
║ ՄԵԳԱ-ՀԱՆՐԱԳԻՏԱՐԱՆԱՅԻՆ, ԳԵՐՄԱՆՐԱՄԱՍՆ ՈՒՂԵՑՈՒՅՑ ║
|
|
297
|
+
╚══════════════════════════════════════════════════════════════════════════════════╝
|
|
298
|
+
|
|
299
|
+
[ 1. PYTHON-Ի ՆԵՐՔԻՆ ԱՇԽԱՏԱՆՔԸ (INTERNALS) ]
|
|
300
|
+
────────────────────────────────────────────────────────────────────────────────────
|
|
301
|
+
1.1. CPython և Բայթկոդ (Bytecode)
|
|
302
|
+
• Python-ի լռելյայն ինտերպրետատորը գրված է C-ով (CPython):
|
|
303
|
+
• Կոդը թարգմանվում է CPython բայթկոդի (.pyc ֆայլեր __pycache__ պանակում):
|
|
304
|
+
• Կարող եք ուսումնասիրել բայթկոդը `dis` (disassembler) մոդուլով (օր.՝ `import dis; dis.dis(my_func)`):
|
|
305
|
+
1.2. Հիշողության Կառավարում (Memory Management)
|
|
306
|
+
• Բոլոր օբյեկտները հիշողության մեջ ունեն PyObject C-կառուցվածք՝ տիպի ցուցիչ և Reference Count:
|
|
307
|
+
• Հղումների Հաշվարկ (Reference Counting): Երբ օբյեկտի հղումները դառնում են 0, այն ոչնչանում է:
|
|
308
|
+
• Ցիկլիկ աղբահանող (Cyclic Garbage Collector): Լուծում է խնդիրը, երբ A-ն հղվում է B-ին, իսկ B-ն A-ին
|
|
309
|
+
(import gc; gc.collect()՝ ձեռքով մաքրելու համար):
|
|
310
|
+
1.3. Global Interpreter Lock (GIL)
|
|
311
|
+
• Mutex CPython-ի ներսում, որը թույլ չի տալիս բազմաթիվ thread-ների միաժամանակ կատարել օբյեկտների փոփոխում:
|
|
312
|
+
• Արդյունքը՝ CPU-bound խնդիրներում multithreading-ը անիմաստ է: Փոխարենը օգտագործվում է `multiprocessing`:
|
|
313
|
+
|
|
314
|
+
[ 2. ԲԱՐՁՐԱԳՈՒՅՆ OOP ԵՎ ԴԻԶԱՅՆ ՊԱՏԵՐՆՆԵՐ ]
|
|
315
|
+
────────────────────────────────────────────────────────────────────────────────────
|
|
316
|
+
2.1. Դեկրիպտորներ (Descriptors) և հատկություններ
|
|
317
|
+
• Եթե դասն ունի `__get__`, `__set__`, `__delete__` մեթոդներ, այն Descriptor է:
|
|
318
|
+
• `@property` դեկորատորը հենց Descriptor-ի ներկառուցված օրինակ է:
|
|
319
|
+
2.2. Մետադասեր (Metaclasses)
|
|
320
|
+
• Դասերը նույնպես օբյեկտներ են, որոնք ստեղծվում են Մետադասերի կողմից (default՝ `type`):
|
|
321
|
+
• `class MyMeta(type):` — թույլ է տալիս միջամտել դասի ստեղծման գործընթացին նախքան
|
|
322
|
+
դրա գոյություն ունենալը (հզոր գործիք ORM-ների համար՝ Django, SQLAlchemy):
|
|
323
|
+
2.3. __slots__ օպտիմիզացիա
|
|
324
|
+
• Լռելյայն ամեն օբյեկտ իր ատրիբուտները պահում է բառարանում (`__dict__`):
|
|
325
|
+
• `__slots__ = ('name', 'age')` օգտագործելով խնայում ենք RAM, արգելելով `__dict__`-ի ստեղծումը:
|
|
326
|
+
2.4. Data Classes և Pydantic
|
|
327
|
+
• `@dataclass`: Թույլ է տալիս խուսափել երկար `__init__`, `__repr__`, `__eq__` գրելուց:
|
|
328
|
+
• `Pydantic`: Ստուգում է տվյալների տիպերը run-time-ում (օգտագործվում է FastAPI-ում):
|
|
329
|
+
|
|
330
|
+
[ 3. ՖՈՒՆԿՑԻՈՆԱԼ ԵՎ ԱՍԻՆԽՐՈՆ ԾՐԱԳՐԱՎՈՐՈՒՄ (ADVANCED) ]
|
|
331
|
+
────────────────────────────────────────────────────────────────────────────────────
|
|
332
|
+
3.1. Առաջին Կարգի Օբյեկտներ (First-Class Citizens)
|
|
333
|
+
• Ֆունկցիաները կարելի է փոխանցել որպես արգումենտ, վերադարձնել ֆունկցիայից կամ պահել ցուցակում:
|
|
334
|
+
• Փակումներ (Closures): Ներքին ֆունկցիան պահպանում է արտաքին ֆունկցիայի փոփոխականները
|
|
335
|
+
նույնիսկ վերջինիս ավարտից հետո (կարևոր է Decorators-ի համար):
|
|
336
|
+
3.2. Ասինխրոն Ծրագրավորում (Asyncio)
|
|
337
|
+
• Event Loop: Շարժիչ, որը կատարում է Task-երը և զբաղվում I/O սպասումներով (Network, Disk):
|
|
338
|
+
• Coroutines: `async def` ֆունկցիաները, որոնք վերադարձնում են կորուտիններ և չեն աշխատում
|
|
339
|
+
մինչև չկանչվեն `await`-ով կամ չդառնան `Task` (`asyncio.create_task()`):
|
|
340
|
+
• Concurrency (համաժամանակություն), բայց ոչ Parallelism (զուգահեռություն):
|
|
341
|
+
|
|
342
|
+
[ 4. ՏԻՊԱՎՈՐՈՒՄ ԵՎ ԺԱՄԱՆԱԿԱԿԻՑ ՆՈՐԱՄՈՒԾՈՒԹՅՈՒՆՆԵՐ ]
|
|
343
|
+
────────────────────────────────────────────────────────────────────────────────────
|
|
344
|
+
4.1. Type Hinting (Տիպերի հուշումներ, PEP 484)
|
|
345
|
+
• `def func(x: int) -> list[str]:` (Python 3.9+-ում `typing` մոդուլից `List`-ը պետք չէ):
|
|
346
|
+
• Covariance և Contravariance: `TypeVar('T', covariant=True)` (շատ խորացված տիպավորում):
|
|
347
|
+
• Mypy / Pyright: Ստատիկ վերլուծիչներ, որոնք ստուգում են տիպերը նախքան աշխատեցնելը (օգնում են
|
|
348
|
+
խուսափել բազում runtime bug-երից):
|
|
349
|
+
4.2. Կառուցվածքային Օրինաչափության Համապատասխանեցում (Match-Case) (Python 3.10+)
|
|
350
|
+
• Նման է `switch-case`-ին, բայց շատ ավելի հզոր:
|
|
351
|
+
match data:
|
|
352
|
+
case {"type": "user", "id": int(uid), "tags": [*tags]}:
|
|
353
|
+
print(f"User {uid} with tags {tags}")
|
|
354
|
+
case _:
|
|
355
|
+
print("Անհայտ ֆորմատ")
|
|
356
|
+
4.3. Ուոլրուս Օպերատոր (Walrus Operator :=) (Python 3.8+)
|
|
357
|
+
• Վերագրում և վերադարձնում է միաժամանակ.
|
|
358
|
+
`if (n := len(a)) > 10: print(f"Շատ երկար է ({n} տարր)")`
|
|
359
|
+
|
|
360
|
+
[ 5. ՍՏԱՆԴԱՐՏ ԵՎ ԼՐԱՑՈՒՑԻՉ ՀԶՈՐ ՄՈԴՈՒԼՆԵՐ ]
|
|
361
|
+
────────────────────────────────────────────────────────────────────────────────────
|
|
362
|
+
• `contextlib`: `contextmanager` դեկորատոր՝ արագ Context Manager սարքելու համար:
|
|
363
|
+
• `functools`: `partial` (ֆունկցիայի արգումենտների նախապես լրացում), `reduce`, `singledispatch`:
|
|
364
|
+
• `multiprocessing.shared_memory`: RAM-ի ուղիղ կիսում պրոցեսների միջև՝ առանց Serialization-ի:
|
|
365
|
+
• `ast` (Abstract Syntax Tree): Թույլ է տալիս կարդալ և վերլուծել Python կոդը որպես ծառ (կիրառվում
|
|
366
|
+
է linter-ների և code formatter-ների կողմից, ինչպիսիք են Pylint կամ Black):
|
|
367
|
+
• `ctypes` և `cffi`: Թույլ են տալիս անմիջապես կանչել C կամ C++ գրադարանների ֆունկցիաներ
|
|
368
|
+
(.dll կամ .so ֆայլերից), ինչը չափազանց կարևոր է Numpy/TensorFlow-ի նման գրադարանների համար:
|
|
369
|
+
|
|
370
|
+
[ 6. ԾՐԱԳՐԱՎՈՐՄԱՆ ԼԱՎԱԳՈՒՅՆ ՓՈՐՁ (BEST PRACTICES) ]
|
|
371
|
+
────────────────────────────────────────────────────────────────────────────────────
|
|
372
|
+
• PEP 8: Դասերը PascalCase, ֆունկցիաներն ու փոփոխականները snake_case, հաստատունները SCREAMING_SNAKE_CASE:
|
|
373
|
+
• Կանխարգելելով Խնդիրները: Օգտագործեք `isinstance()` փոխարեն `type(a) == type(b)`:
|
|
374
|
+
• "EAFP vs LBYL": Python-ում խրախուսվում է EAFP ("Easier to ask for forgiveness than permission"):
|
|
375
|
+
Այսինքն՝ ուղղակի արեք գործողությունը տրամաբանորեն, և Catch արեք Exception-ը (try/except),
|
|
376
|
+
քան LBYL (օգտագործելով երկար `if hasattr(...)` կամ `if type(...)`):
|
|
377
|
+
"""
|
|
378
|
+
print(text)
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def help():
|
|
382
|
+
text = """
|
|
383
|
+
╔══════════════════════════════════════════════════════════════╗
|
|
384
|
+
║ nampy ║
|
|
385
|
+
╚══════════════════════════════════════════════════════════════╝
|
|
386
|
+
|
|
387
|
+
Կարող եք կանչել ֆունկցիաներն առանց արգումենտների՝ nampy.max()
|
|
388
|
+
Այդ դեպքում ծրագիրը կհարցնի ձեզնից մոդելի համարը և հարցումը:
|
|
389
|
+
Կամ կարող եք կանչել միանգամից՝ nampy.max(0, "հարց"):
|
|
390
|
+
|
|
391
|
+
import nampy
|
|
392
|
+
|
|
393
|
+
┌─ nampy.max() ────────────────────────────────────────────────┐
|
|
394
|
+
│ Ամենաուժեղ AI մոդելները (բարդ հարցեր, ծրագրավորում) │
|
|
395
|
+
│ [0] deepseek/deepseek-r1-0528 │
|
|
396
|
+
│ [1] anthropic/claude-sonnet-4-5 │
|
|
397
|
+
│ [2] openai/gpt-4o │
|
|
398
|
+
│ [3] google/gemini-2.5-flash-preview-05-20 │
|
|
399
|
+
│ [4] meta-llama/llama-3.3-70b-instruct │
|
|
400
|
+
│ [5] nvidia/nemotron-3-ultra-550b-a55b:free │
|
|
401
|
+
│ [6] openrouter/owl-alpha │
|
|
402
|
+
│ [7] moonshotai/kimi-k2.6:free │
|
|
403
|
+
│ [8] qwen/qwen3-coder:free │
|
|
404
|
+
│ [9] nousresearch/hermes-3-llama-3.1-405b:free │
|
|
405
|
+
└──────────────────────────────────────────────────────────────┘
|
|
406
|
+
|
|
407
|
+
┌─ nampy.min() ────────────────────────────────────────────────┐
|
|
408
|
+
│ Mistral AI մոդելներ (արագ, հուսալի) │
|
|
409
|
+
│ [0] mistral-large-latest │
|
|
410
|
+
└──────────────────────────────────────────────────────────────┘
|
|
411
|
+
|
|
412
|
+
┌─ nampy.mean() ───────────────────────────────────────────────┐
|
|
413
|
+
│ Թեթև և արագ մոդելներ (պարզ հարցեր) │
|
|
414
|
+
│ [0] google/gemini-2.5-flash-preview-05-20 │
|
|
415
|
+
│ [1] google/gemma-3-27b-it │
|
|
416
|
+
│ [2] meta-llama/llama-3.1-8b-instruct │
|
|
417
|
+
│ [3] mistralai/mistral-7b-instruct │
|
|
418
|
+
└──────────────────────────────────────────────────────────────┘
|
|
419
|
+
|
|
420
|
+
┌─ Օգնական ֆունկցիաներ ────────────────────────────────────────┐
|
|
421
|
+
│ nampy.hello() - Տպում է Machine Learning հասկացություններ: │
|
|
422
|
+
│ nampy.bye() - Տպում է Python լեզվի առանձնահատկություններ: │
|
|
423
|
+
└──────────────────────────────────────────────────────────────┘
|
|
424
|
+
"""
|
|
425
|
+
print(text)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: numpy_fast
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Python AI toolkit — max, min, mean functions powered by top AI models
|
|
5
5
|
Author-email: Karen_Poghosyan_042009 <karenpoghosyan242@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/karen/numpy_fast
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import warnings
|
|
3
|
-
import requests
|
|
4
|
-
import openai
|
|
5
|
-
|
|
6
|
-
warnings.filterwarnings("ignore")
|
|
7
|
-
os.environ["PYTHONWARNINGS"] = "ignore"
|
|
8
|
-
|
|
9
|
-
# --- API Keys ---
|
|
10
|
-
DEFAULT_MISTRAL_KEY = os.getenv("MISTRAL_API_KEY", "83m7bHL52JrfeG8mD910gy2PHxQ9wBRa")
|
|
11
|
-
DEFAULT_OPENROUTER_KEY = os.getenv("OPENROUTER_API_KEY", "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")
|
|
12
|
-
|
|
13
|
-
# max() — ուժեղ մոդելներ (DeepSeek R1, Claude, GPT-4o...)
|
|
14
|
-
MAX_MODELS = [
|
|
15
|
-
"deepseek/deepseek-r1-0528",
|
|
16
|
-
"anthropic/claude-sonnet-4-5",
|
|
17
|
-
"openai/gpt-4o",
|
|
18
|
-
"google/gemini-2.5-flash-preview-05-20",
|
|
19
|
-
"meta-llama/llama-3.3-70b-instruct",
|
|
20
|
-
]
|
|
21
|
-
|
|
22
|
-
# mean() — թեթև, արագ մոդելներ
|
|
23
|
-
MEAN_MODELS = [
|
|
24
|
-
"google/gemini-2.5-flash-preview-05-20",
|
|
25
|
-
"google/gemma-3-27b-it",
|
|
26
|
-
"meta-llama/llama-3.1-8b-instruct",
|
|
27
|
-
"mistralai/mistral-7b-instruct",
|
|
28
|
-
]
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def _get_api_key(provider, provided_key=None):
|
|
32
|
-
if provided_key:
|
|
33
|
-
return provided_key
|
|
34
|
-
if provider == "mistral":
|
|
35
|
-
return DEFAULT_MISTRAL_KEY
|
|
36
|
-
if provider == "openrouter":
|
|
37
|
-
return DEFAULT_OPENROUTER_KEY
|
|
38
|
-
return None
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
def max(prompt, api_key=None):
|
|
42
|
-
"""
|
|
43
|
-
Ուժեղ AI մոդելներ OpenRouter-ի միջոցով (DeepSeek R1, Claude, GPT-4o...).
|
|
44
|
-
Լավագույն հարցերի, վերլուծության և բարդ առաջադրանքների համար:
|
|
45
|
-
"""
|
|
46
|
-
key = _get_api_key("openrouter", api_key)
|
|
47
|
-
if not key:
|
|
48
|
-
return "Սխալ: OpenRouter API Key-ը սահմանված չէ:"
|
|
49
|
-
|
|
50
|
-
client = openai.OpenAI(base_url="https://openrouter.ai/api/v1", api_key=key)
|
|
51
|
-
|
|
52
|
-
last_error = None
|
|
53
|
-
for model_name in MAX_MODELS:
|
|
54
|
-
try:
|
|
55
|
-
response = client.chat.completions.create(
|
|
56
|
-
model=model_name,
|
|
57
|
-
messages=[{"role": "user", "content": prompt}],
|
|
58
|
-
temperature=0.7,
|
|
59
|
-
timeout=30.0,
|
|
60
|
-
)
|
|
61
|
-
return response.choices[0].message.content
|
|
62
|
-
except Exception as e:
|
|
63
|
-
last_error = e
|
|
64
|
-
continue
|
|
65
|
-
|
|
66
|
-
return f"max() Սխալ: {str(last_error)}"
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def min(prompt, api_key=None):
|
|
70
|
-
"""
|
|
71
|
-
Mistral AI — արագ և հուսալի պատասխաններ mistral-large-latest մոդելով:
|
|
72
|
-
"""
|
|
73
|
-
key = _get_api_key("mistral", api_key)
|
|
74
|
-
if not key:
|
|
75
|
-
return "Սխալ: Mistral API Key-ը սահմանված չէ:"
|
|
76
|
-
|
|
77
|
-
try:
|
|
78
|
-
response = requests.post(
|
|
79
|
-
"https://api.mistral.ai/v1/chat/completions",
|
|
80
|
-
headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"},
|
|
81
|
-
json={"model": "mistral-large-latest", "messages": [{"role": "user", "content": prompt}]},
|
|
82
|
-
timeout=15,
|
|
83
|
-
)
|
|
84
|
-
if response.status_code == 200:
|
|
85
|
-
return response.json()["choices"][0]["message"]["content"]
|
|
86
|
-
return f"Mistral Սխալ {response.status_code}: {response.text}"
|
|
87
|
-
except Exception as e:
|
|
88
|
-
return f"min() Սխալ: {str(e)}"
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def mean(prompt, api_key=None):
|
|
92
|
-
"""
|
|
93
|
-
Թեթև և արագ AI մոդելներ OpenRouter-ի միջոցով (Gemini Flash, Gemma, LLaMA...).
|
|
94
|
-
Պարզ հարցերի և արագ պատասխանների համար:
|
|
95
|
-
"""
|
|
96
|
-
key = _get_api_key("openrouter", api_key)
|
|
97
|
-
if not key:
|
|
98
|
-
return "Սխալ: OpenRouter API Key-ը սահմանված չէ:"
|
|
99
|
-
|
|
100
|
-
client = openai.OpenAI(base_url="https://openrouter.ai/api/v1", api_key=key)
|
|
101
|
-
|
|
102
|
-
last_error = None
|
|
103
|
-
for model_name in MEAN_MODELS:
|
|
104
|
-
try:
|
|
105
|
-
response = client.chat.completions.create(
|
|
106
|
-
model=model_name,
|
|
107
|
-
messages=[{"role": "user", "content": prompt}],
|
|
108
|
-
temperature=0.7,
|
|
109
|
-
timeout=15.0,
|
|
110
|
-
)
|
|
111
|
-
return response.choices[0].message.content
|
|
112
|
-
except Exception as e:
|
|
113
|
-
last_error = e
|
|
114
|
-
continue
|
|
115
|
-
|
|
116
|
-
return f"mean() Սխալ: {str(last_error)}"
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
def hello():
|
|
120
|
-
return ""
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
def help():
|
|
124
|
-
text = """
|
|
125
|
-
╔══════════════════════════════════════════════════════════════╗
|
|
126
|
-
║ nampy — AI Toolkit ║
|
|
127
|
-
╚══════════════════════════════════════════════════════════════╝
|
|
128
|
-
|
|
129
|
-
import nampy
|
|
130
|
-
|
|
131
|
-
┌─ nampy.max(prompt) ────────────────────────────────────────┐
|
|
132
|
-
│ Ամենաուժեղ AI մոդելները (DeepSeek R1, Claude, GPT-4o) │
|
|
133
|
-
│ Բարդ հարցեր, վերլուծություն, ծրագրավորում │
|
|
134
|
-
│ nampy.max("Բացատրի ռեկուրսիան Python-ում") │
|
|
135
|
-
└────────────────────────────────────────────────────────────┘
|
|
136
|
-
|
|
137
|
-
┌─ nampy.min(prompt) ────────────────────────────────────────┐
|
|
138
|
-
│ Mistral Large — արագ, հուսալի │
|
|
139
|
-
│ Ստանդարտ հարցեր, թարգմանություն, ամփոփում │
|
|
140
|
-
│ nampy.min("Ո՞րն է Python-ի առավելությունը") │
|
|
141
|
-
└────────────────────────────────────────────────────────────┘
|
|
142
|
-
|
|
143
|
-
┌─ nampy.mean(prompt) ───────────────────────────────────────┐
|
|
144
|
-
│ Թեթև մոդելներ (Gemini Flash, Gemma, LLaMA) │
|
|
145
|
-
│ Պարզ հարցեր, արագ պատասխաններ │
|
|
146
|
-
│ nampy.mean("Ինչ է HTTP?") │
|
|
147
|
-
└────────────────────────────────────────────────────────────┘
|
|
148
|
-
|
|
149
|
-
API Keys (environment variables):
|
|
150
|
-
MISTRAL_API_KEY — Mistral AI key (min ֆունկցիա)
|
|
151
|
-
OPENROUTER_API_KEY — OpenRouter key (max, mean ֆունկցիաներ)
|
|
152
|
-
"""
|
|
153
|
-
print(text)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|