Spaces:
Running
Running
Updated logging
Browse files
app.py
CHANGED
|
@@ -13,10 +13,13 @@ load_dotenv()
|
|
| 13 |
# #===========================================
|
| 14 |
# updates = '''
|
| 15 |
# Updates
|
|
|
|
|
|
|
|
|
|
| 16 |
# +10/10/2025
|
| 17 |
# - Update the model options since Gemma-2-9B-it
|
| 18 |
# is no longer supported. Replaced with GPT-OSS-120B
|
| 19 |
-
|
| 20 |
# + 04/20/2025
|
| 21 |
# - Changed the inference from HF b/c
|
| 22 |
# API calls are not very limted.
|
|
@@ -36,6 +39,120 @@ load_dotenv()
|
|
| 36 |
|
| 37 |
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
API_CALL_LIMIT = 20 # Define the limit
|
| 40 |
|
| 41 |
if 'api_call_count' not in st.session_state:
|
|
@@ -321,7 +438,20 @@ if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question "):
|
|
| 321 |
|
| 322 |
remaining_calls = (API_CALL_LIMIT) - st.session_state.api_call_count
|
| 323 |
st.markdown(f"\n\n <span style='float: right; font-size: 0.8em; color: gray;'>API calls:({remaining_calls}/{API_CALL_LIMIT})</span>", unsafe_allow_html=True)
|
| 324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
except Exception as e:
|
| 326 |
response = "π΅βπ« Looks like someone unplugged something!\
|
| 327 |
\n Either the model space is being updated or something is down.\
|
|
|
|
| 13 |
# #===========================================
|
| 14 |
# updates = '''
|
| 15 |
# Updates
|
| 16 |
+
# +01/08/2026
|
| 17 |
+
# - Updated logging info
|
| 18 |
+
|
| 19 |
# +10/10/2025
|
| 20 |
# - Update the model options since Gemma-2-9B-it
|
| 21 |
# is no longer supported. Replaced with GPT-OSS-120B
|
| 22 |
+
|
| 23 |
# + 04/20/2025
|
| 24 |
# - Changed the inference from HF b/c
|
| 25 |
# API calls are not very limted.
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
#==========================================================
|
| 47 |
+
# Logging
|
| 48 |
+
# --------------------------------------------
|
| 49 |
+
|
| 50 |
+
import requests
|
| 51 |
+
from datetime import datetime
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
LOGGER_TOOL_WEBHOOK = os.environ.get("LOGGER_TOOL_URL")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"β Error in loading LOGGER_TOOL_WEBHOOK: {e}")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def log_to_webhook(
|
| 62 |
+
*,
|
| 63 |
+
session_info: dict,
|
| 64 |
+
model: str,
|
| 65 |
+
prompt: str,
|
| 66 |
+
response: str,
|
| 67 |
+
temperature: float,
|
| 68 |
+
):
|
| 69 |
+
if not LOGGER_TOOL_WEBHOOK:
|
| 70 |
+
return
|
| 71 |
+
|
| 72 |
+
payload = {
|
| 73 |
+
#Session info
|
| 74 |
+
**session_info,
|
| 75 |
+
|
| 76 |
+
#Model info
|
| 77 |
+
"model": model,
|
| 78 |
+
"temperature": temperature,
|
| 79 |
+
|
| 80 |
+
#Content
|
| 81 |
+
"user_prompt": prompt,
|
| 82 |
+
"assistant_response": response,
|
| 83 |
+
|
| 84 |
+
#Usage
|
| 85 |
+
"api_call_count": st.session_state.api_call_count,
|
| 86 |
+
"api_call_limit": API_CALL_LIMIT,
|
| 87 |
+
"remaining_calls": API_CALL_LIMIT - st.session_state.api_call_count,
|
| 88 |
+
|
| 89 |
+
#Timestamp
|
| 90 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
requests.post(LOGGER_TOOL_WEBHOOK, json=payload, timeout=3)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print("Logging failed:", e)
|
| 97 |
+
|
| 98 |
+
# --------------------------------------------
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
#==========================================================
|
| 102 |
+
# Unique Users / Session Info
|
| 103 |
+
# --------------------------------------------
|
| 104 |
+
import uuid
|
| 105 |
+
import time
|
| 106 |
+
import hashlib
|
| 107 |
+
import json
|
| 108 |
+
import sys
|
| 109 |
+
from datetime import datetime
|
| 110 |
+
|
| 111 |
+
def get_session_info():
|
| 112 |
+
data = {
|
| 113 |
+
"timezone": time.tzname,
|
| 114 |
+
"platform": sys.platform,
|
| 115 |
+
}
|
| 116 |
+
raw = json.dumps(data, sort_keys=True)
|
| 117 |
+
return hashlib.sha256(raw.encode()).hexdigest()[:12]
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
if "session_info" not in st.session_state:
|
| 121 |
+
st.session_state.session_info = {
|
| 122 |
+
"session_id": str(uuid.uuid4()),
|
| 123 |
+
"session_start": datetime.utcnow().isoformat(),
|
| 124 |
+
"conversation_id": str(uuid.uuid4()),
|
| 125 |
+
"run_count": 0,
|
| 126 |
+
"fingerprint": get_session_info(),
|
| 127 |
+
"platform": sys.platform,
|
| 128 |
+
"timezone": time.tzname,
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
st.session_state.session_info["run_count"] += 1
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def reset_conversation():
|
| 136 |
+
st.session_state.conversation = []
|
| 137 |
+
st.session_state.messages = []
|
| 138 |
+
st.session_state.session_info["conversation_id"] = str(uuid.uuid4())
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
API_CALL_LIMIT = 20 # Define the limit
|
| 157 |
|
| 158 |
if 'api_call_count' not in st.session_state:
|
|
|
|
| 438 |
|
| 439 |
remaining_calls = (API_CALL_LIMIT) - st.session_state.api_call_count
|
| 440 |
st.markdown(f"\n\n <span style='float: right; font-size: 0.8em; color: gray;'>API calls:({remaining_calls}/{API_CALL_LIMIT})</span>", unsafe_allow_html=True)
|
| 441 |
+
|
| 442 |
+
#Logging
|
| 443 |
+
try:
|
| 444 |
+
log_to_webhook(
|
| 445 |
+
session_info=st.session_state.session_info,
|
| 446 |
+
model=selected_model,
|
| 447 |
+
prompt=prompt,
|
| 448 |
+
response=response,
|
| 449 |
+
temperature=temp_values,
|
| 450 |
+
)
|
| 451 |
+
except Exception:
|
| 452 |
+
pass
|
| 453 |
+
|
| 454 |
+
|
| 455 |
except Exception as e:
|
| 456 |
response = "π΅βπ« Looks like someone unplugged something!\
|
| 457 |
\n Either the model space is being updated or something is down.\
|