REALME5-pro commited on
Commit
938d757
·
verified ·
1 Parent(s): 4eaa6ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -3
app.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastai.text.all import *
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
  import gradio as gr
5
 
@@ -15,7 +15,7 @@ def classify_medical_text(txt):
15
  return dict(zip(medical_categories, map(float, probs)))
16
 
17
  # Load the psychiatric model from Hugging Face
18
- psychiatric_model_name = "nlp4good/psych-search" # Replace with the appropriate model
19
  psychiatric_tokenizer = AutoTokenizer.from_pretrained(psychiatric_model_name)
20
  psychiatric_model = AutoModelForSequenceClassification.from_pretrained(psychiatric_model_name)
21
 
@@ -31,6 +31,27 @@ def classify_psychiatric_text(txt):
31
  probabilities = torch.softmax(logits, dim=1).squeeze().tolist()
32
  return dict(zip(psychiatric_labels, probabilities))
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # Gradio Interfaces
35
  medical_text = gr.Textbox(lines=2, label='Describe your symptoms in detail')
36
  medical_label = gr.Label()
@@ -40,6 +61,17 @@ psychiatric_text = gr.Textbox(lines=2, label='Describe your mental health concer
40
  psychiatric_label = gr.Label()
41
  psychiatric_examples = ['I feel hopeless and have no energy.', 'I am unable to concentrate and feel anxious all the time.', 'I have recurring intrusive thoughts.']
42
 
 
 
 
 
 
 
 
 
 
 
 
43
  medical_interface = gr.Interface(
44
  fn=classify_medical_text,
45
  inputs=medical_text,
@@ -56,6 +88,16 @@ psychiatric_interface = gr.Interface(
56
  description=psychiatric_description,
57
  )
58
 
 
 
 
 
 
 
59
  # Combine interfaces using Tabs
60
- app = gr.TabbedInterface([medical_interface, psychiatric_interface], ["Medical Diagnosis", "Psychiatric Analysis"])
 
 
 
 
61
  app.launch(inline=False)
 
1
  from fastai.text.all import *
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
  import gradio as gr
5
 
 
15
  return dict(zip(medical_categories, map(float, probs)))
16
 
17
  # Load the psychiatric model from Hugging Face
18
+ psychiatric_model_name = "mental/mental-bert-base-uncased" # Replace with the appropriate model
19
  psychiatric_tokenizer = AutoTokenizer.from_pretrained(psychiatric_model_name)
20
  psychiatric_model = AutoModelForSequenceClassification.from_pretrained(psychiatric_model_name)
21
 
 
31
  probabilities = torch.softmax(logits, dim=1).squeeze().tolist()
32
  return dict(zip(psychiatric_labels, probabilities))
33
 
34
+ # Load pre-trained conversational model for Lifestyle and Nutrition Chatbot
35
+ lifestyle_model_name = "microsoft/DialoGPT-medium" # Replace with a fine-tuned model if available
36
+ lifestyle_tokenizer = AutoTokenizer.from_pretrained(lifestyle_model_name)
37
+ lifestyle_model = AutoModelForCausalLM.from_pretrained(lifestyle_model_name)
38
+
39
+ # Chat function for Lifestyle and Nutrition
40
+ chat_history = []
41
+
42
+ def chatbot_response(user_input):
43
+ global chat_history
44
+ new_input_ids = lifestyle_tokenizer.encode(user_input + lifestyle_tokenizer.eos_token, return_tensors='pt')
45
+ bot_input_ids = torch.cat([chat_history, new_input_ids], dim=-1) if chat_history else new_input_ids
46
+ chat_history = lifestyle_model.generate(bot_input_ids, max_length=1000, pad_token_id=lifestyle_tokenizer.eos_token_id)
47
+ response = lifestyle_tokenizer.decode(chat_history[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
48
+ return response
49
+
50
+ def clear_chat():
51
+ global chat_history
52
+ chat_history = []
53
+ return []
54
+
55
  # Gradio Interfaces
56
  medical_text = gr.Textbox(lines=2, label='Describe your symptoms in detail')
57
  medical_label = gr.Label()
 
61
  psychiatric_label = gr.Label()
62
  psychiatric_examples = ['I feel hopeless and have no energy.', 'I am unable to concentrate and feel anxious all the time.', 'I have recurring intrusive thoughts.']
63
 
64
+ lifestyle_chatbot = gr.Chatbot(label="Chat with me about your health and nutrition!")
65
+ lifestyle_msg = gr.Textbox(placeholder="Ask your question here...", label="Your Question")
66
+ lifestyle_clear = gr.Button("Clear Chat")
67
+
68
+ def user_message(input_text):
69
+ if not input_text.strip():
70
+ return lifestyle_chatbot, "Please enter a question."
71
+ response = chatbot_response(input_text)
72
+ lifestyle_chatbot.append((input_text, response))
73
+ return lifestyle_chatbot, ""
74
+
75
  medical_interface = gr.Interface(
76
  fn=classify_medical_text,
77
  inputs=medical_text,
 
88
  description=psychiatric_description,
89
  )
90
 
91
+ with gr.Blocks() as lifestyle_interface:
92
+ gr.Markdown("## Lifestyle and Nutrition Chatbot")
93
+ gr.Markdown("Ask me anything about fitness, nutrition, or wellness!")
94
+ lifestyle_msg.submit(user_message, inputs=[lifestyle_msg], outputs=[lifestyle_chatbot, lifestyle_msg])
95
+ lifestyle_clear.click(clear_chat, outputs=[lifestyle_chatbot])
96
+
97
  # Combine interfaces using Tabs
98
+ app = gr.TabbedInterface(
99
+ [medical_interface, psychiatric_interface, lifestyle_interface],
100
+ ["Medical Diagnosis", "Psychiatric Analysis", "Lifestyle & Nutrition Chat"]
101
+ )
102
+
103
  app.launch(inline=False)