Upload install_functions.py
Browse files- install_functions.py +51 -25
install_functions.py
CHANGED
|
@@ -3,6 +3,7 @@ import urllib.request
|
|
| 3 |
import urllib.error
|
| 4 |
import sys
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
base_url = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:8080'
|
| 8 |
token = sys.argv[2] if len(sys.argv) > 2 else ''
|
|
@@ -11,7 +12,12 @@ if not token:
|
|
| 11 |
print("β No token provided.")
|
| 12 |
sys.exit(1)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def post(endpoint, data_dict):
|
|
|
|
| 15 |
data = json.dumps(data_dict).encode()
|
| 16 |
req = urllib.request.Request(
|
| 17 |
base_url + endpoint,
|
|
@@ -23,65 +29,85 @@ def post(endpoint, data_dict):
|
|
| 23 |
method='POST'
|
| 24 |
)
|
| 25 |
try:
|
| 26 |
-
with urllib.request.urlopen(req) as r:
|
| 27 |
return r.status, r.read().decode()
|
| 28 |
except urllib.error.HTTPError as e:
|
| 29 |
return e.code, e.read().decode()
|
| 30 |
except Exception as e:
|
|
|
|
| 31 |
return 0, str(e)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# ββ FUNCTIONS (Filter/Pipe klase) ββββββββββββββββββββββββββ
|
| 34 |
functions = [
|
| 35 |
{
|
| 36 |
"id": "context_detector",
|
| 37 |
"name": "Context Detector",
|
| 38 |
"type": "filter",
|
| 39 |
-
"content":
|
| 40 |
"meta": {"description": "Automatski prepoznaje kontekst svake poruke."}
|
| 41 |
},
|
| 42 |
{
|
| 43 |
"id": "token_saver",
|
| 44 |
"name": "Token Saver",
|
| 45 |
"type": "filter",
|
| 46 |
-
"content":
|
| 47 |
"meta": {"description": "Ε tedi tokene ograniΔavanjem historije razgovora."}
|
| 48 |
}
|
| 49 |
]
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
print(f"Instaliram {len(functions)} funkcija (filtera)...")
|
| 52 |
for func in functions:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
print(f"β {func['name']} β
|
| 63 |
|
| 64 |
# ββ TOOLS (Tools klasa) ββββββββββββββββββββββββββββββββββββ
|
| 65 |
tools = [
|
| 66 |
{
|
| 67 |
"id": "smart_matcher",
|
| 68 |
"name": "Smart Matcher",
|
| 69 |
-
"content":
|
| 70 |
"meta": {"description": "Sparuje stavke iz dvije liste s confidence scorom."}
|
| 71 |
}
|
| 72 |
]
|
| 73 |
|
|
|
|
|
|
|
|
|
|
| 74 |
print(f"Instaliram {len(tools)} tool(a)...")
|
| 75 |
for tool in tools:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
print(f"β {tool['name']} β
|
| 86 |
|
| 87 |
-
print("Gotovo!")
|
|
|
|
| 3 |
import urllib.error
|
| 4 |
import sys
|
| 5 |
import os
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
base_url = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:8080'
|
| 9 |
token = sys.argv[2] if len(sys.argv) > 2 else ''
|
|
|
|
| 12 |
print("β No token provided.")
|
| 13 |
sys.exit(1)
|
| 14 |
|
| 15 |
+
# Δekaj malo prije nego Ε‘to poΔneΕ‘ - WebUI trebat vremena
|
| 16 |
+
print("Δekam da se WebUI stabilizira...")
|
| 17 |
+
time.sleep(5)
|
| 18 |
+
|
| 19 |
def post(endpoint, data_dict):
|
| 20 |
+
"""Sigurna POST funkcija s error handlingom"""
|
| 21 |
data = json.dumps(data_dict).encode()
|
| 22 |
req = urllib.request.Request(
|
| 23 |
base_url + endpoint,
|
|
|
|
| 29 |
method='POST'
|
| 30 |
)
|
| 31 |
try:
|
| 32 |
+
with urllib.request.urlopen(req, timeout=10) as r:
|
| 33 |
return r.status, r.read().decode()
|
| 34 |
except urllib.error.HTTPError as e:
|
| 35 |
return e.code, e.read().decode()
|
| 36 |
except Exception as e:
|
| 37 |
+
print(f"β οΈ POST error on {endpoint}: {str(e)}")
|
| 38 |
return 0, str(e)
|
| 39 |
|
| 40 |
+
def read_file_safe(filepath):
|
| 41 |
+
"""Siguran ΔitaΔ datoteka"""
|
| 42 |
+
try:
|
| 43 |
+
if os.path.exists(filepath) and os.path.getsize(filepath) > 0:
|
| 44 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 45 |
+
return f.read()
|
| 46 |
+
else:
|
| 47 |
+
print(f"β οΈ {filepath} je prazna ili ne postoji")
|
| 48 |
+
return None
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"β Error reading {filepath}: {e}")
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
# ββ FUNCTIONS (Filter/Pipe klase) ββββββββββββββββββββββββββ
|
| 54 |
functions = [
|
| 55 |
{
|
| 56 |
"id": "context_detector",
|
| 57 |
"name": "Context Detector",
|
| 58 |
"type": "filter",
|
| 59 |
+
"content": read_file_safe("/app/context_detector.py"),
|
| 60 |
"meta": {"description": "Automatski prepoznaje kontekst svake poruke."}
|
| 61 |
},
|
| 62 |
{
|
| 63 |
"id": "token_saver",
|
| 64 |
"name": "Token Saver",
|
| 65 |
"type": "filter",
|
| 66 |
+
"content": read_file_safe("/app/token_compressor.py"),
|
| 67 |
"meta": {"description": "Ε tedi tokene ograniΔavanjem historije razgovora."}
|
| 68 |
}
|
| 69 |
]
|
| 70 |
|
| 71 |
+
# Filtriraj funkcije koje nisu uΔitane
|
| 72 |
+
functions = [f for f in functions if f["content"]]
|
| 73 |
+
|
| 74 |
print(f"Instaliram {len(functions)} funkcija (filtera)...")
|
| 75 |
for func in functions:
|
| 76 |
+
try:
|
| 77 |
+
code, body = post('/api/v1/functions/create', func)
|
| 78 |
+
if code == 200:
|
| 79 |
+
print(f"β
{func['name']} β kreirana")
|
| 80 |
+
elif code == 409 or 'already exists' in body.lower():
|
| 81 |
+
print(f"β οΈ {func['name']} β veΔ postoji (preskaΔem)")
|
| 82 |
+
else:
|
| 83 |
+
print(f"β {func['name']} β greΕ‘ka {code}")
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"β {func['name']} β exception: {e}")
|
| 86 |
|
| 87 |
# ββ TOOLS (Tools klasa) ββββββββββββββββββββββββββββββββββββ
|
| 88 |
tools = [
|
| 89 |
{
|
| 90 |
"id": "smart_matcher",
|
| 91 |
"name": "Smart Matcher",
|
| 92 |
+
"content": read_file_safe("/app/smart_matcher.py"),
|
| 93 |
"meta": {"description": "Sparuje stavke iz dvije liste s confidence scorom."}
|
| 94 |
}
|
| 95 |
]
|
| 96 |
|
| 97 |
+
# Filtriraj toolove koje nisu uΔitane
|
| 98 |
+
tools = [t for t in tools if t["content"]]
|
| 99 |
+
|
| 100 |
print(f"Instaliram {len(tools)} tool(a)...")
|
| 101 |
for tool in tools:
|
| 102 |
+
try:
|
| 103 |
+
code, body = post('/api/v1/tools/create', tool)
|
| 104 |
+
if code == 200:
|
| 105 |
+
print(f"β
{tool['name']} β kreiran")
|
| 106 |
+
elif code == 409 or 'already exists' in body.lower():
|
| 107 |
+
print(f"β οΈ {tool['name']} β veΔ postoji (preskaΔem)")
|
| 108 |
+
else:
|
| 109 |
+
print(f"β {tool['name']} β greΕ‘ka {code}")
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"β {tool['name']} β exception: {e}")
|
| 112 |
|
| 113 |
+
print("β
Gotovo!")
|