Spaces:
Running
Running
File size: 7,009 Bytes
3d3c8d1 f5a4353 3d3c8d1 4b8a8ee 3d3c8d1 4b8a8ee 4af3a7a 3d3c8d1 4b8a8ee 3d3c8d1 4b8a8ee 3d3c8d1 f5a4353 3d3c8d1 3847dde 4b8a8ee 3847dde 4b8a8ee 4af3a7a 4b8a8ee 3d3c8d1 9e31af8 4b8a8ee 3d3c8d1 4b8a8ee 150178c 3d3c8d1 879369a 3d3c8d1 |
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 |
import parser
import requests
import re
def zephyr_score(sentence):
prompt = f"""<|user|>
You are an assistant helping with paper reviews.
You will be tasked to classify sentences as 'J' or 'V'
'J' is positive or 'J' is encouraging.
'J' has a neutral tone or 'J' is professional.
'V' is overly blunt or 'V' contains excessive negativity and no constructive feedback.
'V' contains an accusatory tone or 'V' contains sweeping generalizations or 'V' contains personal attacks.
Text: "{sentence}"
Please classify this text as either 'J' or 'V'. Only output 'J' or 'V' with no additional explanation.<|endoftext|>
<|assistant|>
"""
return prompt
def zephyr_revise(sentence):
prompt = f"""<|user|>
You are an assistant that helps users revise Paper Reviews.
Paper reviews exist to provide authors of academic research papers constructive critism.
This is text found in a review.
This text was classified as 'toxic':
Text: "{sentence}"
Please revise this text such that it maintains the criticism in the original text and delivers it in a friendly but professional manner. Make minimal changes to the original text.<|endoftext|>
<|assistant|>
"""
return prompt
def mistral_score(sentence):
prompt = f"""<s>[INST]
You are an assistant helping with paper reviews.
You will be tasked to classify sentences as 'J' or 'V'
'J' is positive or 'J' is encouraging.
'J' has a neutral tone or 'J' is professional.
'V' is overly blunt or 'V' contains excessive negativity and no constructive feedback.
'V' contains an accusatory tone or 'V' contains sweeping generalizations or 'V' contains personal attacks.
Text: "{sentence}"
Please classify this text as either 'J' or 'V'. Only output 'J' or 'V' with no additional explanation. [/INST]"""
return prompt
def mistral_revise(sentence):
prompt = f"""<s>[INST]
You are an assistant that helps users revise Paper Reviews.
Paper reviews exist to provide authors of academic research papers constructive critism.
This is text found in a review.
This text was classified as 'toxic':
Text: "{sentence}"
Please revise this text such that it maintains the criticism in the original text and delivers it in a friendly but professional manner. Make minimal changes to the original text. [/INST] Revised Text: """
return prompt
def phi_score(sentence):
prompt = f"""<|system|>
You are an assistant helping with paper reviews.
You will be tasked to classify sentences as 'J' or 'V'
'J' is positive or 'J' is encouraging.
'J' has a neutral tone or 'J' is not discouraging and not encouraging.
'V' is overly blunt or 'V' contains excessive negativity and no constructive feedback.
'V' contains an accusatory tone or 'V' contains sweeping generalizations or 'V' contains personal attacks.<|end|>
<|user|>
Text: "{sentence}"
Please classify this text as either 'J' or 'V'. Only output 'J' or 'V' with no additional explanation.<|end|>
<|assistant|>
"""
return prompt
def phi_revise(sentence):
prompt = f"""<|system|>
You are an assistant that helps users revise Paper Reviews.
Paper reviews exist to provide authors of academic research papers constructive critism.<|end|>
<|user|>
This is text found in a review.
This text was classified as 'toxic'
Text: "{sentence}"
Please revise this text such that it maintains the criticism in the original text and delivers it in a friendly but professional manner. Make minimal changes to the original text.<|end|>
<|assistant|>
"""
return prompt
def query_model_score(sentence, api_key, model_id, prompt_fun):
API_URL = f"/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2F%3Cspan class="hljs-subst">{model_id}"
headers = {"Authorization": f"Bearer {api_key}"}
prompt = prompt_fun(sentence)
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
parameters = {"max_new_tokens" : 5, "temperature": 0.1, "return_full_text": False}
options = {"wait_for_model": True}
data = query({"inputs": f"{prompt}", "parameters": parameters, "options": options})
score = data[0]['generated_text']
if 'v' in score.lower():
return 1
else:
return 0
def query_model_revise(sentence, api_key, model_id, prompt_fun):
API_URL = f"/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2F%3Cspan class="hljs-subst">{model_id}"
headers = {"Authorization": f"Bearer {api_key}"}
prompt = prompt_fun(sentence)
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
parameters = {"max_new_tokens" : 200, "temperature": 0.1, "return_full_text": False}
options = {"wait_for_model": True}
data = query({"inputs": f"{prompt}", "parameters": parameters, "options": options})
revision = data[0]['generated_text']
return revision
def extract_quotes(text):
pattern = r'"([^"]*)"'
matches = re.findall(pattern, text)
if matches:
return ' '.join(matches)
else:
return text
def revise_review(review, api_key, model_id, highlight_color):
result = {
"success": False,
"data": {
"revision": "",
"score": "",
"sentence_count": "",
"revised_sentences": ""
},
"message": ""
}
if 'zephyr' in model_id.lower():
revision_prompt = zephyr_revise
score_prompt = zephyr_score
elif 'mistral' in model_id.lower():
revision_prompt = mistral_revise
score_prompt = mistral_score
elif 'phi' in model_id.lower():
revision_prompt = phi_revise
score_prompt = phi_score
try:
review = review.replace('"', "'")
sentences = parser.parse_sentences(review)
review_score = 0
revision_count = 0
review_revision = ""
for sentence in sentences:
if len(sentence) > 10:
score = query_model_score(sentence, api_key, model_id, score_prompt)
if score == 0:
review_revision += " " + sentence
else:
review_score = 1
revision_count +=1
revision = query_model_revise(sentence, api_key, model_id, revision_prompt)
revision = extract_quotes(revision)
revision = revision.strip().strip('"')
review_revision += f" <div style='background-color: {highlight_color}; display: inline;'>{revision}</div>"
else:
review_revision += " " + sentence
# end revision/prepare return json
result["success"] = True
result["message"] = "Review successfully revised!"
result["data"]["revision"] = review_revision
result["data"]["score"] = review_score
result["data"]["sentence_count"] = sum(1 for sentence in sentences if len(sentence) > 20)
result["data"]["revised_sentences"] = revision_count
except Exception as e:
result["message"] = str(e)
return result
|