Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -752,17 +752,6 @@ async def root(request: Request):
|
|
| 752 |
health_data = await health_check()
|
| 753 |
assistant_data = await internal_assistant()
|
| 754 |
|
| 755 |
-
# Fetch logs internally
|
| 756 |
-
try:
|
| 757 |
-
with open("app.log", "r") as f:
|
| 758 |
-
lines = f.readlines()
|
| 759 |
-
# Show all lines
|
| 760 |
-
log_content = "".join(lines)
|
| 761 |
-
# Escape HTML
|
| 762 |
-
log_content = log_content.replace("&", "&").replace("<", "<").replace(">", ">")
|
| 763 |
-
except Exception as e:
|
| 764 |
-
log_content = f"Error reading logs: {e}"
|
| 765 |
-
|
| 766 |
# Format nicely as JSON strings
|
| 767 |
health_json = json.dumps(health_data, indent=2)
|
| 768 |
assistant_json = json.dumps(assistant_data, indent=2)
|
|
@@ -932,10 +921,7 @@ async def root(request: Request):
|
|
| 932 |
</div>
|
| 933 |
|
| 934 |
<div id="logs" class="tab-content active">
|
| 935 |
-
<div
|
| 936 |
-
<button onclick="location.reload()" style="background: rgba(255,255,255,0.1); border: 1px solid rgba(255,255,255,0.2); color: white; padding: 5px 10px; border-radius: 5px; cursor: pointer; transition: 0.3s;" onmouseover="this.style.background='rgba(255,255,255,0.2)'" onmouseout="this.style.background='rgba(255,255,255,0.1)'">🔄 Refresh</button>
|
| 937 |
-
</div>
|
| 938 |
-
<div class="log-container" id="log-container-view">{log_content}</div>
|
| 939 |
</div>
|
| 940 |
|
| 941 |
<div id="assistant" class="tab-content">
|
|
@@ -991,7 +977,38 @@ async def root(request: Request):
|
|
| 991 |
document.getElementById('health-json').innerHTML = syntaxHighlight(document.getElementById('health-json').innerText);
|
| 992 |
document.getElementById('assistant-json').innerHTML = syntaxHighlight(document.getElementById('assistant-json').innerText);
|
| 993 |
|
| 994 |
-
// Auto-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 995 |
window.onload = function() {{
|
| 996 |
var logContainer = document.getElementById('log-container-view');
|
| 997 |
if (logContainer) {{
|
|
@@ -1002,6 +1019,21 @@ async def root(request: Request):
|
|
| 1002 |
</body>
|
| 1003 |
</html>
|
| 1004 |
""".replace("{{log_content}}", log_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1005 |
async def internal_assistant():
|
| 1006 |
load_dotenv(override=True)
|
| 1007 |
|
|
|
|
| 752 |
health_data = await health_check()
|
| 753 |
assistant_data = await internal_assistant()
|
| 754 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
# Format nicely as JSON strings
|
| 756 |
health_json = json.dumps(health_data, indent=2)
|
| 757 |
assistant_json = json.dumps(assistant_data, indent=2)
|
|
|
|
| 921 |
</div>
|
| 922 |
|
| 923 |
<div id="logs" class="tab-content active">
|
| 924 |
+
<div class="log-container" id="log-container-view">Loading logs...</div>
|
|
|
|
|
|
|
|
|
|
| 925 |
</div>
|
| 926 |
|
| 927 |
<div id="assistant" class="tab-content">
|
|
|
|
| 977 |
document.getElementById('health-json').innerHTML = syntaxHighlight(document.getElementById('health-json').innerText);
|
| 978 |
document.getElementById('assistant-json').innerHTML = syntaxHighlight(document.getElementById('assistant-json').innerText);
|
| 979 |
|
| 980 |
+
// React-like Auto-polling for Logs
|
| 981 |
+
let lastLogContent = "";
|
| 982 |
+
let autoScroll = true;
|
| 983 |
+
|
| 984 |
+
function fetchLogs() {{
|
| 985 |
+
fetch('/logs/data')
|
| 986 |
+
.then(response => response.text())
|
| 987 |
+
.then(data => {{
|
| 988 |
+
let logContainer = document.getElementById('log-container-view');
|
| 989 |
+
if (logContainer && data !== lastLogContent) {{
|
| 990 |
+
|
| 991 |
+
// Check if user has scrolled up manually
|
| 992 |
+
// If they are near the bottom, keep auto-scrolling. If they scrolled up, don't force them down.
|
| 993 |
+
let isAtBottom = logContainer.scrollHeight - logContainer.scrollTop <= logContainer.clientHeight + 50;
|
| 994 |
+
|
| 995 |
+
logContainer.innerHTML = data;
|
| 996 |
+
lastLogContent = data;
|
| 997 |
+
|
| 998 |
+
if (isAtBottom || autoScroll) {{
|
| 999 |
+
logContainer.scrollTop = logContainer.scrollHeight;
|
| 1000 |
+
autoScroll = false; // Only force it heavily on the very first load
|
| 1001 |
+
}}
|
| 1002 |
+
}}
|
| 1003 |
+
}})
|
| 1004 |
+
.catch(error => console.error('Error fetching logs:', error));
|
| 1005 |
+
}}
|
| 1006 |
+
|
| 1007 |
+
// Fetch immediately, then poll every 2 seconds
|
| 1008 |
+
fetchLogs();
|
| 1009 |
+
setInterval(fetchLogs, 2000);
|
| 1010 |
+
|
| 1011 |
+
// Auto-scroll logs on initial tab click
|
| 1012 |
window.onload = function() {{
|
| 1013 |
var logContainer = document.getElementById('log-container-view');
|
| 1014 |
if (logContainer) {{
|
|
|
|
| 1019 |
</body>
|
| 1020 |
</html>
|
| 1021 |
""".replace("{{log_content}}", log_content)
|
| 1022 |
+
|
| 1023 |
+
from fastapi.responses import HTMLResponse
|
| 1024 |
+
|
| 1025 |
+
@app.get("/logs/data")
|
| 1026 |
+
async def get_logs_data():
|
| 1027 |
+
"""API Endpoint: Returns raw text for the frontend to poll."""
|
| 1028 |
+
try:
|
| 1029 |
+
with open("app.log", "r") as f:
|
| 1030 |
+
lines = f.readlines()
|
| 1031 |
+
log_content = "".join(lines)
|
| 1032 |
+
log_content = log_content.replace("&", "&").replace("<", "<").replace(">", ">")
|
| 1033 |
+
return HTMLResponse(content=log_content)
|
| 1034 |
+
except Exception as e:
|
| 1035 |
+
return HTMLResponse(content=f"Error reading logs: {e}")
|
| 1036 |
+
|
| 1037 |
async def internal_assistant():
|
| 1038 |
load_dotenv(override=True)
|
| 1039 |
|