mirzayasirabdullahbaig07 commited on
Commit
428d1fc
·
verified ·
1 Parent(s): 23ba1ec

File Added

Browse files

This is the main code file

Files changed (1) hide show
  1. app.py +185 -0
app.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ model = None
6
+ tokenizer = None
7
+
8
+ def load_model():
9
+ global model, tokenizer
10
+ if model is None:
11
+ print("[MediAssist] Loading...")
12
+ model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
13
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ model_id, torch_dtype=torch.float32, low_cpu_mem_usage=True
16
+ )
17
+ model.eval()
18
+ print("[MediAssist] Ready!")
19
+
20
+ # For Urdu/Hindi we translate the question to English, get answer, keep it simple
21
+ # TinyLlama only works well in English — this is honest and works
22
+ SYSTEM_EN = """You are MediAssist, a medical assistant for rural Pakistan communities.
23
+ Answer clearly in English with this exact format:
24
+ 🔍 What this might be:
25
+ - cause 1
26
+ - cause 2
27
+ 🏠 Home care steps:
28
+ - step 1
29
+ - step 2
30
+ - step 3
31
+ 🚨 Go to doctor immediately if:
32
+ - warning 1
33
+ - warning 2
34
+ Keep under 120 words. Never diagnose. Always suggest seeing a doctor for serious issues."""
35
+
36
+ SYSTEM_UR = """You are a medical assistant. The user is asking in Urdu.
37
+ First translate their question to English, answer in English with this format:
38
+ 🔍 What this might be:
39
+ - cause
40
+ 🏠 Home care:
41
+ - step 1
42
+ - step 2
43
+ 🚨 See doctor if:
44
+ - warning
45
+ Then write: "اردو خلاصہ:" and give a 2-line Urdu summary of your answer.
46
+ Keep total response under 150 words."""
47
+
48
+ SYSTEM_HI = """You are a medical assistant. Answer in simple Hindi with this format:
49
+ 🔍 यह क्या हो सकता है:
50
+ - कारण
51
+ 🏠 घर पर करें:
52
+ - कदम 1
53
+ - कदम 2
54
+ 🚨 डॉक्टर के पास जाएं अगर:
55
+ - चेतावनी
56
+ 100 शब्दों से कम रखें।"""
57
+
58
+ SYSTEMS = {
59
+ "English": SYSTEM_EN,
60
+ "اردو": SYSTEM_UR,
61
+ "हिन्दी": SYSTEM_HI,
62
+ }
63
+
64
+ DISCLAIMERS = {
65
+ "English": "\n\n⚠️ *For informational purposes only. Always consult a real doctor.*",
66
+ "اردو": "\n\n⚠️ *صرف معلوماتی مقاصد کے لیے۔ ہمیشہ ڈاکٹر سے مشورہ کریں۔*",
67
+ "हिन्दी": "\n\n⚠️ *केवल जानकारी के लिए। हमेशा डॉक्टर से सलाह लें।*",
68
+ }
69
+
70
+ def respond(message, history, language):
71
+ load_model()
72
+ system = SYSTEMS.get(language, SYSTEM_EN)
73
+ chat = [{"role": "system", "content": system}]
74
+ for user_msg, bot_msg in history:
75
+ if user_msg: chat.append({"role": "user", "content": user_msg})
76
+ if bot_msg: chat.append({"role": "assistant", "content": bot_msg})
77
+ chat.append({"role": "user", "content": message})
78
+
79
+ prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
80
+ inputs = tokenizer(prompt, return_tensors="pt")
81
+ input_len = inputs["input_ids"].shape[1]
82
+
83
+ with torch.no_grad():
84
+ outputs = model.generate(
85
+ **inputs,
86
+ max_new_tokens=300,
87
+ do_sample=False,
88
+ repetition_penalty=1.3,
89
+ pad_token_id=tokenizer.eos_token_id,
90
+ )
91
+
92
+ reply = tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True).strip()
93
+ return reply + DISCLAIMERS.get(language, DISCLAIMERS["English"])
94
+
95
+ CSS = """
96
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
97
+ * { font-family: 'Inter', sans-serif !important; }
98
+ body, .gradio-container { background: #0a0f0d !important; }
99
+ footer { display: none !important; }
100
+ .gradio-container { max-width: 860px !important; margin: 0 auto !important; padding: 24px 16px !important; }
101
+ .mh {
102
+ background: linear-gradient(135deg, #064e3b, #047857);
103
+ border: 1px solid #10b981; border-radius: 20px; padding: 28px 32px; margin-bottom: 12px;
104
+ }
105
+ .hb { display: flex; align-items: center; gap: 14px; }
106
+ .hi {
107
+ width: 52px; height: 52px; background: rgba(16,185,129,0.2);
108
+ border: 1.5px solid rgba(16,185,129,0.5); border-radius: 14px;
109
+ display: flex; align-items: center; justify-content: center; font-size: 24px; flex-shrink:0;
110
+ }
111
+ .ht { color: #ecfdf5; font-size: 26px; font-weight: 700; margin: 0; }
112
+ .hs { color: #6ee7b7; font-size: 13px; margin: 3px 0 0; }
113
+ .hbadges { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
114
+ .badge {
115
+ background: rgba(16,185,129,0.15); border: 1px solid rgba(16,185,129,0.35);
116
+ color: #6ee7b7; font-size: 12px; font-weight: 500; padding: 5px 12px; border-radius: 20px;
117
+ }
118
+ .lr {
119
+ background: #111816; border: 1px solid #1f2e28; border-radius: 14px;
120
+ padding: 14px 20px; margin-bottom: 8px; display: flex; align-items: center; gap: 12px;
121
+ }
122
+ .ll { color: #6ee7b7; font-size: 13px; font-weight: 500; white-space: nowrap; }
123
+ .db {
124
+ background: #1a1500; border: 1px solid #78350f; border-left: 3px solid #f59e0b;
125
+ border-radius: 10px; padding: 12px 16px; margin-top: 8px;
126
+ font-size: 12px; color: #fbbf24; line-height: 1.6;
127
+ }
128
+ """
129
+
130
+ with gr.Blocks(title="MediAssist") as demo:
131
+
132
+ gr.HTML("""
133
+ <div class="mh">
134
+ <div class="hb">
135
+ <div class="hi">🏥</div>
136
+ <div>
137
+ <p class="ht">MediAssist</p>
138
+ <p class="hs">AI-powered health guidance for underserved communities</p>
139
+ </div>
140
+ </div>
141
+ <div class="hbadges">
142
+ <span class="badge">🧠 TinyLlama-1.1B</span>
143
+ <span class="badge">🆓 100% Free</span>
144
+ <span class="badge">🌍 3 Languages</span>
145
+ <span class="badge">🇵🇰 Built for Rural Punjab</span>
146
+ <span class="badge">✅ &lt;32B params</span>
147
+ </div>
148
+ </div>
149
+ """)
150
+
151
+ with gr.Row(elem_classes=["lr"]):
152
+ gr.HTML('<span class="ll">🌐 Language / زبان / भाषा</span>')
153
+ language = gr.Radio(
154
+ choices=["English", "اردو", "हिन्दी"],
155
+ value="English", show_label=False,
156
+ )
157
+
158
+ gr.ChatInterface(
159
+ fn=respond,
160
+ additional_inputs=[language],
161
+ chatbot=gr.Chatbot(height=440, show_label=False),
162
+ textbox=gr.Textbox(
163
+ placeholder="Describe your symptoms or ask a health question…",
164
+ show_label=False, lines=2,
165
+ ),
166
+ examples=[
167
+ ["I have fever 38.5°C and headache for 2 days", "English"],
168
+ ["Paracetamol dose for a 5 year old child?", "English"],
169
+ ["Signs of dehydration in children?", "English"],
170
+ ["How to treat a minor burn at home?", "English"],
171
+ ["مجھے بخار اور سر درد ہے", "اردو"],
172
+ ["بچے کو پیراسیٹامول کتنی دیں؟", "اردو"],
173
+ ],
174
+ title="", description="",
175
+ )
176
+
177
+ gr.HTML("""
178
+ <div class="db">
179
+ ⚠️ <strong>Medical Disclaimer:</strong> MediAssist provides general health information only.
180
+ Always consult a qualified healthcare provider. In emergencies, call your local emergency number.
181
+ </div>
182
+ """)
183
+
184
+ if __name__ == "__main__":
185
+ demo.launch(css=CSS)