|
import gradio as gr |
|
from sklearn.feature_extraction.text import CountVectorizer |
|
from sklearn.naive_bayes import MultinomialNB |
|
|
|
|
|
symptoms_data = { |
|
"fever": "Flu, Dengue, Malaria", |
|
"cough": "Cold, Flu, Tuberculosis", |
|
"chest pain": "Heart Attack, Asthma", |
|
"headache": "Migraine, Sinusitis, Flu", |
|
"fatigue": "Anemia, Hypothyroidism, Diabetes", |
|
"joint pain": "Arthritis, Gout", |
|
"rash": "Allergy, Chickenpox, Measles", |
|
"nausea": "Food Poisoning, Pregnancy, Gastritis" |
|
} |
|
|
|
|
|
def train_disease_model(): |
|
symptoms = list(symptoms_data.keys()) |
|
diseases = [symptoms_data[symptom] for symptom in symptoms] |
|
|
|
vectorizer = CountVectorizer() |
|
X = vectorizer.fit_transform(symptoms) |
|
model = MultinomialNB() |
|
model.fit(X, diseases) |
|
|
|
return vectorizer, model |
|
|
|
vectorizer, model = train_disease_model() |
|
|
|
|
|
def recognize_disease(symptoms): |
|
symptoms_vector = vectorizer.transform([symptoms]) |
|
predicted_disease = model.predict(symptoms_vector)[0] |
|
return f"Based on the symptoms, the possible diseases are: {predicted_disease}" |
|
|
|
|
|
def get_food_suggestions(disease): |
|
diet_recommendations = { |
|
"Diabetes": "Low-sugar fruits, whole grains, green leafy vegetables, nuts.", |
|
"Hypertension": "Low-sodium foods, potassium-rich foods like bananas and spinach.", |
|
"Anemia": "Iron-rich foods like spinach, red meat, and beans.", |
|
"Flu": "Warm fluids, citrus fruits, ginger tea.", |
|
"Heart Disease": "Oats, salmon, avocado, and nuts.", |
|
"Gastritis": "Non-spicy foods, bananas, rice, and yogurt.", |
|
} |
|
return diet_recommendations.get(disease, "No specific food recommendations available for this disease.") |
|
|
|
|
|
def assess_health(bp, heart_rate): |
|
if bp < 120: |
|
bp_status = "Normal" |
|
elif 120 <= bp <= 129: |
|
bp_status = "Elevated" |
|
elif 130 <= bp <= 139: |
|
bp_status = "Hypertension Stage 1" |
|
else: |
|
bp_status = "Hypertension Stage 2" |
|
|
|
if heart_rate < 60: |
|
hr_status = "Low Heart Rate (Bradycardia)" |
|
elif heart_rate > 100: |
|
hr_status = "High Heart Rate (Tachycardia)" |
|
else: |
|
hr_status = "Normal Heart Rate" |
|
|
|
return f"BP Status: {bp_status}, Heart Rate Status: {hr_status}" |
|
|
|
|
|
def health_tips(): |
|
return """General Health Tips: |
|
1. Maintain a balanced diet with plenty of vegetables and fruits. |
|
2. Exercise regularly, at least 30 minutes a day. |
|
3. Stay hydrated by drinking 8-10 glasses of water daily. |
|
4. Get 7-8 hours of sleep every night. |
|
5. Avoid smoking and limit alcohol intake.""" |
|
|
|
|
|
def create_gradio_interface(): |
|
with gr.Blocks() as app: |
|
gr.Markdown("# Health Management Application") |
|
|
|
|
|
with gr.Tab("Disease Recognizer"): |
|
symptoms_input = gr.Textbox(label="Enter Symptoms", placeholder="e.g., fever, cough") |
|
disease_output = gr.Textbox(label="Predicted Disease") |
|
recognize_button = gr.Button("Recognize Disease") |
|
recognize_button.click(recognize_disease, inputs=symptoms_input, outputs=disease_output) |
|
|
|
|
|
with gr.Tab("Diet Suggestions"): |
|
disease_input = gr.Textbox(label="Enter Disease", placeholder="e.g., Diabetes") |
|
diet_output = gr.Textbox(label="Recommended Foods") |
|
diet_button = gr.Button("Get Food Suggestions") |
|
diet_button.click(get_food_suggestions, inputs=disease_input, outputs=diet_output) |
|
|
|
|
|
with gr.Tab("Health Assessment"): |
|
bp_input = gr.Number(label="Blood Pressure (mmHg)") |
|
heart_rate_input = gr.Number(label="Heart Rate (bpm)") |
|
health_output = gr.Textbox(label="Health Status") |
|
health_button = gr.Button("Assess Health") |
|
health_button.click(assess_health, inputs=[bp_input, heart_rate_input], outputs=health_output) |
|
|
|
|
|
with gr.Tab("Health Tips"): |
|
tips_output = gr.Textbox(label="General Health Tips", value=health_tips(), interactive=False) |
|
|
|
return app |
|
|
|
|
|
app = create_gradio_interface() |
|
app.launch() |
|
|