Sai Sandesh Reddy commited on
Commit
73ce021
·
verified ·
1 Parent(s): 889cf4d

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +109 -0
README.md ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags: []
4
+ ---
5
+
6
+ # Model Card for Model ID
7
+
8
+ <!-- Provide a quick summary of what the model is/does. -->
9
+ ## Model: MathTutor RL version (Lambda = 1.0) (no Think) (HARD)
10
+
11
+ ## Usage:
12
+
13
+ ```
14
+ import torch
15
+ import json
16
+ from huggingface_hub import hf_hub_download
17
+ from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig
18
+
19
+ # --- Configuration ---
20
+ model_weights_id = "Sandesh-Zenteiq/MathTutor-7B-H_v0.1"
21
+ tokenizer_id = "Qwen/Qwen2.5-7B-Instruct"
22
+ device = "cuda" if torch.cuda.is_available() else "cpu"
23
+
24
+ print(f"Loading weights from: {model_weights_id}")
25
+ print(f"Loading tokenizer from: {tokenizer_id}")
26
+ print(f"Using device: {device}")
27
+
28
+ # --- Loading Logic ---
29
+ print("\nLoading model config...")
30
+ config = AutoConfig.from_pretrained(model_weights_id, trust_remote_code=True)
31
+
32
+ print("\nLoading tokenizer...")
33
+ tokenizer = AutoTokenizer.from_pretrained(tokenizer_id, trust_remote_code=True)
34
+
35
+ print("Loading model weights...")
36
+ model = AutoModelForCausalLM.from_pretrained(
37
+ model_weights_id,
38
+ config=config,
39
+ torch_dtype=torch.bfloat16,
40
+ device_map="auto",
41
+ trust_remote_code=True
42
+ )
43
+ print("Model loaded successfully!")
44
+
45
+ # --- Interactive Socratic Chat Loop ---
46
+ conversation_history = [
47
+ {"role": "system", "content": "You are a Socratic teacher. Guide the student to solve the problem by asking heuristic questions. Do not give direct answers or calculations. Ask one question at a time."},
48
+ {"role": "user", "content": "YOUR QUESTION HERE"}
49
+
50
+ ]
51
+
52
+ print("\n--- Starting Interactive Socratic Session ---")
53
+ print("You are the student. The model is the teacher.")
54
+ print("Type 'quit' or 'exit' to end the conversation.\n")
55
+
56
+ # Generate the very first response from the teacher
57
+ prompt_parts = []
58
+ for message in conversation_history:
59
+ prompt_parts.append(f"<|im_start|>{message['role']}\n{message['content']}<|im_end|>")
60
+ # Signal to the model that it's its turn to generate
61
+ prompt_parts.append("<|im_start|>assistant")
62
+ manual_prompt = "\n".join(prompt_parts)
63
+
64
+ inputs = tokenizer(manual_prompt, return_tensors="pt").to(model.device)
65
+ outputs = model.generate(**inputs, max_new_tokens=1000, temperature=0.7, do_sample=True)
66
+ initial_response = tokenizer.decode(outputs[0], skip_special_tokens=False)
67
+ # Extract only the assistant's part of the response
68
+ teacher_response_text = initial_response.split('<|im_start|>assistant')[1].replace('<|im_end|>', '').strip()
69
+
70
+ print(f"Teacher: {teacher_response_text}")
71
+ conversation_history.append({"role": "assistant", "content": teacher_response_text})
72
+
73
+
74
+ # Now start the interactive loop for back-and-forth
75
+ while True:
76
+ student_input = input("Student: ")
77
+ if student_input.lower() in ["quit", "exit"]:
78
+ print("--- Session Ended ---")
79
+ break
80
+
81
+ # Add the user's new message to the history
82
+ conversation_history.append({"role": "user", "content": student_input})
83
+
84
+ # --- Manually build the prompt with the FULL history ---
85
+ prompt_parts = []
86
+ for message in conversation_history:
87
+ prompt_parts.append(f"<|im_start|>{message['role']}\n{message['content']}<|im_end|>")
88
+ prompt_parts.append("<|im_start|>assistant")
89
+ manual_prompt = "\n".join(prompt_parts)
90
+
91
+ # Generate the next response based on the full history
92
+ inputs = tokenizer(manual_prompt, return_tensors="pt").to(model.device)
93
+ outputs = model.generate(**inputs, max_new_tokens=1000, temperature=0.7, do_sample=True)
94
+ full_generation = tokenizer.decode(outputs[0], skip_special_tokens=False)
95
+
96
+ # Cleanly extract only the *newest* assistant response
97
+ try:
98
+ new_response_part = full_generation.split(manual_prompt)[1]
99
+ teacher_response_text = new_response_part.replace('<|im_end|>', '').strip()
100
+ except IndexError:
101
+ # Fallback if splitting fails
102
+ teacher_response_text = "I'm sorry, I seem to have lost my train of thought. Could you please repeat your question?"
103
+
104
+
105
+ print(f"\nTeacher: {teacher_response_text}")
106
+
107
+ # Add the model's new response to the history for the next turn
108
+ conversation_history.append({"role": "assistant", "content": teacher_response_text})
109
+ ```