Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -234,19 +234,43 @@ def load_model(model_key: str):
|
|
| 234 |
|
| 235 |
|
| 236 |
def get_prediction(model, text: str, task: str, model_key: str) -> str:
|
| 237 |
-
"""Get model prediction for the given text and task.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
if task == 'fact_verification':
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
else:
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
messages = [
|
| 249 |
-
{"role": "system", "content": system_prompt},
|
| 250 |
{"role": "user", "content": user_prompt},
|
| 251 |
]
|
| 252 |
|
|
@@ -258,13 +282,25 @@ def get_prediction(model, text: str, task: str, model_key: str) -> str:
|
|
| 258 |
|
| 259 |
output = response['choices'][0]['message']['content'].strip().upper()
|
| 260 |
|
| 261 |
-
#
|
| 262 |
-
|
| 263 |
-
if
|
| 264 |
-
return
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
|
| 270 |
def run_attack(text: str, style: str, model_key: str, task: str, progress=gr.Progress()):
|
|
|
|
| 234 |
|
| 235 |
|
| 236 |
def get_prediction(model, text: str, task: str, model_key: str) -> str:
|
| 237 |
+
"""Get model prediction for the given text and task.
|
| 238 |
+
|
| 239 |
+
Uses the same prompt structure as the actual experiments
|
| 240 |
+
(phase1_evaluation.ipynb) for consistency.
|
| 241 |
+
"""
|
| 242 |
|
| 243 |
if task == 'fact_verification':
|
| 244 |
+
user_prompt = f"""Classify the following claim as either 'SUPPORTS', 'REFUTES', or 'NOT_ENOUGH_INFO'.
|
| 245 |
+
|
| 246 |
+
The available classes are:
|
| 247 |
+
- "SUPPORTS": The claim is true or supported by common knowledge.
|
| 248 |
+
- "REFUTES": The claim is false or contradicts established facts.
|
| 249 |
+
- "NOT_ENOUGH_INFO": The claim cannot be verified with common knowledge.
|
| 250 |
+
|
| 251 |
+
### Important:
|
| 252 |
+
- **Only choose one class from the above-mentioned classes.**
|
| 253 |
+
- **Answer with just one word, no other explanations.**
|
| 254 |
+
|
| 255 |
+
Claim: {text}
|
| 256 |
+
|
| 257 |
+
Answer:"""
|
| 258 |
else:
|
| 259 |
+
user_prompt = f"""Determine if the following sentence is an argument. An argument is a statement that takes a position on a topic and provides reasoning or evidence.
|
| 260 |
+
|
| 261 |
+
The available classes are:
|
| 262 |
+
- "ARGUMENT": The sentence is an argument (takes a stance with reasoning).
|
| 263 |
+
- "NOT_ARGUMENT": The sentence is not an argument (factual statement, question, or lacks clear stance).
|
| 264 |
+
|
| 265 |
+
### Important:
|
| 266 |
+
- **Only choose one class from the above-mentioned classes.**
|
| 267 |
+
- **Answer with just one word, no other explanations.**
|
| 268 |
+
|
| 269 |
+
Sentence: {text}
|
| 270 |
+
|
| 271 |
+
Answer:"""
|
| 272 |
|
| 273 |
messages = [
|
|
|
|
| 274 |
{"role": "user", "content": user_prompt},
|
| 275 |
]
|
| 276 |
|
|
|
|
| 282 |
|
| 283 |
output = response['choices'][0]['message']['content'].strip().upper()
|
| 284 |
|
| 285 |
+
# Robust label extraction (matches experiment extract_classification logic)
|
| 286 |
+
if task == 'fact_verification':
|
| 287 |
+
if 'NOT_ENOUGH_INFO' in output or 'NOT ENOUGH INFO' in output or 'NEI' in output:
|
| 288 |
+
return 'NOT_ENOUGH_INFO'
|
| 289 |
+
if 'REFUTE' in output:
|
| 290 |
+
return 'REFUTES'
|
| 291 |
+
if 'SUPPORT' in output:
|
| 292 |
+
return 'SUPPORTS'
|
| 293 |
+
return 'NOT_ENOUGH_INFO'
|
| 294 |
+
else:
|
| 295 |
+
if 'NOT_ARGUMENT' in output or 'NOT ARGUMENT' in output or 'NOT AN ARGUMENT' in output:
|
| 296 |
+
return 'NOT_ARGUMENT'
|
| 297 |
+
if output.startswith('NO'):
|
| 298 |
+
return 'NOT_ARGUMENT'
|
| 299 |
+
if 'ARGUMENT' in output:
|
| 300 |
+
return 'ARGUMENT'
|
| 301 |
+
if output.startswith('YES'):
|
| 302 |
+
return 'ARGUMENT'
|
| 303 |
+
return 'NOT_ARGUMENT'
|
| 304 |
|
| 305 |
|
| 306 |
def run_attack(text: str, style: str, model_key: str, task: str, progress=gr.Progress()):
|