kaikaidai commited on
Commit
be3c6a3
1 Parent(s): 06545f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -3,7 +3,6 @@ import re
3
  import random
4
  from collections import defaultdict
5
  from datetime import datetime, timezone
6
- import hashlib
7
 
8
  from dotenv import load_dotenv
9
 
@@ -28,6 +27,8 @@ from common import (
28
  )
29
  from example_metrics import EXAMPLE_METRICS
30
 
 
 
31
 
32
  # Model and ELO score data
33
  DEFAULT_ELO = 1500 # Starting ELO for new models
@@ -61,10 +62,10 @@ def load_model_data():
61
 
62
  model_data = load_model_data()
63
 
 
64
 
65
- # Generate a unique session ID based on the user's IP address
66
- def get_new_session_id(request: gr.Request):
67
- """Generate a unique session ID based on the user's IP address."""
68
  if "cf-connecting-ip" in request.headers:
69
  ip = request.headers["cf-connecting-ip"]
70
  elif "x-forwarded-for" in request.headers:
@@ -73,9 +74,13 @@ def get_new_session_id(request: gr.Request):
73
  ip = ip.split(",")[0]
74
  else:
75
  ip = request.client.host
76
-
77
- # Create a hash of the IP to maintain privacy
78
- return f"user_{hashlib.md5(ip.encode()).hexdigest()[:8]}"
 
 
 
 
79
 
80
 
81
  def store_vote_data(prompt, response_a, response_b, model_a, model_b, winner, judge_id):
@@ -154,8 +159,11 @@ def vote(
154
  critique_a,
155
  score_b,
156
  critique_b,
157
- judge_id,
158
  ):
 
 
 
159
  # Update ELO scores based on user choice
160
  elo_a = elo_scores[model_a]
161
  elo_b = elo_scores[model_b]
@@ -443,7 +451,6 @@ def get_random_metric():
443
 
444
 
445
  with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
446
- judge_id = gr.State(value=lambda: get_new_session_id(gr.Request.current()))
447
  gr.Markdown(MAIN_TITLE)
448
  gr.Markdown(HOW_IT_WORKS)
449
 
@@ -610,7 +617,7 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
610
 
611
  # Update the vote button click handlers
612
  vote_a.click(
613
- fn=lambda *args: vote("A", *args),
614
  inputs=[
615
  model_a_state,
616
  model_b_state,
@@ -619,7 +626,6 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
619
  critique_a,
620
  score_b,
621
  critique_b,
622
- judge_id,
623
  ],
624
  outputs=[
625
  action_buttons_row,
@@ -631,7 +637,7 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
631
  )
632
 
633
  vote_b.click(
634
- fn=lambda *args: vote("B", *args),
635
  inputs=[
636
  model_a_state,
637
  model_b_state,
@@ -640,7 +646,6 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
640
  critique_a,
641
  score_b,
642
  critique_b,
643
- judge_id,
644
  ],
645
  outputs=[
646
  action_buttons_row,
@@ -652,7 +657,7 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
652
  )
653
 
654
  vote_tie.click(
655
- fn=lambda *args: vote("Tie", *args),
656
  inputs=[
657
  model_a_state,
658
  model_b_state,
@@ -661,7 +666,6 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
661
  critique_a,
662
  score_b,
663
  critique_b,
664
- judge_id,
665
  ],
666
  outputs=[
667
  action_buttons_row,
 
3
  import random
4
  from collections import defaultdict
5
  from datetime import datetime, timezone
 
6
 
7
  from dotenv import load_dotenv
8
 
 
27
  )
28
  from example_metrics import EXAMPLE_METRICS
29
 
30
+ import hashlib
31
+
32
 
33
  # Model and ELO score data
34
  DEFAULT_ELO = 1500 # Starting ELO for new models
 
62
 
63
  model_data = load_model_data()
64
 
65
+ current_session_id = 0
66
 
67
+
68
+ def get_ip(request: gr.Request):
 
69
  if "cf-connecting-ip" in request.headers:
70
  ip = request.headers["cf-connecting-ip"]
71
  elif "x-forwarded-for" in request.headers:
 
74
  ip = ip.split(",")[0]
75
  else:
76
  ip = request.client.host
77
+ return ip
78
+
79
+
80
+ def get_new_session_id(request: gr.Request):
81
+ ip = get_ip(request)
82
+ hashed_ip = hashlib.md5(ip.encode()).hexdigest()
83
+ return hashed_ip
84
 
85
 
86
  def store_vote_data(prompt, response_a, response_b, model_a, model_b, winner, judge_id):
 
159
  critique_a,
160
  score_b,
161
  critique_b,
162
+ request: gr.Request,
163
  ):
164
+ # Generate judge_id from hashed IP
165
+ judge_id = get_new_session_id(request)
166
+
167
  # Update ELO scores based on user choice
168
  elo_a = elo_scores[model_a]
169
  elo_b = elo_scores[model_b]
 
451
 
452
 
453
  with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
 
454
  gr.Markdown(MAIN_TITLE)
455
  gr.Markdown(HOW_IT_WORKS)
456
 
 
617
 
618
  # Update the vote button click handlers
619
  vote_a.click(
620
+ fn=lambda *args, request=None: vote("A", *args, request=request),
621
  inputs=[
622
  model_a_state,
623
  model_b_state,
 
626
  critique_a,
627
  score_b,
628
  critique_b,
 
629
  ],
630
  outputs=[
631
  action_buttons_row,
 
637
  )
638
 
639
  vote_b.click(
640
+ fn=lambda *args, request=None: vote("B", *args, request=request),
641
  inputs=[
642
  model_a_state,
643
  model_b_state,
 
646
  critique_a,
647
  score_b,
648
  critique_b,
 
649
  ],
650
  outputs=[
651
  action_buttons_row,
 
657
  )
658
 
659
  vote_tie.click(
660
+ fn=lambda *args, request=None: vote("Tie", *args, request=request),
661
  inputs=[
662
  model_a_state,
663
  model_b_state,
 
666
  critique_a,
667
  score_b,
668
  critique_b,
 
669
  ],
670
  outputs=[
671
  action_buttons_row,