backing up few-shot
This commit is contained in:
parent
6f2858dd72
commit
43984fb605
File diff suppressed because it is too large
Load Diff
110
p2/quest/python_scripts/olmo_labeling/batched_olmo_cat.py
Normal file
110
p2/quest/python_scripts/olmo_labeling/batched_olmo_cat.py
Normal file
@ -0,0 +1,110 @@
|
||||
from transformers import AutoModelForCausalLM, AutoTokenizer, OlmoForCausalLM
|
||||
import torch
|
||||
import csv
|
||||
import pandas as pd
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
#import os
|
||||
#os.environ['BNB_CUDA_VERSION'] = ''
|
||||
#import bitsandbytes
|
||||
|
||||
import nltk
|
||||
nltk.download('punkt_tab')
|
||||
|
||||
cache_directory = "/projects/p32852/cache/"
|
||||
#load in the different models
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
print(device)
|
||||
print(torch.cuda.get_device_name(0))
|
||||
print(torch.cuda.get_device_properties(0))
|
||||
|
||||
#olmo = AutoModelForCausalLM.from_pretrained("allenai/OLMo-2-0325-32B", torch_dtype=torch.float16, load_in_8bit=True, cache_dir=cache_directory).to(device)
|
||||
#olmo = AutoModelForCausalLM.from_pretrained("allenai/OLMo-2-0325-32B-Instruct-GGUF", cache_dir=cache_directory).to(device)
|
||||
#tokenizer = AutoTokenizer.from_pretrained("allenai/OLMo-2-0325-32B-Instruct-GGUF", cache_dir=cache_directory)
|
||||
olmo = AutoModelForCausalLM.from_pretrained("allenai/OLMo-2-1124-13B", cache_dir=cache_directory).to(device)
|
||||
tokenizer = AutoTokenizer.from_pretrained("allenai/OLMo-2-1124-13B", padding_side='left')
|
||||
|
||||
information_types = Path('/home/nws8519/git/mw-lifecycle-analysis/p2/quest/python_scripts/olmo_labeling/info_definitions.txt').read_text(encoding="utf-8")
|
||||
prompt_template = Path('/home/nws8519/git/mw-lifecycle-analysis/p2/quest/python_scripts/olmo_labeling/prompt_template.txt').read_text(encoding="utf-8")
|
||||
|
||||
csv.field_size_limit(sys.maxsize)
|
||||
with open("/home/nws8519/git/mw-lifecycle-analysis/analysis_data/102725_unified.csv", mode='r', newline='') as file:
|
||||
reader = csv.reader(file)
|
||||
array_of_categorizations = []
|
||||
index = -1
|
||||
for row in reader:
|
||||
index += 1
|
||||
if index <= 0:
|
||||
continue
|
||||
text_dict = {}
|
||||
#organizing the data from each citation
|
||||
text_dict['id'] = row[0]
|
||||
text_dict['task_title'] = row[1]
|
||||
task_title = text_dict['task_title']
|
||||
text_dict['comment_text'] = row[2]
|
||||
text_dict['date_created'] = row[3]
|
||||
text_dict['comment_type'] = row[6]
|
||||
text_dict['TaskPHID'] = row[5]
|
||||
text_dict['AuthorPHID'] = row[4]
|
||||
if text_dict['comment_type'] == "task_description":
|
||||
raw_text = text_dict['task_title'] + ". \n\n" + text_dict['comment_text']
|
||||
else:
|
||||
raw_text = text_dict['comment_text']
|
||||
|
||||
# comment_text preprocessing per https://arxiv.org/pdf/1902.07093
|
||||
# 1. replace code with CODE
|
||||
comment_text = re.sub(r'`[^`]+`', 'CODE', raw_text) # Inline code
|
||||
comment_text = re.sub(r'```[\s\S]+?```', 'CODE', comment_text) # Block code
|
||||
# 2. replace quotes with QUOTE
|
||||
lines = comment_text.split('\n')
|
||||
lines = ['QUOTE' if line.strip().startswith('>') else line for line in lines]
|
||||
comment_text = '\n'.join(lines)
|
||||
# 3. replace Gerrit URLs with GERRIT URL
|
||||
gerrit_url_pattern = r'https://gerrit\.wikimedia\.org/r/\d+'
|
||||
comment_text = re.sub(gerrit_url_pattern, 'GERRIT_URL', comment_text)
|
||||
# replace URL with URL
|
||||
url_pattern = r'https?://[^\s]+'
|
||||
comment_text = re.sub(url_pattern, 'URL', comment_text)
|
||||
# 4. if possible, replace @ with SCREEN_NAME
|
||||
comment_text = re.sub(r'(^|\s)@\w+', 'SCREEN_NAME', comment_text)
|
||||
# 5. split into an array of sentences
|
||||
comment_sentences = nltk.sent_tokenize(comment_text)
|
||||
text_dict['cleaned_sentences'] = comment_sentences
|
||||
|
||||
results = []
|
||||
batch_size = 2
|
||||
for i in range(0, len(comment_sentences), batch_size):
|
||||
batch = comment_sentences[i:i+batch_size]
|
||||
prompts = []
|
||||
for sent in batch:
|
||||
prompt = prompt_template.format_map({"info_definitions": information_types, "sent": sent, "task_title": task_title})
|
||||
prompts.append(prompt)
|
||||
inputs = tokenizer(prompts, return_tensors='pt', return_token_type_ids=False, padding=True, truncation=True).to(device)
|
||||
with torch.no_grad():
|
||||
outputs = olmo.generate(**inputs, max_new_tokens=256, do_sample=False)
|
||||
decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
||||
for response_txt in decoded:
|
||||
match = re.search(r"Response: \s*(.*)", response_txt)
|
||||
#print(match)
|
||||
if match:
|
||||
category = re.sub(r"[(),\d]", "", match.group(1)).strip()
|
||||
else:
|
||||
category = "NO CATEGORY"
|
||||
results.append(category)
|
||||
torch.cuda.empty_cache()
|
||||
|
||||
text_dict['sentence_categories']=results
|
||||
print(results)
|
||||
array_of_categorizations.append(text_dict)
|
||||
if index == 20:
|
||||
break
|
||||
df = pd.DataFrame(array_of_categorizations)
|
||||
#print(df.head())
|
||||
#df.to_csv('all_110525_olmo_batched_categorized.csv', index=False)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
24
p2/quest/python_scripts/olmo_labeling/info_definitions.txt
Normal file
24
p2/quest/python_scripts/olmo_labeling/info_definitions.txt
Normal file
@ -0,0 +1,24 @@
|
||||
# Information Type Definitions for Software Engineering Task Discussions
|
||||
|
||||
Sentences in software engineering task discussions often contain different types of information.
|
||||
Each sentence often has only one primary information type.
|
||||
Below are the different kinds of information types found in task discussion sentences:
|
||||
|
||||
EXPECTED BEHAVIOR: A sentence in which stakeholders discuss, from the user’s perspective, the expected or ideal situation affected by the issue. Such as “My suggestion/request in the near term would be to have an option to make the vocabulary read only so that users who want to be able to leave spacy alone to do streaming data processing don’t need to worry about changing memory requirements.”
|
||||
MOTIVATION: A sentence in which stakeholders elaborate on why the issue needs to be fixed or a feature needs to be added. Such as “Right now, this method starves my GPU all the time, which is a shame because most other [deep learning] frameworks manage to make this much more performantly.”
|
||||
OBSERVED BUG BEHAVIOR: A sentence which appears in bug reports and focuses on describing the observed behaviour of the bug. Such as one participant commented: “I found strange behavior using the ‘pipe()’ method”, then started to describe this behavior.
|
||||
BUG REPRODUCTION: A sentence focused on any report, request, and/or question regarding the reproduction of the bug. Such as “Same problem here, working on Windows 10 with German text.”
|
||||
INVESTIGATION AND EXPLORATION: A sentence where OSS stakeholders discuss their exploration of ideas about the problem that was thought to have caused the issue. Such as “This result confirms my hypothesis but also shows that the memory increase really isn’t all that significant... But it still points to a potential flaw in the design of the library.”
|
||||
SOLUTION DISCUSSION: A sentence that is framed around the solution space from the developers’ point of view, in which participants discuss design ideas and implementation details, as well as suggestions, constraints, challenges, and useful references around such topics. Such as “I know there are multiple ways of approaching this however I strongly recommend node-gyp for performance.”
|
||||
CONTRIBUTION AND COMMITMENT: A sentence in which participants call for contributors and/or voice willingness or unwillingness to contribute to resolving the issue. Such as “I will gladly contribute in any way I can, however, this is something I will not be able to do alone. Would be best if a few other people is interested as well...”
|
||||
NA: A sentence which contains only non-English terms or consists entirely of punctuation and numerals. Such as "***", "ve-ce-protectedNode", or "T8597".
|
||||
TASK PROGRESS: A sentence in which stakeholders request or report progress of tasks and sub-tasks towards the solution of the issue. This includes automated reports of merged code changes. Such as “I made an initial stab at it... - this is just a proof of concept that gets the version string into nodejs. I’ll start working on adding the swig interfaces...”
|
||||
TESTING: A sentence in which participants discuss the testing procedure and results, as well as the system environment, code, data, and feedback involved in testing. Such as “Tested on ‘0.101’ and ‘master’ - the issue seems to be fixed on ‘master’ not just for the example document, but for the entire corpus...”
|
||||
FUTURE PLAN: A sentence in which participants discuss the long-term plan related to the issue; such plans usually involve work/ideas that are not required to close the current issue. Such as “For the futures, stay tuned, as we’re prototyping something in this direction.”
|
||||
POTENTIAL NEW ISSUES AND REQUESTS: A sentence in which participants identify and discuss new bugs or needed features while investigating and addressing the current issue. Such as “As a side point, I note there seems to be a lot more joblib parallelisation overhead in master... that wasn’t there in 0.14.”
|
||||
SOLUTION USAGE: A sentence in which stakeholders asked questions or provided suggestions about how to use the library with the new solution update. Such as “Please help me how to continue training the model [with the new release].”
|
||||
WORKAROUNDS: A sentence in which stakeholders discussed temporary or alternative solutions that can help overcome the issue until the official fix or enhancement is released. Such as “For now workaround with reloading / collecting nlp object works quite ok in production.”
|
||||
ISSUE CONTENT MANAGEMENT: A sentence in which a stakeholder focuses on redirecting the discussions and controlling the quality of the comments with respect to the issue. Such as “We might want to move this discussion to here: [link to another issue]” or "This other issue [link to another issue] is a duplicate of this issue".
|
||||
ACTION ON ISSUE: A sentence in which participants comment on the proper actions to perform on the issue itself. Such as “I’m going to close this issue because it’s old and most of the information here is now out of date.”
|
||||
SOCIAL CONVERSATION: A sentence in which participants express emotions such as appreciation, disappointment, annoyance, regret, etc. or engage in small talk. Such as “I’m so glad that this has received so much thought and attention!”, "My apologies." or "Thank you!"
|
||||
|
||||
41
p2/quest/python_scripts/olmo_labeling/prompt_template.txt
Normal file
41
p2/quest/python_scripts/olmo_labeling/prompt_template.txt
Normal file
@ -0,0 +1,41 @@
|
||||
{info_definitions}
|
||||
|
||||
---
|
||||
|
||||
Task
|
||||
|
||||
Given the title of a software engineering task discussion and a sentence from within that discussion, identify the primary information type from the list above that applies to the sentence.
|
||||
For each sentence:
|
||||
1. Provide the information type label (exactly as named above)
|
||||
2. Provide a confidence score from 1-10, where 10 means you are highly confident this information type applies to this sentence.
|
||||
|
||||
Output format (valid tuple only):
|
||||
("INFORMATION_TYPE", CONFIDENCE_SCORE)
|
||||
|
||||
---
|
||||
Examples
|
||||
|
||||
Example 1
|
||||
|
||||
Discussion Title: LocalSettings.php lacks wgSecureLogin, wgCookieHttpOnly and wgCookieSecure
|
||||
Sentence: Both projects failed to enable wgSecureLogin and wgCookieSecure, and plain text passwords were used in subsequent logins.
|
||||
Response: (INVESTIGATION AND EXPLORATION, 8)
|
||||
|
||||
Example 2
|
||||
|
||||
Discussion Title: VisualEditor: Drag-and-drop of content (text, transclusions, references, …) to move it
|
||||
Sentence: *** Bug 50183 has been marked as a duplicate of this bug.
|
||||
Response: (ISSUE CONTENT MANAGEMENT, 6)
|
||||
|
||||
Example 3
|
||||
|
||||
Discussion Title: Can't login to catgraph instance
|
||||
Sentence: When trying to ssh to sylvester I get the following:\n\n``CODE``\n\nThe catgraph service which should be running there is not reachable either (connection refused).
|
||||
Response: (OBSERVED BUG BEHAVIOR, 9)
|
||||
|
||||
---
|
||||
Now label this sentence
|
||||
|
||||
Discussion Title: {task_title}
|
||||
Sentence: {sent}
|
||||
Response:
|
||||
Loading…
Reference in New Issue
Block a user