-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFluency.py
79 lines (62 loc) · 2.19 KB
/
getFluency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# %%
import openai
# Function to evaluate the fluency of a text using GPT-4
def rate_fluency(text_list):
fluency_ratings = []
for text in text_list:
prompt = f"Please rate the fluency of the following text on a scale of 1 to 5, where 1 is least fluent and 5 is most fluent: \"{text}\". Provide only the number."
messages = [
{"role": "system", "content": "You are a text evaluation assistant."},
{"role": "user", "content": prompt}
]
rating = outputModel_LLM(GPT_4_O_API_ENPOINT, GPT_4_O_MODEL_NAME, messages)
# Append the rating to the list
fluency_ratings.append({text: rating})
return fluency_ratings
# Example list of strings
texts = [
"This is a simple sentence.",
"I are not good at speaking English.",
"The quick brown fox jumps over the lazy dog.",
"Grammar mistake sentence writing bad."
]
# Call the function to rate fluency
# fluency_results = rate_fluency(texts)
# # Print the fluency ratings
# for result in fluency_results:
# print(result)
import json
# %%
modelName = 'meta-llama/Meta-Llama-3-8B-Instruct'
task = 'toxicity'
scale = False
saveFilePath = f'{modelName.split("/")[1]}_{task}_scaling_{scale}_fluency_ratings.json'
# %%
from Utils.utils import *
layers, fileName = getInfo(modelName, task, scale)
import os
if(os.path.exists(f'{saveFilePath}') == False):
import json
with open(f'{saveFilePath}', 'w') as f:
json.dump([], f)
print(f'File created: {saveFilePath}')
with open(f'{saveFilePath}', 'r') as f:
data = json.load(f)
print(f'File loaded: {saveFilePath}')
print(f'Previous data length: {len(data)}')
import pickle
with open(f'{fileName}', 'rb') as f:
texts = pickle.load(f)
for i in range(len(data), len(texts)):
print(f'{i+1}/{len(texts)}')
text = texts[i]
fluency_ratings = rate_fluency([text])
# breakpoint()
import time
time.sleep(1)
data.append({text: fluency_ratings[0]})
with open(f'{saveFilePath}', 'w') as f:
json.dump(data, f, indent=4)
print(f'Fluency rating for text {i+1}: {fluency_ratings[0]}')
with open(f'{saveFilePath}', 'r') as f:
data = json.load(f)