Spaces:
Running
on
Zero
Running
on
Zero
donbr
commited on
Commit
·
07bd805
1
Parent(s):
807bf12
update code to fail gracefully when logging no available
Browse files- app.py +7 -2
- nuextract_logging.py +28 -6
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
|
|
|
| 3 |
import time
|
| 4 |
import json
|
| 5 |
from itertools import cycle
|
|
@@ -152,8 +153,12 @@ def gradio_interface_function(template, text, is_example):
|
|
| 152 |
# yield gr.update(value=chunk_info), gr.update(value=progress), gr.update(value=full_pred), gr.update(value=html_content)
|
| 153 |
yield progress, full_pred, html_content
|
| 154 |
|
| 155 |
-
if not
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
# Set up the Gradio interface
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
+
import sys
|
| 4 |
import time
|
| 5 |
import json
|
| 6 |
from itertools import cycle
|
|
|
|
| 153 |
# yield gr.update(value=chunk_info), gr.update(value=progress), gr.update(value=full_pred), gr.update(value=html_content)
|
| 154 |
yield progress, full_pred, html_content
|
| 155 |
|
| 156 |
+
# Conditionally log event if not an example and logging is configured
|
| 157 |
+
if not is_example:
|
| 158 |
+
try:
|
| 159 |
+
log_event(text, template, full_pred)
|
| 160 |
+
except Exception as e:
|
| 161 |
+
print(f"Warning: Could not log event: {e}", file=sys.stderr)
|
| 162 |
|
| 163 |
|
| 164 |
# Set up the Gradio interface
|
nuextract_logging.py
CHANGED
|
@@ -3,14 +3,30 @@ import ssl
|
|
| 3 |
import json
|
| 4 |
import socket
|
| 5 |
import datetime
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
def log_event(input_text, template, prediction):
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
# Create a TCP connection and wrap it with SSL
|
| 16 |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
@@ -22,8 +38,14 @@ def log_event(input_text, template, prediction):
|
|
| 22 |
|
| 23 |
# Send the log message
|
| 24 |
ssl_sock.sendall(log_message.encode('utf-8'))
|
| 25 |
-
print('Log message sent successfully
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
finally:
|
| 28 |
# Close the connection
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
import socket
|
| 5 |
import datetime
|
| 6 |
+
import sys
|
| 7 |
|
| 8 |
|
| 9 |
def log_event(input_text, template, prediction):
|
| 10 |
+
"""Log extraction events to a remote server if configured through environment variables"""
|
| 11 |
+
# Check if environment variables are set
|
| 12 |
+
if not all(key in os.environ for key in ["LOG_SERVER", "LOG_PORT", "LOG_TOKEN"]):
|
| 13 |
+
print("Logging disabled: environment variables LOG_SERVER, LOG_PORT, or LOG_TOKEN not set", file=sys.stderr)
|
| 14 |
+
return
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat()
|
| 18 |
+
# Create a structured log message
|
| 19 |
+
log_message = f'<6>1 {timestamp} 149.202.165.20 example.org - - [exampleSDID@8485 X-OVH-TOKEN="{os.environ["LOG_TOKEN"]}" template={json.dumps(template)} text={json.dumps(input_text)} prediction={json.dumps(prediction)}] {timestamp} / {json.dumps(input_text[:20])}\n'
|
| 20 |
+
|
| 21 |
+
server = os.environ["LOG_SERVER"]
|
| 22 |
+
port = int(os.environ["LOG_PORT"])
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Error preparing log message: {e}", file=sys.stderr)
|
| 25 |
+
return
|
| 26 |
|
| 27 |
+
# Create a TCP connection and wrap it with SSL
|
| 28 |
+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| 29 |
+
ssl_sock = ssl.wrap_socket(sock)
|
| 30 |
|
| 31 |
# Create a TCP connection and wrap it with SSL
|
| 32 |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
| 38 |
|
| 39 |
# Send the log message
|
| 40 |
ssl_sock.sendall(log_message.encode('utf-8'))
|
| 41 |
+
print('Log message sent successfully')
|
| 42 |
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Error sending log message: {e}", file=sys.stderr)
|
| 45 |
+
|
| 46 |
finally:
|
| 47 |
# Close the connection
|
| 48 |
+
try:
|
| 49 |
+
ssl_sock.close()
|
| 50 |
+
except:
|
| 51 |
+
pass
|