import gradio as gr import os import pandas as pd from transformers import pipeline,…
- User
import gradio as gr import os import pandas as pd from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM from huggingface_hub import InferenceClient
Load the medical dataset from GitHub
url = "https://raw.githubusercontent.com/wifix199/health_dataset/refs/heads/main/dataset.csv" data = pd.read_csv(url)
Assuming the dataset has 'Symptom' and 'Disease' columns
symptoms = data['symptoms'].tolist() diseases = data['disease'].tolist() HF_TOKEN = os.getenv("HF_TOKEN")
Load an open-source LLM (e.g., facebook/opt-350m)
model_name = "mistralai/Mistral-7B-Instruct-v0.3" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name)
Create a text generation pipeline with 'max_new_tokens' to limit output length
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
Define the chatbot logic
async def chatbot(messages, symptom_input): # Add user input to the chat history messages.append(("User", symptom_input))
code# Find the closest matching symptom in the dataset matched_diseases = [disease for symptom, disease in zip(symptoms, diseases) if symptom_input.lower() in symptom.lower()] if matched_diseases: response = f"Based on the symptom '{symptom_input}', the potential disease(s) might be: {', '.join(matched_diseases)}." else: # Generate a more refined response using the LLM if no match is found refined_prompt = ( f"The user has reported a symptom {symptom_input}." "Please provide a brief and informative explanation, focusing on possible causes, and avoid repeating the user's input. " "Ensure the response is concise and helpful." ) try: generated = text_generator(refined_prompt, max_new_tokens=150, num_return_sequences=1) response = generated[0]['generated_text'].strip() except Exception as e: response = f"Error during text generation: {str(e)}" # Add the bot's response to the chat history messages.append(("Bot", response)) return messages, "" - Assistant
Certainly! To change the way the bot responds, we can adjust the logic within the
chatbotfunction. Here are a few ways you could modify the bot's response generation:-
Include More Detailed Explanations: You may want the bot to use the language model to provide a detailed explanation even when matched diseases are found, instead of directly outputting the symptom-disease match.
-
- User
Generate a more refined response using the LLM
coderefined_prompt = ( f"The user has reported a symptom '{symptom_input}'. " "Please provide a friendly and informative response, explaining potential diseases, lifestyle recommendations, " "and guidelines on when to consult a healthcare professional. Ensure the response is divided into clear sections." )Change this one
- Assistant
Certainly! If you want to change the prompt to elicit different behavior from the language model, you might consider focusing on different attributes or styles of the response. Here's an example of a revised prompt:
python# Generate a more refined response using the LLM refined_prompt = ( f"The user mentioned experiencing the symptom '{symptom_input}'. " "Craft a compassionate and concise response that includes the following sections: " "1. Possible medical conditions associated with the symptom, " "2. Simple lifestyle modifications that might help, and " "3. When it would be advisable to seek medical attention. " "Keep the language easy to understand and encourage proactive health management." ) - User
Generate a more refined response using the LLM
coderefined_prompt = ( f"The user has reported a symptom '{symptom_input}'. " "Please provide a friendly and informative response, explaining potential diseases, lifestyle recommendations, " "and guidelines on when to consult a healthcare professional. Ensure the response is divided into clear sections." )Redefine or remove this one
- Assistant
Certainly! If you want to redefine the prompt or remove it entirely, you can either simplify the task or focus on a different set of instructions for the language model. Here's how you might redefine it:
Simplified Prompt Version
If you want to make it simpler and less structured but still informative:
python - User
- Assistant