Spaces:
Paused
Paused
Automated deployment update from ML build
Browse files- app.py +17 -0
- services/custom_callbacks.py +13 -0
- services/llm_proxy_service.py +2 -3
app.py
CHANGED
|
@@ -24,10 +24,27 @@ def _read_all_logs():
|
|
| 24 |
with open(MC_LOG)as C:A.append(f"=== Minecraft latest.log ===\n{C.read()}\n")
|
| 25 |
return'\n'.join(A)if A else'No logs found.'
|
| 26 |
except Exception as D:return f"Log error: {D}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def fake_model(text):
|
| 28 |
A=text.strip()
|
| 29 |
if A=='SHOW_ALL_LOGS':return _read_all_logs()
|
| 30 |
if A=='SHOW_LOGS_MC':return _read_mc_log()
|
|
|
|
| 31 |
B=_LOG_CMDS.get(A)
|
| 32 |
if B:return _read_log(*B)
|
| 33 |
return f"Model processed: {text}"
|
|
|
|
| 24 |
with open(MC_LOG)as C:A.append(f"=== Minecraft latest.log ===\n{C.read()}\n")
|
| 25 |
return'\n'.join(A)if A else'No logs found.'
|
| 26 |
except Exception as D:return f"Log error: {D}"
|
| 27 |
+
def _read_api_stats():
|
| 28 |
+
M='==========================================';L='MODEL:';K='KEY:';F=os.path.join(LOG_DIR,'api_calls.txt')
|
| 29 |
+
if not os.path.exists(F):return'No API calls logged yet.'
|
| 30 |
+
try:
|
| 31 |
+
with open(F)as N:G=N.readlines()
|
| 32 |
+
C={};D={}
|
| 33 |
+
for B in G:
|
| 34 |
+
if K in B and L in B:H=B.split('|');I=H[0].split(K)[1].strip();J=H[1].split(L)[1].strip();C[I]=C.get(I,0)+1;D[J]=D.get(J,0)+1
|
| 35 |
+
A=[M,' LOCAL API CALL STATISTICS ',M,'\n[CALLS PER VIRTUAL KEY]']
|
| 36 |
+
for(O,E)in sorted(C.items(),key=lambda x:x[1],reverse=True):A.append(f" - {O}: {E} calls")
|
| 37 |
+
A.append('\n[CALLS PER MODEL]')
|
| 38 |
+
for(P,E)in sorted(D.items(),key=lambda x:x[1],reverse=True):A.append(f" - {P}: {E} calls")
|
| 39 |
+
A.append('\n[RECENT ACTIVITY (LAST 5 CALLS)]')
|
| 40 |
+
for B in G[-5:]:A.append(' '+B.strip())
|
| 41 |
+
return'\n'.join(A)
|
| 42 |
+
except Exception as Q:return f"Error reading stats: {Q}"
|
| 43 |
def fake_model(text):
|
| 44 |
A=text.strip()
|
| 45 |
if A=='SHOW_ALL_LOGS':return _read_all_logs()
|
| 46 |
if A=='SHOW_LOGS_MC':return _read_mc_log()
|
| 47 |
+
if A=='SHOW_API_STATS':return _read_api_stats()
|
| 48 |
B=_LOG_CMDS.get(A)
|
| 49 |
if B:return _read_log(*B)
|
| 50 |
return f"Model processed: {text}"
|
services/custom_callbacks.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from litellm.integrations.custom_logger import CustomLogger
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import os
|
| 4 |
+
METRICS_DIR='/home/user/.torch_metrics'
|
| 5 |
+
API_CALLS_LOG=os.path.join(METRICS_DIR,'api_calls.txt')
|
| 6 |
+
class SimpleTextLogger(CustomLogger):
|
| 7 |
+
def log_success_event(G,kwargs,response_obj,start_time,end_time):
|
| 8 |
+
A=kwargs
|
| 9 |
+
try:
|
| 10 |
+
B=A.get('user_api_key_alias')or'unknown-key';C=A.get('model')or'unknown-model';D=datetime.now().strftime('%Y-%m-%d %H:%M:%S');E=f"[{D}] KEY: {B} | MODEL: {C}\n";os.makedirs(METRICS_DIR,exist_ok=True)
|
| 11 |
+
with open(API_CALLS_LOG,'a')as F:F.write(E)
|
| 12 |
+
except Exception:pass
|
| 13 |
+
proxy_handler_instance=SimpleTextLogger()
|
services/llm_proxy_service.py
CHANGED
|
@@ -71,9 +71,8 @@ router_settings:
|
|
| 71 |
retry_after: 5
|
| 72 |
|
| 73 |
litellm_settings:
|
| 74 |
-
success_callback: ["helicone"]
|
| 75 |
check_provider_endpoint: true
|
| 76 |
-
|
| 77 |
|
| 78 |
general_settings:
|
| 79 |
drop_params: true
|
|
@@ -81,6 +80,6 @@ general_settings:
|
|
| 81 |
def start():
|
| 82 |
os.makedirs(METRICS_DIR,exist_ok=True);A=_build_config()
|
| 83 |
if not A:logger.warning(f"{PREFIX} No API keys loaded or LLM_KEYS not set — skipping llm_proxy");return
|
| 84 |
-
Path(CONFIG_PATH).write_text(A);logger.info(f"{PREFIX} Config written to {CONFIG_PATH}");os.environ['
|
| 85 |
with open(LOG_PATH,'a')as B:D=subprocess.Popen(C,stdout=B,stderr=B)
|
| 86 |
logger.success(f"{PREFIX} litellm proxy started on 127.0.0.1:{PORT} (pid {D.pid})")
|
|
|
|
| 71 |
retry_after: 5
|
| 72 |
|
| 73 |
litellm_settings:
|
|
|
|
| 74 |
check_provider_endpoint: true
|
| 75 |
+
callbacks: ["services.custom_callbacks.proxy_handler_instance"]
|
| 76 |
|
| 77 |
general_settings:
|
| 78 |
drop_params: true
|
|
|
|
| 80 |
def start():
|
| 81 |
os.makedirs(METRICS_DIR,exist_ok=True);A=_build_config()
|
| 82 |
if not A:logger.warning(f"{PREFIX} No API keys loaded or LLM_KEYS not set — skipping llm_proxy");return
|
| 83 |
+
Path(CONFIG_PATH).write_text(A);logger.info(f"{PREFIX} Config written to {CONFIG_PATH}");os.environ['DISABLE_ADMIN_UI']='True';C=['litellm','--config',CONFIG_PATH,'--port',str(PORT),'--host','127.0.0.1']
|
| 84 |
with open(LOG_PATH,'a')as B:D=subprocess.Popen(C,stdout=B,stderr=B)
|
| 85 |
logger.success(f"{PREFIX} litellm proxy started on 127.0.0.1:{PORT} (pid {D.pid})")
|