Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "socratesft/socrates-qwen2.5-14b-sft"
4tokenizer = AutoTokenizer.from_pretrained(model_name)
5model = AutoModelForCausalLM.from_pretrained(
6 model_name,
7 torch_dtype="auto",
8 device_map="auto"
9)
10
11# Example usage
12prompt_system = "You are simulating a survey respondent. Answer exactly as instructed, following the specified response format without additional commentary."
13prompt_user = """You are a survey respondent with the following demographic profile:
14- Age: 31
15- Gender: Male
16- Education: Post grad study/professional degree
17- Employment: Employed as paid employee
18- Marital Status: Living with partner
19- Housing Ownership: Rented for cash
20- Housing Type: A one-family house detached from any other house
21- Location: Kentucky
22- Metro Status: Metro Area
23- Income: 100-124K
24- Internet Access: Internet Household
25- Household Size: 2
26- Phone Service: Cellphone only
27
28Read the question below and answer exactly as this person would. Follow the response instructions precisely.
29
30You read “There is a new halfway house opening in your neighborhood where recently released felons will live. The director is letting neighbors select applicants to live at the house, and you have the choice between the following two candidates:” Candidate 1: Sex: Male; Crime: Nonviolent burglary; Education: Vocational training; Race: Latino; Age: 35 years old; Previous work: Seasonal/part-time employment; Job seeking: Going to temp agencies; Family status: Divorced, no children. Candidate 2: Sex: Male; Crime: Nonviolent burglary; Education: GED; Race: Black; Age: 22 years old; Previous work: Steady full-time employment; Job seeking: Submitting resumes; Family status: Divorced, no children. You were then asked: “If you had to choose between them, which of the two candidates should be admitted to the halfway house in your neighborhood?” Only return 1 to choose the first candidate; 2 to choose the second candidate, nothing else."""
31messages = [
32 {"role": "system", "content": prompt_system},
33 {"role": "user", "content": prompt_user}
34]
35text = tokenizer.apply_chat_template(
36 messages,
37 tokenize=False,
38 add_generation_prompt=True
39)
40inputs = tokenizer([text], return_tensors="pt").to(model.device)
41outputs = model.generate(**inputs, max_new_tokens=500)
42generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, outputs)]
43response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
44print(response)participant_mapping)