Upload QWQ_infer.py with huggingface_hub
Browse files- QWQ_infer.py +86 -0
QWQ_infer.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import pprint
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def read_json(file_path):
|
| 8 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 9 |
+
data = json.load(file)
|
| 10 |
+
return data
|
| 11 |
+
|
| 12 |
+
def write_json(file_path, data):
|
| 13 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 14 |
+
json.dump(data, file, ensure_ascii=False, indent=4)
|
| 15 |
+
|
| 16 |
+
data = read_json("DataSet/train_samples_all_tuning.json")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 20 |
+
|
| 21 |
+
model_name = "Model/QwQ-32B-Preview"
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_name,
|
| 24 |
+
torch_dtype="auto",
|
| 25 |
+
device_map="auto"
|
| 26 |
+
)
|
| 27 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def chat_QwQ(prompt):
|
| 32 |
+
|
| 33 |
+
messages = [
|
| 34 |
+
{"role": "system", "content": "You are a helpful and harmless assistant. You are Qwen developed by Alibaba. You should think step-by-step."},
|
| 35 |
+
{"role": "user", "content": prompt}
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
text = tokenizer.apply_chat_template(
|
| 39 |
+
messages,
|
| 40 |
+
tokenize=False,
|
| 41 |
+
add_generation_prompt=True
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 45 |
+
|
| 46 |
+
generated_ids = model.generate(
|
| 47 |
+
**model_inputs,
|
| 48 |
+
max_new_tokens=512
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
generated_ids = [
|
| 52 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 56 |
+
# print(response)
|
| 57 |
+
return response
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# from transformers import MarianMTModel, MarianTokenizer
|
| 63 |
+
|
| 64 |
+
# model_name = "Model/opus-mt-en-zh"
|
| 65 |
+
# tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 66 |
+
# model = MarianMTModel.from_pretrained(model_name)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
for i in data:
|
| 73 |
+
sent1 = i['conversations'][0]['value']
|
| 74 |
+
sent2 = i['conversations'][1]['value']
|
| 75 |
+
sentence = sent1 + sent2
|
| 76 |
+
prompt = "This is a question-answering datapoint based on image information. Determine whether the answer can be judged without relying on the image. If it can, this is considered bad data; if it requires the image, it is considered good data. Rate this datapoint on a scale from 1 (bad) to 5 (good). ###### " + sentence
|
| 77 |
+
answer = chat_QwQ(prompt)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# english_text = answer
|
| 81 |
+
# inputs = tokenizer.encode(english_text, return_tensors="pt", truncation=True)
|
| 82 |
+
# translated = model.generate(inputs, max_length=40, num_beams=4, early_stopping=True)
|
| 83 |
+
# chinese_translation = tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 84 |
+
pprint.pprint(prompt)
|
| 85 |
+
pprint.pprint(answer)
|
| 86 |
+
break
|