Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,117 @@
|
|
|
|
|
|
1 |
from fastai.text.all import *
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
import torch
|
4 |
-
|
|
|
|
|
5 |
|
6 |
# Load the medical model
|
7 |
medical_learn = load_learner('model.pkl')
|
8 |
|
9 |
# Medical model configuration
|
10 |
-
medical_description = "Medical Diagnosis"
|
11 |
medical_categories = ['Allergy', 'Anemia', 'Bronchitis', 'Diabetes', 'Diarrhea', 'Fatigue', 'Flu', 'Malaria', 'Stress']
|
12 |
|
13 |
def classify_medical_text(txt):
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
# Load the psychiatric model
|
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 |
|
22 |
# Psychiatric model configuration
|
23 |
-
|
24 |
-
psychiatric_labels = ['Depression', 'Anxiety', 'Bipolar Disorder', 'PTSD', 'OCD', 'Stress', 'Schizophrenia'] # Adjust based on the model
|
25 |
|
26 |
def classify_psychiatric_text(txt):
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
43 |
medical_interface = gr.Interface(
|
44 |
fn=classify_medical_text,
|
45 |
-
inputs=
|
46 |
-
outputs=
|
47 |
-
examples=
|
48 |
-
description=
|
49 |
)
|
50 |
|
51 |
psychiatric_interface = gr.Interface(
|
52 |
fn=classify_psychiatric_text,
|
53 |
-
inputs=
|
54 |
-
outputs=
|
55 |
-
examples=
|
56 |
-
description=
|
57 |
)
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
from fastai.text.all import *
|
4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
import torch
|
6 |
+
|
7 |
+
# Initialize Hugging Face Client
|
8 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
9 |
|
10 |
# Load the medical model
|
11 |
medical_learn = load_learner('model.pkl')
|
12 |
|
13 |
# Medical model configuration
|
|
|
14 |
medical_categories = ['Allergy', 'Anemia', 'Bronchitis', 'Diabetes', 'Diarrhea', 'Fatigue', 'Flu', 'Malaria', 'Stress']
|
15 |
|
16 |
def classify_medical_text(txt):
|
17 |
+
try:
|
18 |
+
pred, idx, probs = medical_learn.predict(txt)
|
19 |
+
return dict(zip(medical_categories, map(float, probs)))
|
20 |
+
except Exception as e:
|
21 |
+
return {"error": str(e)}
|
22 |
|
23 |
+
# Load the psychiatric model
|
24 |
psychiatric_model_name = "nlp4good/psych-search" # Replace with the appropriate model
|
25 |
psychiatric_tokenizer = AutoTokenizer.from_pretrained(psychiatric_model_name)
|
26 |
psychiatric_model = AutoModelForSequenceClassification.from_pretrained(psychiatric_model_name)
|
27 |
|
28 |
# Psychiatric model configuration
|
29 |
+
psychiatric_labels = ['Depression', 'Anxiety', 'Bipolar Disorder', 'PTSD', 'OCD', 'Stress', 'Schizophrenia']
|
|
|
30 |
|
31 |
def classify_psychiatric_text(txt):
|
32 |
+
try:
|
33 |
+
inputs = psychiatric_tokenizer(txt, return_tensors="pt", truncation=True, padding=True)
|
34 |
+
with torch.no_grad():
|
35 |
+
outputs = psychiatric_model(**inputs)
|
36 |
+
logits = outputs.logits
|
37 |
+
probabilities = torch.softmax(logits, dim=1).squeeze().tolist()
|
38 |
+
return dict(zip(psychiatric_labels, probabilities))
|
39 |
+
except Exception as e:
|
40 |
+
return {"error": str(e)}
|
41 |
|
42 |
+
# Chat-based Interface
|
43 |
+
def respond(
|
44 |
+
message,
|
45 |
+
history: list[tuple[str, str]],
|
46 |
+
system_message,
|
47 |
+
max_tokens,
|
48 |
+
temperature,
|
49 |
+
top_p,
|
50 |
+
):
|
51 |
+
messages = [{"role": "system", "content": system_message}]
|
52 |
+
for val in history:
|
53 |
+
if val[0]:
|
54 |
+
messages.append({"role": "user", "content": val[0]})
|
55 |
+
if val[1]:
|
56 |
+
messages.append({"role": "assistant", "content": val[1]})
|
57 |
+
messages.append({"role": "user", "content": message})
|
58 |
|
59 |
+
response = ""
|
60 |
+
try:
|
61 |
+
for message in client.chat_completion(
|
62 |
+
messages,
|
63 |
+
max_tokens=max_tokens,
|
64 |
+
stream=True,
|
65 |
+
temperature=temperature,
|
66 |
+
top_p=top_p,
|
67 |
+
):
|
68 |
+
token = message.choices[0].delta.content
|
69 |
+
response += token
|
70 |
+
yield response
|
71 |
+
except Exception as e:
|
72 |
+
yield f"Error: {str(e)}"
|
73 |
|
74 |
+
# Gradio Interfaces
|
75 |
medical_interface = gr.Interface(
|
76 |
fn=classify_medical_text,
|
77 |
+
inputs=gr.Textbox(lines=2, label="Describe your symptoms in detail"),
|
78 |
+
outputs=gr.Label(label="Medical Diagnosis"),
|
79 |
+
examples=["I feel short of breath and have a high fever.", "My throat hurts and I keep sneezing.", "I am always thirsty."],
|
80 |
+
description="Identify potential medical conditions based on symptoms."
|
81 |
)
|
82 |
|
83 |
psychiatric_interface = gr.Interface(
|
84 |
fn=classify_psychiatric_text,
|
85 |
+
inputs=gr.Textbox(lines=2, label="Describe your mental health concerns in detail"),
|
86 |
+
outputs=gr.Label(label="Psychiatric Analysis"),
|
87 |
+
examples=["I feel hopeless and have no energy.", "I am unable to concentrate and feel anxious all the time.", "I have recurring intrusive thoughts."],
|
88 |
+
description="Analyze potential mental health concerns based on input."
|
89 |
)
|
90 |
|
91 |
+
chat_interface = gr.ChatInterface(
|
92 |
+
respond,
|
93 |
+
additional_inputs=[
|
94 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
95 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
96 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
97 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
98 |
+
],
|
99 |
+
description="Chat with an AI assistant for general inquiries or extended conversation."
|
100 |
+
)
|
101 |
+
|
102 |
+
# Unified Gradio App with Tabs
|
103 |
+
with gr.Blocks() as app:
|
104 |
+
gr.Markdown("# Unified Medical and Psychiatric Assistant")
|
105 |
+
|
106 |
+
with gr.Tab("Chat Assistant"):
|
107 |
+
chat_interface.render()
|
108 |
+
|
109 |
+
with gr.Tab("Medical Diagnosis"):
|
110 |
+
medical_interface.render()
|
111 |
+
|
112 |
+
with gr.Tab("Psychiatric Analysis"):
|
113 |
+
psychiatric_interface.render()
|
114 |
+
|
115 |
+
# Launch the App
|
116 |
+
if __name__ == "__main__":
|
117 |
+
app.launch()
|