deema commited on
Commit
c006a72
1 Parent(s): 79823b5

add database

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. app.py +10 -10
  3. database.py +34 -0
  4. read_responses.py +12 -0
  5. users_ranking.txt +0 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /__pycache__
app.py CHANGED
@@ -5,6 +5,8 @@ from collections import defaultdict
5
  import json
6
  import os
7
  import sys
 
 
8
 
9
  css = """
10
  .rtl{
@@ -14,6 +16,7 @@ css = """
14
  direction: rtl !important;
15
  }
16
  """
 
17
  file_path = 'output/merged.json'
18
  df = pd.read_json(file_path, orient='records', lines=False)
19
 
@@ -34,17 +37,14 @@ def process_rankings(user_rankings):
34
  model = answer_id.split('_')[0] # Extracting the model name from the answer_id
35
  rank_suffix = get_rank_suffix(rank)
36
  model_rankings[model][f'{rank}{rank_suffix}'] += 1 # Using the correct suffix based on the rank
37
-
38
- file_path = 'users_ranking.txt'
39
- if not os.path.exists(file_path):
40
- # If the file path does not exist, print an error message and exit the script
41
- print(f"Error: The file path '{file_path}' does not exist.")
42
- sys.exit(1) # Exit the script with an error code (1 is a common choice for a general error)
43
-
44
- with open(file_path, 'a') as file:
45
  model_rankings_dict = dict(model_rankings)
46
- json.dump(model_rankings_dict, file)
47
- file.write('\n') # Add a newline to separate entries
 
 
 
 
 
48
  print("Updated Model Rankings:", model_rankings) # Debugging print
49
  return
50
 
 
5
  import json
6
  import os
7
  import sys
8
+ from database import save_response
9
+
10
 
11
  css = """
12
  .rtl{
 
16
  direction: rtl !important;
17
  }
18
  """
19
+
20
  file_path = 'output/merged.json'
21
  df = pd.read_json(file_path, orient='records', lines=False)
22
 
 
37
  model = answer_id.split('_')[0] # Extracting the model name from the answer_id
38
  rank_suffix = get_rank_suffix(rank)
39
  model_rankings[model][f'{rank}{rank_suffix}'] += 1 # Using the correct suffix based on the rank
 
 
 
 
 
 
 
 
40
  model_rankings_dict = dict(model_rankings)
41
+
42
+ save_response(model_rankings_dict)
43
+ # file_path = 'users_ranking.txt'
44
+ # with open(file_path, 'a') as file:
45
+ # model_rankings_dict = dict(model_rankings)
46
+ # json.dump(model_rankings_dict, file)
47
+ # file.write('\n') # Add a newline to separate entries
48
  print("Updated Model Rankings:", model_rankings) # Debugging print
49
  return
50
 
database.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pymongo.mongo_client import MongoClient
2
+ from pymongo.server_api import ServerApi
3
+ from urllib.parse import quote_plus
4
+
5
+ password = quote_plus('cidar@2024$')
6
+ uri = f"mongodb+srv://deemaa2:{password}@cluster0.qjruxcb.mongodb.net/?retryWrites=true&w=majority"
7
+
8
+ # Create a new client and connect to the server
9
+ client = MongoClient(uri, server_api=ServerApi('1'))
10
+
11
+ # Specify the database and collection
12
+ db = client.your_database_name
13
+ collection = db.your_collection_name
14
+
15
+ def save_response(response_data):
16
+ """Saves a response to the MongoDB collection."""
17
+ try:
18
+ # Insert the response data into the collection
19
+ collection.insert_one(response_data)
20
+ print("Response saved successfully.")
21
+ except Exception as e:
22
+ print(f"An error occurred: {e}")
23
+
24
+
25
+ def read_responses(filter_query=None):
26
+ """Reads responses from the MongoDB collection based on an optional filter."""
27
+ try:
28
+ if filter_query is None:
29
+ filter_query = {} # An empty query will return all documents
30
+ responses = collection.find(filter_query)
31
+ return list(responses) # Convert cursor to list
32
+ except Exception as e:
33
+ print(f"An error occurred: {e}")
34
+ return []
read_responses.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from database import read_responses
3
+
4
+ def fetch_and_display_responses():
5
+ # Fetch responses from the database
6
+ responses = read_responses()
7
+ # Format the responses for display (simple text format in this example)
8
+ formatted_responses = "\n".join([str(response) for response in responses])
9
+ print(formatted_responses)
10
+ # return formatted_responses
11
+
12
+ fetch_and_display_responses()
users_ranking.txt DELETED
File without changes