Spaces:
Sleeping
Sleeping
File size: 11,137 Bytes
22e1b62 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
import os
from config import (
CHATGPT,
GEMINI,
GEMINI_MODEL,
IS_OUTPUT_NORMALIZATION,
MODEL_PATHS,
OPENAI_MODEL,
TEMPERATURE,
TOGETHER_API_KEY,
TOGETHER_PATH,
)
from evaluation import extract_by_best_similarity
from openai import OpenAI
from utils import (
generate_column_names,
generate_file_name,
get_column,
normalize_text,
print_and_log,
read_csv_data,
write_new_data,
write_to_csv,
)
def abstract_proofread(model_path, temperature, base_url, api_key, prompt):
"""
Function to proofread an abstract using an AI language model.
Parameters:
model_path (str): The path or identifier of the AI model to use.
temperature (float): Sampling temperature for the model's output.
base_url (str): The base URL for the API endpoint.
api_key (str): The API key for authentication.
prompt (str): The text prompt to provide to the AI for proofreading.
Returns:
str: The proofread abstract generated by the AI model.
"""
# Initialize the AI client with the provided API key and base URL
ai_client = OpenAI(api_key=api_key, base_url=base_url)
# Create a chat completion request with the system message and user prompt
chat_completion = ai_client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are an AI assistant",
},
{
"role": "user",
"content": prompt,
},
],
model=model_path,
max_tokens=1024,
temperature=temperature,
)
# Return the content of the first choice's message
return chat_completion.choices[0].message.content
def proofread_by_model_name(model_name, input_text, normalize_output):
"""
Proofreads the given input text using the specified model.
Args:
model_name (str): The name of the model to use for proofreading.
input_text (str): The text to be proofread.
normalize_output (bool): Whether to normalize the output or not.
Returns:
str: The proofread text.
"""
# Constants for API access
base_url = TOGETHER_PATH
api_key = TOGETHER_API_KEY
temperature = TEMPERATURE
# Retrieve the model path from the dictionary
if model_name in MODEL_PATHS:
model_path = MODEL_PATHS[model_name]
else:
raise ValueError("Model name not found in the dictionary.")
# Formulate the prompt for the model
prompt = f"Proofreading for the text: ```{input_text}```"
# Apply output normalization if required
if normalize_output:
prompt = output_normalization(prompt)
# Debugging: Print the prompt
print(f"Prompt: {prompt}")
# Call the abstract proofreading function with the prepared parameters
return abstract_proofread(
model_path,
temperature,
base_url,
api_key,
prompt,
)
def gemini_proofread(input_text, normalize_output):
"""
Proofreads the given text using the GEMINI_MODEL.
Parameters:
input_text (str): The text to be proofread.
normalize_output (bool): Flag indicating whether to normalize the output.
Returns:
str: The proofread text.
"""
prompt = f"Proofreading for the text: ```{input_text}```"
if normalize_output:
prompt = output_normalization(prompt)
response = GEMINI_MODEL.generate_content(prompt)
return response.text
def chatGPT_proofread(input_text, normalize_output):
"""
Proofreads the given text using the chat_model.
Parameters:
input_text (str): The text to be proofread.
normalize_output (bool): Flag indicating whether to normalize the output.
Returns:
str: The proofread text.
"""
prompt = f"Proofreading for the text: ```{input_text}```"
if normalize_output:
prompt = output_normalization(prompt)
print(f"Starting API call with prompt: {prompt}")
result = OPENAI_MODEL.predict(prompt)
print(f"Ending API call with prompt: {prompt}")
return result
def output_normalization(prompt):
"""
Normalizes the output by appending a specific instruction to the prompt.
Parameters:
prompt (str): The initial prompt.
Returns:
str: The modified prompt.
"""
return (
prompt
+ " Please only output the proofread text without any explanation."
)
def proofread_with_best_similarity(input_text, model_kind):
"""
Proofreads the input text using the specified model and extracts the
best-corrected text based on similarity.
Args:
input_text (str): The original text to be proofread.
model_kind (str): The kind of model to use for proofreading
(e.g., CHATGPT, GEMINI).
Returns:
tuple: A tuple containing the raw proofread text and the
best-corrected text.
"""
# Normalize the input text
normalized_input_text = normalize_text(input_text)
print_and_log(f"INPUT = {normalized_input_text}")
result_text = ""
raw_text = ""
for i in range(
1,
): # Loop is redundant as it runs only once;
# consider removing if unnecessary
# Select the proofreading model based on model_kind
if model_kind == CHATGPT:
raw_text = chatGPT_proofread(
normalized_input_text,
normalize_output=IS_OUTPUT_NORMALIZATION,
)
elif model_kind == GEMINI:
raw_text = gemini_proofread(
normalized_input_text,
normalize_output=IS_OUTPUT_NORMALIZATION,
)
else:
raw_text = proofread_by_model_name(
model_kind,
normalized_input_text,
normalize_output=IS_OUTPUT_NORMALIZATION,
)
# Extract the best candidate text based on similarity
result_text = extract_by_best_similarity(
normalized_input_text,
raw_text,
)
# Log the raw and result texts
print_and_log(f"RAW_{i} = {raw_text}")
print
# Normalize the result text
result_text = normalize_text(result_text)
# If a valid result is obtained, return it
if result_text != "":
return raw_text, result_text
# Return the raw and result texts
return raw_text, result_text
def generate_new_data_with_best_similarity(
existing_data_file,
existing_kinds,
new_kinds,
):
"""
Generates new data with the best similarity based on existing and new
kinds, and writes the results to a CSV file.
Args:
existing_data_file (str): The path to the existing data file.
existing_kinds (list): A list of existing kinds.
new_kinds (list): A list of new kinds.
Returns:
None
"""
# Combine existing and new kinds into a single list
all_kinds = existing_kinds + new_kinds
# Generate column names for the CSV file
column_names = generate_column_names(all_kinds)
# Generate column names for existing kinds
existing_column_names = generate_column_names(existing_kinds)
# Generate the output file name
output_file = generate_file_name(
existing_data_file,
existing_kinds,
new_kinds,
)
# Create the output file with column names if it doesn't exist
if not os.path.exists(output_file):
write_to_csv(output_file, column_names)
# Read existing data from the file
existing_data = {
kind: get_column(existing_data_file, kind)
for kind in existing_column_names
}
# Read input data from the output file
input_data = read_csv_data(output_file)
start_index = len(input_data)
print(f"start_index = {start_index}")
num_rows = len(existing_data["human"])
global_generate_set = []
global_reuse = []
for index in range(start_index, num_rows):
# Initialize generation and reuse sets
generate_set = []
reuse_set = []
# Prepare the current generation dictionary
current_generation = {
kind: existing_data[kind][index] for kind in existing_column_names
}
print(f"current_generation before generation = {current_generation}")
human_text = current_generation["human"]
# Generate new kinds based on human text
for kind in new_kinds:
_, generated_text = proofread_with_best_similarity(
human_text,
kind,
)
current_generation[kind] = generated_text
generate_set.append(kind)
print(f"current_generation after generate one = {current_generation}")
# Generate combinations of kinds
for first_kind in all_kinds:
for second_kind in all_kinds:
combination_name = f"{first_kind}_{second_kind}"
if combination_name not in current_generation:
if (
first_kind in current_generation
and current_generation[first_kind] == human_text
):
generated_text = current_generation[second_kind]
reuse_set.append(
f"{combination_name} from {second_kind}",
)
else:
is_need_generation = True
for first_kind_2 in all_kinds:
if (
first_kind != first_kind_2
and current_generation[first_kind]
== current_generation[first_kind_2]
):
combination_name_2 = (
f"{first_kind_2}_{second_kind}"
)
if combination_name_2 in current_generation:
generated_text = current_generation[
combination_name_2
]
reuse_set.append(
f"{combination_name} from {combination_name_2}", # noqa: E501
)
is_need_generation = False
break
if is_need_generation:
_, generated_text = proofread_with_best_similarity(
current_generation[first_kind],
second_kind,
)
generate_set.append(f"{first_kind}_{second_kind}")
current_generation[combination_name] = generated_text
# Write the current generation to the output file
write_new_data(output_file, current_generation, column_names)
# Update global sets
global_generate_set.append(generate_set)
global_reuse
|