import gradio as gr from transformers import pipeline # Load a free model from Hugging Face (DistilGPT2) gpt = pipeline("text-generation", model="distilgpt2") # More structured symptoms and conditions database with a basic "likelihood" conditions_db = { "fever": {"flu": 0.8, "cold": 0.7, "malaria": 0.6}, "headache": {"migraine": 0.7, "tension headache": 0.6, "dehydration": 0.5}, "fatigue": {"anemia": 0.7, "thyroid issues": 0.6, "lack of sleep": 0.8}, "cough": {"bronchitis": 0.7, "cold": 0.6, "covid-19": 0.7}, "sore throat": {"strep throat": 0.8, "flu": 0.7, "covid-19": 0.6}, } # Function to predict conditions with likelihood def predict_conditions(symptoms): possible_conditions = {} for symptom in symptoms: symptom = symptom.lower().strip() if symptom in conditions_db: for condition, likelihood in conditions_db[symptom].items(): possible_conditions[condition] = possible_conditions.get(condition, 0) + likelihood # Sort conditions by total likelihood (descending) sorted_conditions = sorted(possible_conditions.items(), key=lambda item: item[1], reverse=True) return [f"{condition} (likelihood: {likelihood:.1f})" for condition, likelihood in sorted_conditions] # Function to respond to user input def health_chat(user_input): if not user_input.strip(): return "Please enter your symptoms." symptoms = [word.strip() for word in user_input.lower().split(",")] predicted_conditions = predict_conditions(symptoms) if predicted_conditions: response = f"Based on the symptoms you described ({', '.join(symptoms)}), possible conditions from our limited database (with a basic likelihood indication) include: {', '.join(predicted_conditions)}. For more specific information and management, please consult a healthcare professional." return response else: prompt = f"""The user is describing some health concerns: "{user_input}". As a helpful AI assistant for demonstration purposes, provide a brief, general overview of potential health areas these symptoms might relate to. Suggest a couple of possibilities in a very general way, and strongly emphasize that this is not medical advice and the user should consult a doctor for accurate diagnosis and treatment. Do not provide specific diagnoses or treatments.""" ai_response = gpt(prompt, max_length=250, num_return_sequences=1, do_sample=True)[0]['generated_text'] return f"Based on what you've described, some general health areas to consider could include: {ai_response} It's crucial to remember that I am not a medical professional and this information is for demonstration purposes only. Please consult a qualified doctor for an accurate diagnosis and appropriate management of your health concerns." # Gradio UI interface = gr.Interface( fn=health_chat, inputs=gr.Textbox(lines=2, placeholder="Enter your symptoms, separated by commas, e.g., fever, cough"), outputs=gr.Textbox(placeholder="Possible conditions or AI response will appear here.") ) interface.launch()
verified