kaikaidai commited on
Commit
8863707
1 Parent(s): be3c6a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -28
app.py CHANGED
@@ -3,6 +3,7 @@ import re
3
  import random
4
  from collections import defaultdict
5
  from datetime import datetime, timezone
 
6
 
7
  from dotenv import load_dotenv
8
 
@@ -62,27 +63,6 @@ def load_model_data():
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:
72
- ip = request.headers["x-forwarded-for"]
73
- if "," in ip:
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):
87
  vote = Vote(
88
  timestamp=datetime.now().isoformat(),
@@ -150,6 +130,21 @@ def submit_prompt(eval_prompt, *variable_values):
150
  )
151
 
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  def vote(
154
  choice,
155
  model_a,
@@ -161,9 +156,9 @@ def vote(
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]
@@ -617,8 +612,9 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
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,
624
  final_prompt_state,
@@ -637,8 +633,9 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
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,
644
  final_prompt_state,
@@ -657,8 +654,9 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
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,
664
  final_prompt_state,
@@ -811,4 +809,4 @@ with gr.Blocks(theme="default", css=CSS_STYLES) as demo:
811
  )
812
 
813
  if __name__ == "__main__":
814
- demo.launch()
 
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
 
 
63
 
64
  model_data = load_model_data()
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def store_vote_data(prompt, response_a, response_b, model_a, model_b, winner, judge_id):
67
  vote = Vote(
68
  timestamp=datetime.now().isoformat(),
 
130
  )
131
 
132
 
133
+ def get_ip(request: gr.Request) -> str:
134
+ """Get and hash the IP address from the request."""
135
+ if "cf-connecting-ip" in request.headers:
136
+ ip = request.headers["cf-connecting-ip"]
137
+ elif "x-forwarded-for" in request.headers:
138
+ ip = request.headers["x-forwarded-for"]
139
+ if "," in ip:
140
+ ip = ip.split(",")[0]
141
+ else:
142
+ ip = request.client.host
143
+
144
+ # Hash the IP address for privacy
145
+ return hashlib.sha256(ip.encode()).hexdigest()[:16]
146
+
147
+
148
  def vote(
149
  choice,
150
  model_a,
 
156
  critique_b,
157
  request: gr.Request,
158
  ):
159
+ # Get hashed IP as judge_id
160
+ judge_id = get_ip(request)
161
+
162
  # Update ELO scores based on user choice
163
  elo_a = elo_scores[model_a]
164
  elo_b = elo_scores[model_b]
 
612
 
613
  # Update the vote button click handlers
614
  vote_a.click(
615
+ fn=vote,
616
  inputs=[
617
+ gr.State("A"), # Choice
618
  model_a_state,
619
  model_b_state,
620
  final_prompt_state,
 
633
  )
634
 
635
  vote_b.click(
636
+ fn=vote,
637
  inputs=[
638
+ gr.State("B"), # Choice
639
  model_a_state,
640
  model_b_state,
641
  final_prompt_state,
 
654
  )
655
 
656
  vote_tie.click(
657
+ fn=vote,
658
  inputs=[
659
+ gr.State("Tie"), # Choice
660
  model_a_state,
661
  model_b_state,
662
  final_prompt_state,
 
809
  )
810
 
811
  if __name__ == "__main__":
812
+ demo.launch()