File size: 1,564 Bytes
b206615
20b90d7
b206615
20b90d7
b206615
20b90d7
b206615
20b90d7
b206615
 
 
 
 
 
20b90d7
b206615
 
 
 
 
 
 
 
20b90d7
b206615
 
 
 
 
 
 
20b90d7
b206615
 
 
 
 
 
20b90d7
b206615
 
20b90d7
b206615
 
 
 
 
 
20b90d7
b206615
 
20b90d7
b206615
20b90d7
 
b206615
 
20b90d7
b206615
 
 
 
 
 
20b90d7
b206615
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

model_name = "mistralai/Mistral-7B-Instruct-v0.1"

tokenizer = AutoTokenizer.from_pretrained(model_name)

model = AutoModelForCausalLM.from_pretrained(
    model_name,
    device_map="auto",
    load_in_4bit=True,
    torch_dtype=torch.float16
)

generator = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    max_new_tokens=200,
    temperature=0.3,
    repetition_penalty=1.1
)

def is_unsafe(query):
    blocked = [
        "dose", "dosage", "how much",
        "diagnose", "prescribe",
        "medicine for", "treatment", "cure"
    ]
    return any(word in query.lower() for word in blocked)

def health_chatbot(user_input):
    if is_unsafe(user_input):
        return (
            "I can’t provide diagnosis or medication instructions. "
            "Please consult a qualified healthcare professional."
        )

    prompt = f"""
You are a general health information assistant.

Rules:
- Do NOT diagnose diseases.
- Do NOT prescribe medicines or give dosages.
- Provide general causes, symptoms, and prevention only.
- Keep answers simple and clear.
- End with: 'If symptoms persist, consult a healthcare professional.'

Question:
{user_input}

Answer:
"""

    output = generator(prompt)[0]["generated_text"]
    return output.split("Answer:")[-1].strip()

demo = gr.Interface(
    fn=health_chatbot,
    inputs=gr.Textbox(lines=2),
    outputs="text",
    title="🩺 General Health Query Chatbot"
)

demo.launch()