Jaamie commited on
Commit
57f2dd9
Β·
verified Β·
1 Parent(s): ec6aa13

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -14
app.py CHANGED
@@ -27,6 +27,7 @@ from fpdf import FPDF
27
  import uuid
28
  import textwrap
29
  from dotenv import load_dotenv
 
30
  try:
31
  import whisper
32
  except ImportError:
@@ -39,10 +40,11 @@ whisper_model = whisper.load_model("base")
39
  load_dotenv()
40
 
41
  hf_token = os.getenv("HF_TOKEN")
42
- resend_api_key = os.getenv("RESEND_API_KEY")
43
 
44
  login(token=hf_token)
45
 
 
46
  # Mount Google Drive
47
  #drive.mount('/content/drive')
48
 
@@ -235,11 +237,13 @@ def process_query(user_query, input_type="text"):
235
  1. **Diagnosed Mental Disorder**
236
  2. **Detected emotion** {emotion}
237
  3. **Intensity of emotion** {value}
238
- 3. **Matching Symptoms**
239
- 4. **Personalized Treatment**
240
  5. **Helpline Numbers**
241
  6. **Source Link** (if applicable)
242
 
 
 
243
  If a disorder cannot be determined, return **Diagnosed Mental Disorder** as "Unknown".
244
 
245
  ---
@@ -253,11 +257,13 @@ def process_query(user_query, input_type="text"):
253
  RAG_PROMPT_TEMPLATE = tokenizer.apply_chat_template(
254
  prompt_in_chat_format, tokenize=False, add_generation_prompt=True
255
  )
 
256
 
257
  # Generate response
258
  #answer = READER_LLM(RAG_PROMPT_TEMPLATE)[0]["generated_text"]
259
  try:
260
  response = READER_LLM(RAG_PROMPT_TEMPLATE)
 
261
  answer = response[0]["generated_text"] if response and "generated_text" in response[0] else "⚠️ No output generated."
262
  except Exception as e:
263
  print("❌ Error during generation:", e)
@@ -330,6 +336,8 @@ def show_logs():
330
 
331
  def create_summary_pdf(text, filename_prefix="diagnosis_report"):
332
  try:
 
 
333
  pdf = FPDF()
334
  pdf.add_page()
335
  pdf.set_font("Arial", style='B', size=14)
@@ -341,12 +349,9 @@ def create_summary_pdf(text, filename_prefix="diagnosis_report"):
341
  for line in wrapped:
342
  pdf.cell(200, 10, txt=line, ln=True)
343
 
344
- # Save to /tmp instead of root dir
345
- filename = f"/tmp/{filename_prefix}_{uuid.uuid4().hex[:6]}.pdf"
346
- pdf.output(filename)
347
-
348
- print(f"βœ… PDF created at: {filename}")
349
- return filename
350
  except Exception as e:
351
  print(f"❌ Error creating PDF: {e}")
352
  return None
@@ -403,12 +408,16 @@ def unified_handler(audio, text):
403
  response, download_path = process_query(text, input_type="text")
404
 
405
  # Ensure download path is valid
406
- if not (download_path and os.path.exists(download_path)):
 
 
 
 
 
 
407
  print("❌ PDF not found or failed to generate.")
408
  return response, None
409
-
410
- return response, download_path
411
-
412
  #Agentic Framework from HF spaces
413
  # agent_iframe = gr.HTML(
414
  # '<iframe src="https://jaamie-mental-health-agent.hf.space" width="100%" height="700px" style="border:none;"></iframe>'
@@ -482,7 +491,10 @@ feedback_tab = gr.Interface(
482
  gr.Textbox(label="Confidence Score"),
483
  gr.Textbox(label="Status")
484
  ],
485
- outputs=gr.Textbox(label="πŸ“¬ Submission Status"),
 
 
 
486
  title="πŸ“ Submit Feedback With Session Metadata"
487
  )
488
 
 
27
  import uuid
28
  import textwrap
29
  from dotenv import load_dotenv
30
+ import shutil
31
  try:
32
  import whisper
33
  except ImportError:
 
40
  load_dotenv()
41
 
42
  hf_token = os.getenv("HF_TOKEN")
43
+ # resend_api_key = os.getenv("RESEND_API_KEY")
44
 
45
  login(token=hf_token)
46
 
47
+
48
  # Mount Google Drive
49
  #drive.mount('/content/drive')
50
 
 
237
  1. **Diagnosed Mental Disorder**
238
  2. **Detected emotion** {emotion}
239
  3. **Intensity of emotion** {value}
240
+ 3. **Matching Symptoms** from the context
241
+ 4. **Personalized Treatment**
242
  5. **Helpline Numbers**
243
  6. **Source Link** (if applicable)
244
 
245
+ Make sure to provide a comprehensive and accurate diagnosis and explain the personalised treatment in detail.
246
+
247
  If a disorder cannot be determined, return **Diagnosed Mental Disorder** as "Unknown".
248
 
249
  ---
 
257
  RAG_PROMPT_TEMPLATE = tokenizer.apply_chat_template(
258
  prompt_in_chat_format, tokenize=False, add_generation_prompt=True
259
  )
260
+
261
 
262
  # Generate response
263
  #answer = READER_LLM(RAG_PROMPT_TEMPLATE)[0]["generated_text"]
264
  try:
265
  response = READER_LLM(RAG_PROMPT_TEMPLATE)
266
+ # print("πŸ” Raw LLM output:", response)
267
  answer = response[0]["generated_text"] if response and "generated_text" in response[0] else "⚠️ No output generated."
268
  except Exception as e:
269
  print("❌ Error during generation:", e)
 
336
 
337
  def create_summary_pdf(text, filename_prefix="diagnosis_report"):
338
  try:
339
+ filename = f"{filename_prefix}_{uuid.uuid4().hex[:6]}.pdf"
340
+ filepath = os.path.join(".", filename) # Save in current directory
341
  pdf = FPDF()
342
  pdf.add_page()
343
  pdf.set_font("Arial", style='B', size=14)
 
349
  for line in wrapped:
350
  pdf.cell(200, 10, txt=line, ln=True)
351
 
352
+ pdf.output(filepath)
353
+ print(f"βœ… PDF created at: {filepath}")
354
+ return filepath
 
 
 
355
  except Exception as e:
356
  print(f"❌ Error creating PDF: {e}")
357
  return None
 
408
  response, download_path = process_query(text, input_type="text")
409
 
410
  # Ensure download path is valid
411
+ # if not (download_path and os.path.exists(download_path)):
412
+ # print("❌ PDF not found or failed to generate.")
413
+ # return response, None
414
+
415
+ if download_path and os.path.exists(download_path):
416
+ return response, download_path
417
+ else:
418
  print("❌ PDF not found or failed to generate.")
419
  return response, None
420
+
 
 
421
  #Agentic Framework from HF spaces
422
  # agent_iframe = gr.HTML(
423
  # '<iframe src="https://jaamie-mental-health-agent.hf.space" width="100%" height="700px" style="border:none;"></iframe>'
 
491
  gr.Textbox(label="Confidence Score"),
492
  gr.Textbox(label="Status")
493
  ],
494
+ outputs=[
495
+ gr.Textbox(label="🧠 Personalized Diagnosis", lines=8),
496
+ gr.File(label="πŸ“₯ Download Diagnosis Report")
497
+ ],
498
  title="πŸ“ Submit Feedback With Session Metadata"
499
  )
500