Spaces:
Paused
Paused
Commit
·
ceaba60
1
Parent(s):
474ac56
fixed output formatting for model
Browse files
app.py
CHANGED
@@ -65,7 +65,6 @@ print(
|
|
65 |
# === Role Agent with instruction/input/output format ===
|
66 |
class RoleAgent:
|
67 |
def __init__(self, role_instruction, tokenizer, model):
|
68 |
-
|
69 |
self.tokenizer = tokenizer
|
70 |
self.model = model
|
71 |
self.role_instruction = role_instruction
|
@@ -81,36 +80,97 @@ class RoleAgent:
|
|
81 |
|
82 |
outputs = self.model.generate(
|
83 |
**inputs,
|
84 |
-
max_new_tokens=
|
85 |
do_sample=True,
|
86 |
temperature=0.7,
|
87 |
pad_token_id=self.tokenizer.eos_token_id,
|
88 |
)
|
89 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
if
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
else:
|
97 |
-
# Fallback:
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
print(
|
105 |
-
"[
|
106 |
-
response,
|
107 |
-
"→",
|
108 |
-
"[THINKING] thinking:",
|
109 |
-
thinking,
|
110 |
-
"[ANSWER] answer:",
|
111 |
-
answer,
|
112 |
)
|
113 |
|
|
|
114 |
return {"thinking": thinking, "output": answer}
|
115 |
|
116 |
|
|
|
65 |
# === Role Agent with instruction/input/output format ===
|
66 |
class RoleAgent:
|
67 |
def __init__(self, role_instruction, tokenizer, model):
|
|
|
68 |
self.tokenizer = tokenizer
|
69 |
self.model = model
|
70 |
self.role_instruction = role_instruction
|
|
|
80 |
|
81 |
outputs = self.model.generate(
|
82 |
**inputs,
|
83 |
+
max_new_tokens=128,
|
84 |
do_sample=True,
|
85 |
temperature=0.7,
|
86 |
pad_token_id=self.tokenizer.eos_token_id,
|
87 |
)
|
88 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
89 |
|
90 |
+
# Extract only the new generated content after the prompt
|
91 |
+
prompt_length = len(prompt)
|
92 |
+
if len(response) > prompt_length:
|
93 |
+
generated_text = response[prompt_length:].strip()
|
94 |
+
else:
|
95 |
+
generated_text = response.strip()
|
96 |
+
|
97 |
+
# Clean up the response - remove any repeated instruction/input/output patterns
|
98 |
+
lines = generated_text.split("\n")
|
99 |
+
clean_lines = []
|
100 |
+
|
101 |
+
for line in lines:
|
102 |
+
line = line.strip()
|
103 |
+
# Skip lines that look like instruction formatting
|
104 |
+
if (
|
105 |
+
line.startswith("instruction:")
|
106 |
+
or line.startswith("input:")
|
107 |
+
or line.startswith("output:")
|
108 |
+
or line == ""
|
109 |
+
):
|
110 |
+
continue
|
111 |
+
clean_lines.append(line)
|
112 |
+
|
113 |
+
# Join the clean lines and take the first substantial response
|
114 |
+
if clean_lines:
|
115 |
+
answer = clean_lines[0]
|
116 |
+
# If there are multiple clean lines, take the first one that's substantial
|
117 |
+
for line in clean_lines:
|
118 |
+
if len(line) > 20: # Arbitrary threshold for substantial content
|
119 |
+
answer = line
|
120 |
+
break
|
121 |
else:
|
122 |
+
# Fallback: try to extract after "output:" if present
|
123 |
+
if "output:" in generated_text.lower():
|
124 |
+
parts = generated_text.lower().split("output:")
|
125 |
+
if len(parts) > 1:
|
126 |
+
answer = parts[-1].strip()
|
127 |
+
else:
|
128 |
+
answer = generated_text
|
129 |
+
else:
|
130 |
+
answer = generated_text
|
131 |
+
|
132 |
+
# Additional cleanup - remove any remaining instruction artifacts
|
133 |
+
answer = (
|
134 |
+
answer.replace("instruction:", "")
|
135 |
+
.replace("input:", "")
|
136 |
+
.replace("output:", "")
|
137 |
+
.strip()
|
138 |
+
)
|
139 |
+
|
140 |
+
# If answer is still messy, try to extract the actual medical content
|
141 |
+
if "patient" in answer.lower() and len(answer) > 100:
|
142 |
+
# Look for sentences that contain medical information
|
143 |
+
sentences = answer.split(".")
|
144 |
+
medical_sentences = []
|
145 |
+
for sentence in sentences:
|
146 |
+
sentence = sentence.strip()
|
147 |
+
if len(sentence) > 10 and any(
|
148 |
+
word in sentence.lower()
|
149 |
+
for word in [
|
150 |
+
"patient",
|
151 |
+
"pain",
|
152 |
+
"symptom",
|
153 |
+
"diagnosis",
|
154 |
+
"treatment",
|
155 |
+
"knee",
|
156 |
+
"reports",
|
157 |
+
"experiencing",
|
158 |
+
]
|
159 |
+
):
|
160 |
+
medical_sentences.append(sentence)
|
161 |
+
|
162 |
+
if medical_sentences:
|
163 |
+
answer = ". ".join(
|
164 |
+
medical_sentences[:2]
|
165 |
+
) # Take first 2 medical sentences
|
166 |
+
if not answer.endswith("."):
|
167 |
+
answer += "."
|
168 |
+
|
169 |
print(
|
170 |
+
f"[CLEANED RESPONSE] Original length: {len(response)}, Cleaned: '{answer}'"
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
)
|
172 |
|
173 |
+
thinking = "" # For now, we'll focus on getting clean answers
|
174 |
return {"thinking": thinking, "output": answer}
|
175 |
|
176 |
|