drvikasgaur commited on
Commit
c9255a6
·
verified ·
1 Parent(s): d719d42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -54
app.py CHANGED
@@ -246,6 +246,8 @@ questions = [
246
  ("Have these difficulties been present since childhood and continue to affect your work or relationships?", "क्या ये कठिनाइयाँ बचपन से रही हैं और अब भी आपके काम या रिश्तों को प्रभावित कर रही हैं?"),
247
  ]
248
 
 
 
249
  # ---- STATE ----
250
  state = {
251
  "index": 0,
@@ -253,6 +255,7 @@ state = {
253
  }
254
 
255
  # ---- FUNCTIONS ----
 
256
  def render_question():
257
  idx = state["index"]
258
  total = len(questions)
@@ -267,15 +270,6 @@ def render_question():
267
  gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
268
  )
269
 
270
- def format_responses(questions, responses):
271
- report = []
272
- for idx, ((en, hi), answer) in enumerate(zip(questions, responses), 1):
273
- if answer.lower() == "yes":
274
- report.append(f"{idx}. {en}\nAnswer: Yes\n")
275
- if not report:
276
- return "No significant symptoms reported."
277
- return "\n".join(report)
278
-
279
  def next_step(response):
280
  state["responses"][state["index"]] = response
281
  state["index"] += 1
@@ -284,7 +278,7 @@ def next_step(response):
284
  return render_question()
285
  else:
286
  return (
287
- gr.update(value="✅ All questions completed. Click 'Submit for AI Analysis'."),
288
  gr.update(value=""),
289
  gr.update(visible=False), gr.update(visible=False),
290
  gr.update(visible=True),
@@ -292,37 +286,43 @@ def next_step(response):
292
  gr.update(visible=False)
293
  )
294
 
 
 
 
 
 
 
 
 
 
295
  def run_final_analysis():
296
  yield (
297
  gr.update(value="🧠 AI is analyzing your responses... Please wait. ⏳"),
298
  gr.update(value=""),
299
- gr.update(visible=False),
300
- gr.update(visible=False),
301
- gr.update(visible=False),
302
- gr.update(visible=False),
303
- gr.update(visible=False)
304
  )
305
 
306
- time.sleep(2)
 
 
307
 
308
- llm_input = format_responses(questions, state["responses"])
309
  prompt = (
310
- "You are a clinical assistant AI evaluating a user's mental health screening responses.\n"
311
- "Analyze only the 'Yes' answers provided below. Group the symptoms into clusters like Anxiety, Depression, Psychosis, Sleep Disorders, or Substance Use.\n"
312
- "Summarize the symptoms clearly, give a preliminary impression, and suggest recommended action.\n"
313
- "Respond in clear English and Hindi.\n\n"
314
- f"{llm_input}"
 
 
315
  )
316
- output = llm(prompt, max_new_tokens=500, temperature=0.3)
 
317
  ai_result = output[0]["generated_text"]
318
 
319
  yield (
320
  gr.update(value="✅ AI Analysis Completed."),
321
  gr.update(value=""),
322
- gr.update(visible=False),
323
- gr.update(visible=False),
324
- gr.update(visible=False),
325
- gr.update(visible=False),
326
  gr.update(value=ai_result, visible=True)
327
  )
328
 
@@ -340,7 +340,9 @@ def start_app():
340
  with gr.Blocks(theme=gr.themes.Soft()) as app:
341
  gr.Markdown("## 🧠 MindScreen: Mental Health Self-Screening")
342
  gr.Markdown("### मानसिक स्वास्थ्य आत्म-स्क्रीनिंग प्रश्नावली")
343
- gr.Markdown("**Note: Please choose 'Yes' only if symptoms interfere with your daily life.**")
 
 
344
 
345
  progress_bar = gr.Markdown("")
346
  question_display = gr.Markdown("", elem_id="question-box")
@@ -357,36 +359,17 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
357
  result_box = gr.Textbox(
358
  label="🧠 AI Interpretation (English + Hindi)",
359
  visible=False,
360
- lines=18,
361
  max_lines=25,
362
  show_copy_button=True,
363
  interactive=False
364
  )
365
 
366
- app.load(start_app, outputs=[
367
- question_display, progress_bar, yes_btn, no_btn, back_btn,
368
- result_btn, result_box
369
- ])
370
-
371
- yes_btn.click(lambda: next_step("Yes"), outputs=[
372
- question_display, progress_bar, yes_btn, no_btn, back_btn,
373
- result_btn, result_box
374
- ])
375
- no_btn.click(lambda: next_step("No"), outputs=[
376
- question_display, progress_bar, yes_btn, no_btn, back_btn,
377
- result_btn, result_box
378
- ])
379
- back_btn.click(go_back, outputs=[
380
- question_display, progress_bar, yes_btn, no_btn, back_btn,
381
- result_btn, result_box
382
- ])
383
- result_btn.click(
384
- run_final_analysis,
385
- outputs=[
386
- question_display, progress_bar, yes_btn, no_btn, back_btn,
387
- result_btn, result_box
388
- ],
389
- concurrency_limit=1
390
- )
391
 
392
  app.launch()
 
246
  ("Have these difficulties been present since childhood and continue to affect your work or relationships?", "क्या ये कठिनाइयाँ बचपन से रही हैं और अब भी आपके काम या रिश्तों को प्रभावित कर रही हैं?"),
247
  ]
248
 
249
+
250
+
251
  # ---- STATE ----
252
  state = {
253
  "index": 0,
 
255
  }
256
 
257
  # ---- FUNCTIONS ----
258
+
259
  def render_question():
260
  idx = state["index"]
261
  total = len(questions)
 
270
  gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
271
  )
272
 
 
 
 
 
 
 
 
 
 
273
  def next_step(response):
274
  state["responses"][state["index"]] = response
275
  state["index"] += 1
 
278
  return render_question()
279
  else:
280
  return (
281
+ gr.update(value="✅ All questions completed. Click 'Submit for AI Analysis'."),
282
  gr.update(value=""),
283
  gr.update(visible=False), gr.update(visible=False),
284
  gr.update(visible=True),
 
286
  gr.update(visible=False)
287
  )
288
 
289
+ def format_yes_responses():
290
+ yes_topics = []
291
+ for (en, _), ans in zip(questions, state["responses"]):
292
+ if ans.lower() == "yes":
293
+ yes_topics.append(en)
294
+ if not yes_topics:
295
+ return "No significant symptoms reported."
296
+ return "\n".join(yes_topics)
297
+
298
  def run_final_analysis():
299
  yield (
300
  gr.update(value="🧠 AI is analyzing your responses... Please wait. ⏳"),
301
  gr.update(value=""),
302
+ *[gr.update(visible=False) for _ in range(5)]
 
 
 
 
303
  )
304
 
305
+ time.sleep(1)
306
+
307
+ yes_summary = format_yes_responses()
308
 
 
309
  prompt = (
310
+ "You are a psychiatric specialist clinical assistant AI evaluating a user's mental health screening.\n"
311
+ "Summarize the user's symptom clusters (like Anxiety, Depression, Psychosis, etc.) based ONLY on these symptoms:\n"
312
+ f"{yes_summary}\n\n"
313
+ "Do NOT list individual questions.\n"
314
+ "Give a brief, professional clinical impression.\n"
315
+ "Suggest recommended next steps.\n"
316
+ "Finally, provide a Hindi translation of your full response.\n"
317
  )
318
+
319
+ output = llm(prompt, max_new_tokens=1500, temperature=0.1, do_sample=False)
320
  ai_result = output[0]["generated_text"]
321
 
322
  yield (
323
  gr.update(value="✅ AI Analysis Completed."),
324
  gr.update(value=""),
325
+ *[gr.update(visible=False) for _ in range(5)],
 
 
 
326
  gr.update(value=ai_result, visible=True)
327
  )
328
 
 
340
  with gr.Blocks(theme=gr.themes.Soft()) as app:
341
  gr.Markdown("## 🧠 MindScreen: Mental Health Self-Screening")
342
  gr.Markdown("### मानसिक स्वास्थ्य आत्म-स्क्रीनिंग प्रश्नावली")
343
+ gr.Markdown("**Note: Please choose 'Yes' only if the symptoms cause distress to you or your family, or if they interfere with your day-to-day functioning.**\n\n"
344
+ "**नोट: कृपया 'हाँ' तभी चुनें जब ये लक्षण आपके या आपके परिवार के लिए परेशानी का कारण बन रहे हों, या आपकी दिनचर्या को प्रभावित करते हों।**")
345
+
346
 
347
  progress_bar = gr.Markdown("")
348
  question_display = gr.Markdown("", elem_id="question-box")
 
359
  result_box = gr.Textbox(
360
  label="🧠 AI Interpretation (English + Hindi)",
361
  visible=False,
362
+ lines=20,
363
  max_lines=25,
364
  show_copy_button=True,
365
  interactive=False
366
  )
367
 
368
+ app.load(start_app, outputs=[question_display, progress_bar, yes_btn, no_btn, back_btn, result_btn, result_box])
369
+
370
+ yes_btn.click(lambda: next_step("Yes"), outputs=[question_display, progress_bar, yes_btn, no_btn, back_btn, result_btn, result_box])
371
+ no_btn.click(lambda: next_step("No"), outputs=[question_display, progress_bar, yes_btn, no_btn, back_btn, result_btn, result_box])
372
+ back_btn.click(go_back, outputs=[question_display, progress_bar, yes_btn, no_btn, back_btn, result_btn, result_box])
373
+ result_btn.click(run_final_analysis, outputs=[question_display, progress_bar, yes_btn, no_btn, back_btn, result_btn, result_box], concurrency_limit=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
 
375
  app.launch()