Ajaykanth Maddi commited on
Commit
ef2e705
·
1 Parent(s): cb0a1c3

Code Changes - Results Upload

Browse files
Files changed (3) hide show
  1. app.py +16 -3
  2. constants.py +3 -0
  3. utils.py +27 -1
app.py CHANGED
@@ -82,7 +82,8 @@ def run_evaluation(
82
  data = json.loads(line)
83
  useddata[data['id']] = data
84
 
85
- print(f"********Information about usedata: {useddata}")
 
86
 
87
  # Inference loop
88
  results = []
@@ -105,7 +106,7 @@ def run_evaluation(
105
  # print(f"Results: \n*********query: {query}, \n*********Answer: {ans}, \n*********docs: {docs}\n*********\n")
106
 
107
  label, prediction, factlabel = predict(query, ans, docs, model, system, instruction, temperature, dataset)
108
- print("f******** Label: {label}\n******** Prediction: {prediction}\n******** factlabel: {factlabel}\n ******** \n")
109
 
110
  newinstance = {
111
  'id': instance['id'],
@@ -117,7 +118,7 @@ def run_evaluation(
117
  'noise_rate': noise_rate,
118
  'factlabel': factlabel
119
  }
120
- print(f"*********Newinstances: {newinstance}")
121
  results.append(newinstance)
122
  f.write(json.dumps(newinstance, ensure_ascii=False) + '\n')
123
  except Exception as e:
@@ -157,11 +158,23 @@ def run_evaluation(
157
  'correct_tt': correct_tt
158
  })
159
 
 
 
 
 
 
 
 
 
 
 
160
  # Save results
161
  try:
162
  score_file = f"{output_file[:-5]}_result.json"
163
  with open(score_file, 'w') as f:
164
  json.dump(scores, f, ensure_ascii=False, indent=4)
 
 
165
  except Exception as e:
166
  print("Error saving scores:", e)
167
 
 
82
  data = json.loads(line)
83
  useddata[data['id']] = data
84
 
85
+
86
+ # print(f"********Information about usedata: {useddata}")
87
 
88
  # Inference loop
89
  results = []
 
106
  # print(f"Results: \n*********query: {query}, \n*********Answer: {ans}, \n*********docs: {docs}\n*********\n")
107
 
108
  label, prediction, factlabel = predict(query, ans, docs, model, system, instruction, temperature, dataset)
109
+ print(f"******** Label: {label}\n******** Prediction: {prediction}\n******** factlabel: {factlabel}\n ******** \n")
110
 
111
  newinstance = {
112
  'id': instance['id'],
 
118
  'noise_rate': noise_rate,
119
  'factlabel': factlabel
120
  }
121
+ # print(f"*********Newinstances: {newinstance}")
122
  results.append(newinstance)
123
  f.write(json.dumps(newinstance, ensure_ascii=False) + '\n')
124
  except Exception as e:
 
158
  'correct_tt': correct_tt
159
  })
160
 
161
+ # Upload results to Hugging Face Hub
162
+ try:
163
+ upload_file = model.upload_file(output_file, resultpath)
164
+ if upload_file:
165
+ print(f"File {output_file} uploaded successfully to Hugging Face Hub.")
166
+ else:
167
+ print(f"Failed to upload {output_file} to Hugging Face Hub.")
168
+ except Exception as e:
169
+ print(f"Error uploading file: {e}")
170
+
171
  # Save results
172
  try:
173
  score_file = f"{output_file[:-5]}_result.json"
174
  with open(score_file, 'w') as f:
175
  json.dump(scores, f, ensure_ascii=False, indent=4)
176
+ model.upload_file(score_file, resultpath)
177
+ print(f"Scores saved to {score_file} and uploaded to Hugging Face Hub.")
178
  except Exception as e:
179
  print("Error saving scores:", e)
180
 
constants.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+
2
+ HF_DATASET_REPO_NAME = "maddiaks/RGB26Demo"
3
+ HF_REPO_TYPE = "space"
utils.py CHANGED
@@ -1,5 +1,11 @@
1
  import random
2
  import math
 
 
 
 
 
 
3
 
4
  def processdata(instance, noise_rate, passage_num, filename, correct_rate = 0):
5
  query = instance['query']
@@ -115,4 +121,24 @@ def predict(query, ground_truth, docs, model, system, instruction, temperature,
115
  if '事实性错误' in prediction or 'factual errors' in prediction:
116
  factlabel = 1
117
 
118
- return labels,prediction, factlabel
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import random
2
  import math
3
+ import json
4
+ import numpy as np
5
+ import os
6
+ from huggingface_hub import HfApi
7
+ from constants import HF_DATASET_REPO_NAME, HF_REPO_TYPE
8
+
9
 
10
  def processdata(instance, noise_rate, passage_num, filename, correct_rate = 0):
11
  query = instance['query']
 
121
  if '事实性错误' in prediction or 'factual errors' in prediction:
122
  factlabel = 1
123
 
124
+ return labels,prediction, factlabel
125
+
126
+ def upload_file(filename: str, folder_path: str) -> str:
127
+ """Upload a file to Hugging Face hub from the specified folder."""
128
+ try:
129
+ # file_path = os.path.join(folder_path, filename)
130
+ # if not os.path.exists(file_path):
131
+ # raise FileNotFoundError(f"File {file_path} does not exist.")
132
+
133
+ api = HfApi()
134
+ api.upload_file(
135
+ path_or_fileobj=filename,
136
+ path_in_repo=f"{folder_path}/{filename}",
137
+ repo_id=HF_DATASET_REPO_NAME,
138
+ repo_type=HF_REPO_TYPE,
139
+ token=os.getenv("HF_TOKEN")
140
+ )
141
+ print(f"Uploaded {filename} to {HF_DATASET_REPO_NAME}")
142
+ except Exception as e:
143
+ print(f"Error uploading {filename}: {e}")
144
+ return None