Views
No views yet
local_doctor-360Mfever, headache, fatiguePossible conditions: Viral infection, Flu1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4
5model_name = "Ahahajij182u2/local_doctor-360M"
6
7# Load model and tokenizer once
8tokenizer = AutoTokenizer.from_pretrained(model_name)
9model = AutoModelForCausalLM.from_pretrained(model_name)
10model.eval()
11
12def generate_diagnosis(symptoms: str, max_tokens: int = 300) -> str:
13 """Generate diagnosis for given symptoms."""
14 prompt = f"""<|im_start|>system
15You are a medical assistant AI.
16
17Rules:
18- Predict the most likely disease from the symptoms.
19- Give 3 to 5 basic precautions or first steps.
20- Use only this format:
21
22POSSIBLE DISEASE: ...
23
24POSSIBLE PRECAUTIONS: ...
25
26- If symptoms suggest an emergency, advise immediate medical help.
27- Do not give unusual or unsafe advice.
28<|im_end|>
29<|im_start|>user
30{symptoms}
31<|im_end|>
32<|im_start|>assistant
33"""
34
35 inputs = tokenizer(prompt, return_tensors="pt")
36
37 with torch.no_grad():
38 outputs = model.generate(
39 **inputs,
40 max_new_tokens=max_tokens,
41 temperature=0.1,
42 do_sample=True,
43 )
44
45 decoded = tokenizer.decode(
46 outputs[0][inputs["input_ids"].shape[1]:],
47 skip_special_tokens=False
48 )
49
50 return decoded.split("<|im_end|>")[0].strip()
51
52
53
54# Test cases
55test_symptoms = [
56 "chest pain, shortness of breath",
57 "diarrhea, vomiting, stomach pain",
58 "rash, itching, swelling",
59 "cough, fever, sore throat",
60]
61
62for symptoms in test_symptoms:
63 response = generate_diagnosis(symptoms)
64 print(f"\n{symptoms}\n→ {response}")
65 assert "POSSIBLE DISEASE" in response
66 assert "POSSIBLE PRECAUTIONS" in response