rwitz commited on
Commit
0b9c45d
1 Parent(s): 5b420e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -2,49 +2,47 @@ import gradio as gr
2
  import requests
3
  import os
4
  import json
 
 
5
  from elo import update_elo_ratings # Custom function for ELO ratings
6
 
7
  # Load the chatbot URLs and their respective model names from a JSON file
8
  with open('chatbot_urls.json', 'r') as file:
9
  chatbots = json.load(file)
10
 
11
- # Load existing ELO ratings or initialize them
 
 
 
 
 
 
 
 
 
12
  def read_elo_ratings():
13
- try:
14
- with open('elo_ratings.json', 'r') as file:
15
- return json.load(file)
16
- except FileNotFoundError:
17
- return {model: 1200 for model in chatbots.keys()}
 
 
 
18
 
19
- # Update ELO ratings in a file
20
  def write_elo_ratings(elo_ratings):
21
- with open('elo_ratings.json', 'w') as file:
22
- json.dump(elo_ratings, file, indent=4)
23
-
24
- elo_ratings = read_elo_ratings()
25
 
26
  def get_bot_response(url, prompt):
27
- payload = {
28
- "input": {
29
- "prompt": prompt,
30
- "sampling_params": {
31
- "max_new_tokens": 16,
32
- "temperature": 0.7,
33
- }
34
- }
35
- }
36
- headers = {
37
- "accept": "application/json",
38
- "content-type": "application/json",
39
- "authorization": os.environ.get("RUNPOD_TOKEN")
40
- }
41
- response = requests.post(url, json=payload, headers=headers)
42
- return response.json()
43
 
44
  def chat_with_bots(user_input):
45
- bot1_url = chatbots[list(chatbots.keys())[random.randint(0,len(list(chatbots.keys())))]]
46
- while bot2_url!=bot1_url:
47
- bot2_url = chatbots[list(chatbots.keys())[random.randint(0,len(list(chatbots.keys())))]]
48
 
49
  bot1_response = get_bot_response(bot1_url, user_input)
50
  bot2_response = get_bot_response(bot2_url, user_input)
@@ -52,7 +50,7 @@ def chat_with_bots(user_input):
52
  return bot1_response, bot2_response
53
 
54
  def update_ratings(bot_index):
55
- global elo_ratings
56
  bot_names = list(chatbots.keys())
57
  winner = bot_names[bot_index]
58
  loser = bot_names[1 - bot_index]
 
2
  import requests
3
  import os
4
  import json
5
+ import random
6
+ import threading
7
  from elo import update_elo_ratings # Custom function for ELO ratings
8
 
9
  # Load the chatbot URLs and their respective model names from a JSON file
10
  with open('chatbot_urls.json', 'r') as file:
11
  chatbots = json.load(file)
12
 
13
+ # Thread-local storage for user-specific data
14
+ user_data = threading.local()
15
+
16
+ # Initialize or get user-specific ELO ratings
17
+ def get_user_elo_ratings():
18
+ if not hasattr(user_data, 'elo_ratings'):
19
+ user_data.elo_ratings = read_elo_ratings()
20
+ return user_data.elo_ratings
21
+
22
+ # Read ELO ratings from file (thread-safe)
23
  def read_elo_ratings():
24
+ elo_ratings = {}
25
+ with threading.Lock():
26
+ try:
27
+ with open('elo_ratings.json', 'r') as file:
28
+ elo_ratings = json.load(file)
29
+ except FileNotFoundError:
30
+ elo_ratings = {model: 1200 for model in chatbots.keys()}
31
+ return elo_ratings
32
 
33
+ # Write ELO ratings to file (thread-safe)
34
  def write_elo_ratings(elo_ratings):
35
+ with threading.Lock():
36
+ with open('elo_ratings.json', 'w') as file:
37
+ json.dump(elo_ratings, file, indent=4)
 
38
 
39
  def get_bot_response(url, prompt):
40
+ # ... (same as before, but add error handling)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  def chat_with_bots(user_input):
43
+ bot_names = list(chatbots.keys())
44
+ random.shuffle(bot_names)
45
+ bot1_url, bot2_url = chatbots[bot_names[0]], chatbots[bot_names[1]]
46
 
47
  bot1_response = get_bot_response(bot1_url, user_input)
48
  bot2_response = get_bot_response(bot2_url, user_input)
 
50
  return bot1_response, bot2_response
51
 
52
  def update_ratings(bot_index):
53
+ elo_ratings = get_user_elo_ratings()
54
  bot_names = list(chatbots.keys())
55
  winner = bot_names[bot_index]
56
  loser = bot_names[1 - bot_index]