Terry Zhuo commited on
Commit
19bdd62
·
1 Parent(s): 26f174c
Files changed (1) hide show
  1. app.py +45 -16
app.py CHANGED
@@ -8,12 +8,12 @@ from log_reader import RemoteLogReader
8
  # logging.basicConfig(level=logging.INFO)
9
  # log = logging.getLogger(__name__)
10
 
11
- def get_file_data(content: str) -> tuple[str, bool]:
12
- """Read file content and return IP and vote condition status"""
13
  try:
14
  lines = [line.strip() for line in content.split('\n') if line.strip()]
15
  if not lines:
16
- return None, False
17
 
18
  # Get IP from first line
19
  try:
@@ -22,20 +22,47 @@ def get_file_data(content: str) -> tuple[str, bool]:
22
  except json.JSONDecodeError:
23
  ip = None
24
 
25
- # Check vote conditions from last line
26
- try:
27
- last_line_data = json.loads(lines[-1])
28
- feedback = last_line_data.get('feedback')
29
- vote_conditions_met = (last_line_data.get('type') == 'vote' and
30
- isinstance(feedback, dict) and
31
- len(feedback) == 6)
32
- except json.JSONDecodeError:
33
- vote_conditions_met = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- return ip, vote_conditions_met
36
  except Exception as e:
37
  logging.error(f"Error processing file content: {e}")
38
- return None, False
39
 
40
  def search_battle_anony(date_str: str, search_query: str) -> list[list]:
41
  """Search battle_anony conversations for a specific date and query."""
@@ -53,10 +80,11 @@ def search_battle_anony(date_str: str, search_query: str) -> list[list]:
53
  for conv_id, logs in battle_anony_logs.items():
54
  found_query = False
55
  ip = None
 
56
 
57
  # Convert messages to file content format for validation check
58
  content = '\n'.join(json.dumps(msg) for msg in logs)
59
- ip, is_valid = get_file_data(content)
60
 
61
  if not ip:
62
  continue
@@ -79,6 +107,7 @@ def search_battle_anony(date_str: str, search_query: str) -> list[list]:
79
  # Convert to list format instead of dict
80
  results.append([
81
  ip,
 
82
  conv_id,
83
  'Yes' if is_valid else 'No',
84
  'Yes'
@@ -132,7 +161,7 @@ def create_ui():
132
 
133
  with gr.Row():
134
  table_output = gr.DataFrame(
135
- headers=['IP', 'Conversation ID', 'Is Valid', 'Contains Query'],
136
  label="Results Table",
137
  interactive=False,
138
  value=[] # Initialize with empty list
 
8
  # logging.basicConfig(level=logging.INFO)
9
  # log = logging.getLogger(__name__)
10
 
11
+ def get_file_data(content: str) -> tuple[str, str, bool]:
12
+ """Read file content and return IP, username, and vote condition status"""
13
  try:
14
  lines = [line.strip() for line in content.split('\n') if line.strip()]
15
  if not lines:
16
+ return None, "", False
17
 
18
  # Get IP from first line
19
  try:
 
22
  except json.JSONDecodeError:
23
  ip = None
24
 
25
+ # Find the vote line (if any)
26
+ username = ""
27
+ vote_conditions_met = False
28
+ vote_line_index = -1
29
+
30
+ # Search for the vote line
31
+ for i, line in enumerate(lines):
32
+ try:
33
+ line_data = json.loads(line)
34
+ if line_data.get('type') == 'vote':
35
+ vote_line_index = i
36
+ break
37
+ except json.JSONDecodeError:
38
+ continue
39
+
40
+ # If we found a vote line, check conditions and get username
41
+ if vote_line_index >= 0:
42
+ try:
43
+ vote_line_data = json.loads(lines[vote_line_index])
44
+
45
+ # Only try to get username if the key exists
46
+ if 'username' in vote_line_data:
47
+ username = vote_line_data.get('username')
48
+
49
+ feedback = vote_line_data.get('feedback')
50
+
51
+ # Check vote conditions: type is vote, feedback has 6 items, and at least 4 lines (2 rounds of chat)
52
+ # Only count lines up to and including the vote line
53
+ relevant_lines = lines[:vote_line_index + 1]
54
+ vote_conditions_met = (
55
+ isinstance(feedback, dict) and
56
+ len(feedback) == 6 and
57
+ len(relevant_lines) >= 4
58
+ )
59
+ except (json.JSONDecodeError, TypeError):
60
+ pass
61
 
62
+ return ip, username, vote_conditions_met
63
  except Exception as e:
64
  logging.error(f"Error processing file content: {e}")
65
+ return None, "", False
66
 
67
  def search_battle_anony(date_str: str, search_query: str) -> list[list]:
68
  """Search battle_anony conversations for a specific date and query."""
 
80
  for conv_id, logs in battle_anony_logs.items():
81
  found_query = False
82
  ip = None
83
+ username = ""
84
 
85
  # Convert messages to file content format for validation check
86
  content = '\n'.join(json.dumps(msg) for msg in logs)
87
+ ip, username, is_valid = get_file_data(content)
88
 
89
  if not ip:
90
  continue
 
107
  # Convert to list format instead of dict
108
  results.append([
109
  ip,
110
+ username, # Add username column
111
  conv_id,
112
  'Yes' if is_valid else 'No',
113
  'Yes'
 
161
 
162
  with gr.Row():
163
  table_output = gr.DataFrame(
164
+ headers=['IP', 'Username', 'Conversation ID', 'Is Valid', 'Contains Query'],
165
  label="Results Table",
166
  interactive=False,
167
  value=[] # Initialize with empty list