Spaces:
Runtime error
Runtime error
Commit
·
0d2640e
1
Parent(s):
10c6208
minor changes.
Browse files
app.py
CHANGED
|
@@ -37,31 +37,29 @@ def retrieve_top_k(query, k=5):
|
|
| 37 |
return results
|
| 38 |
|
| 39 |
def build_prompt(query, retrieved_docs):
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
])
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
Patient Query:
|
| 48 |
-
{query}
|
| 49 |
|
| 50 |
Clinical Context:
|
| 51 |
-
{
|
| 52 |
|
| 53 |
-
|
| 54 |
return prompt
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
output = generation_model.generate(
|
| 59 |
input_ids=input_ids,
|
| 60 |
max_new_tokens=max_new_tokens,
|
| 61 |
-
temperature=0.
|
| 62 |
-
do_sample=True,
|
| 63 |
top_k=50,
|
| 64 |
top_p=0.95,
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 67 |
return decoded.strip()
|
|
|
|
| 37 |
return results
|
| 38 |
|
| 39 |
def build_prompt(query, retrieved_docs):
|
| 40 |
+
context = "\n".join([f"- {row['text']}" for _, row in retrieved_docs.iterrows()])
|
| 41 |
+
prompt = f"""Instruction: Based on the following patient symptoms and clinical notes, provide a detailed diagnostic reasoning or suggestion.
|
|
|
|
| 42 |
|
| 43 |
+
Input:
|
| 44 |
+
Patient Query: {query}
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
Clinical Context:
|
| 47 |
+
{context}
|
| 48 |
|
| 49 |
+
Response:"""
|
| 50 |
return prompt
|
| 51 |
|
| 52 |
+
|
| 53 |
+
def generate_local_answer(prompt, max_new_tokens=512):
|
| 54 |
+
input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids # CPU
|
| 55 |
output = generation_model.generate(
|
| 56 |
input_ids=input_ids,
|
| 57 |
max_new_tokens=max_new_tokens,
|
| 58 |
+
temperature=0.4, # Less random
|
|
|
|
| 59 |
top_k=50,
|
| 60 |
top_p=0.95,
|
| 61 |
+
repetition_penalty=1.2, # Encourage more varied output
|
| 62 |
+
num_beams=2 # Mild beam search for quality
|
| 63 |
)
|
| 64 |
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 65 |
return decoded.strip()
|