Spaces:
Sleeping
Sleeping
from fastai.text.all import * | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import torch | |
import gradio as gr | |
# Load the medical model | |
medical_learn = load_learner('model.pkl') | |
# Medical model configuration | |
medical_description = "Medical Diagnosis" | |
medical_categories = ['Allergy', 'Anemia', 'Bronchitis', 'Diabetes', 'Diarrhea', 'Fatigue', 'Flu', 'Malaria', 'Stress'] | |
def classify_medical_text(txt): | |
pred, idx, probs = medical_learn.predict(txt) | |
return dict(zip(medical_categories, map(float, probs))) | |
# Load the psychiatric model from Hugging Face | |
psychiatric_model_name = "mental/mental-bert-base-uncased" # Replace with the appropriate model | |
psychiatric_tokenizer = AutoTokenizer.from_pretrained(psychiatric_model_name) | |
psychiatric_model = AutoModelForSequenceClassification.from_pretrained(psychiatric_model_name) | |
# Psychiatric model configuration | |
psychiatric_description = "Psychiatric Analysis" | |
psychiatric_labels = ['Depression', 'Anxiety', 'Bipolar Disorder', 'PTSD', 'OCD', 'Stress', 'Schizophrenia'] # Adjust based on the model | |
def classify_psychiatric_text(txt): | |
inputs = psychiatric_tokenizer(txt, return_tensors="pt", truncation=True, padding=True) | |
with torch.no_grad(): | |
outputs = psychiatric_model(**inputs) | |
logits = outputs.logits | |
probabilities = torch.softmax(logits, dim=1).squeeze().tolist() | |
return dict(zip(psychiatric_labels, probabilities)) | |
# Load pre-trained conversational model for Lifestyle and Nutrition Chatbot | |
lifestyle_model_name = "microsoft/DialoGPT-medium" # Replace with a fine-tuned model if available | |
lifestyle_tokenizer = AutoTokenizer.from_pretrained(lifestyle_model_name) | |
lifestyle_model = AutoModelForCausalLM.from_pretrained(lifestyle_model_name) | |
# Chat function for Lifestyle and Nutrition | |
chat_history = [] | |
def chatbot_response(user_input): | |
global chat_history | |
new_input_ids = lifestyle_tokenizer.encode(user_input + lifestyle_tokenizer.eos_token, return_tensors='pt') | |
bot_input_ids = torch.cat([chat_history, new_input_ids], dim=-1) if chat_history else new_input_ids | |
chat_history = lifestyle_model.generate(bot_input_ids, max_length=1000, pad_token_id=lifestyle_tokenizer.eos_token_id) | |
response = lifestyle_tokenizer.decode(chat_history[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True) | |
return response | |
def clear_chat(): | |
global chat_history | |
chat_history = [] | |
return [] | |
# Gradio Interfaces | |
medical_text = gr.Textbox(lines=2, label='Describe your symptoms in detail') | |
medical_label = gr.Label() | |
medical_examples = ['I feel short of breath and have a high fever.', 'My throat hurts and I keep sneezing.', 'I am always thirsty.'] | |
psychiatric_text = gr.Textbox(lines=2, label='Describe your mental health concerns in detail') | |
psychiatric_label = gr.Label() | |
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.'] | |
lifestyle_chatbot = gr.Chatbot(label="Chat with me about your health and nutrition!") | |
lifestyle_msg = gr.Textbox(placeholder="Ask your question here...", label="Your Question") | |
lifestyle_clear = gr.Button("Clear Chat") | |
def user_message(input_text): | |
if not input_text.strip(): | |
return lifestyle_chatbot, "Please enter a question." | |
response = chatbot_response(input_text) | |
lifestyle_chatbot.append((input_text, response)) | |
return lifestyle_chatbot, "" | |
medical_interface = gr.Interface( | |
fn=classify_medical_text, | |
inputs=medical_text, | |
outputs=medical_label, | |
examples=medical_examples, | |
description=medical_description, | |
) | |
psychiatric_interface = gr.Interface( | |
fn=classify_psychiatric_text, | |
inputs=psychiatric_text, | |
outputs=psychiatric_label, | |
examples=psychiatric_examples, | |
description=psychiatric_description, | |
) | |
with gr.Blocks() as lifestyle_interface: | |
gr.Markdown("## Lifestyle and Nutrition Chatbot") | |
gr.Markdown("Ask me anything about fitness, nutrition, or wellness!") | |
lifestyle_msg.submit(user_message, inputs=[lifestyle_msg], outputs=[lifestyle_chatbot, lifestyle_msg]) | |
lifestyle_clear.click(clear_chat, outputs=[lifestyle_chatbot]) | |
# Combine interfaces using Tabs | |
app = gr.TabbedInterface( | |
[medical_interface, psychiatric_interface, lifestyle_interface], | |
["Medical Diagnosis", "Psychiatric Analysis", "Lifestyle & Nutrition Chat"] | |
) | |
app.launch(inline=False) | |