Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,83 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Load the dataset
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Function to suggest treatment and medicines based on symptoms
|
| 8 |
def suggest_treatment(symptoms_input):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Create Gradio interface
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Launch the app
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import logging
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Set up logging to capture errors
|
| 8 |
+
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
# Verify that required files exist
|
| 12 |
+
try:
|
| 13 |
+
if not os.path.exists("dataset.csv"):
|
| 14 |
+
logger.error("dataset.csv not found!")
|
| 15 |
+
raise FileNotFoundError("dataset.csv not found!")
|
| 16 |
+
if not os.path.exists("style.css"):
|
| 17 |
+
logger.error("style.css not found!")
|
| 18 |
+
raise FileNotFoundError("style.css not found!")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
logger.error(f"Error checking files: {str(e)}")
|
| 21 |
+
raise
|
| 22 |
|
| 23 |
# Load the dataset
|
| 24 |
+
try:
|
| 25 |
+
logger.info("Loading dataset.csv...")
|
| 26 |
+
df = pd.read_csv("dataset.csv")
|
| 27 |
+
logger.info("Dataset loaded successfully.")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
logger.error(f"Error loading dataset: {str(e)}")
|
| 30 |
+
raise
|
| 31 |
|
| 32 |
# Function to suggest treatment and medicines based on symptoms
|
| 33 |
def suggest_treatment(symptoms_input):
|
| 34 |
+
try:
|
| 35 |
+
logger.info(f"Processing symptoms: {symptoms_input}")
|
| 36 |
+
symptoms_input = symptoms_input.lower().strip()
|
| 37 |
+
if not symptoms_input:
|
| 38 |
+
return "Please enter symptoms."
|
| 39 |
+
# Search for matching symptoms in the dataset
|
| 40 |
+
for index, row in df.iterrows():
|
| 41 |
+
dataset_symptoms = row['symptoms'].lower().strip()
|
| 42 |
+
input_symptom_list = [s.strip() for s in symptoms_input.split(",")]
|
| 43 |
+
dataset_symptom_list = [s.strip() for s in dataset_symptoms.split(",")]
|
| 44 |
+
if all(symptom in dataset_symptom_list for symptom in input_symptom_list):
|
| 45 |
+
result = (
|
| 46 |
+
f"**Possible Disease**: {row['disease']}\n"
|
| 47 |
+
f"**Suggested Treatment**: {row['treatment']}\n"
|
| 48 |
+
f"**Medicines**: {row['medicines']}"
|
| 49 |
+
)
|
| 50 |
+
logger.info(f"Suggestion generated: {result}")
|
| 51 |
+
return result
|
| 52 |
+
return "Sorry, no matching disease found for the given symptoms. Please consult a doctor."
|
| 53 |
+
except Exception as e:
|
| 54 |
+
logger.error(f"Error in suggest_treatment: {str(e)}")
|
| 55 |
+
return f"Error processing symptoms: {str(e)}"
|
| 56 |
|
| 57 |
+
# Create Gradio interface using Blocks
|
| 58 |
+
with gr.Blocks(css="style.css") as app:
|
| 59 |
+
gr.Markdown("# Medical Suggestion AI")
|
| 60 |
+
gr.Markdown("Enter your symptoms to get treatment and medicine suggestions.")
|
| 61 |
+
|
| 62 |
+
symptoms_input = gr.Textbox(
|
| 63 |
+
label="Symptoms (e.g., fever, headache)",
|
| 64 |
+
placeholder="Enter symptoms separated by commas",
|
| 65 |
+
elem_classes="symptoms-input"
|
| 66 |
+
)
|
| 67 |
+
submit_btn = gr.Button("Submit", elem_classes="submit-btn")
|
| 68 |
+
output = gr.Markdown(label="Suggestions", elem_classes="output")
|
| 69 |
+
|
| 70 |
+
submit_btn.click(
|
| 71 |
+
fn=suggest_treatment,
|
| 72 |
+
inputs=symptoms_input,
|
| 73 |
+
outputs=output
|
| 74 |
+
)
|
| 75 |
|
| 76 |
# Launch the app
|
| 77 |
+
try:
|
| 78 |
+
logger.info("Launching Gradio app...")
|
| 79 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 80 |
+
logger.info("Gradio app launched successfully.")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
logger.error(f"Error launching Gradio app: {str(e)}")
|
| 83 |
+
raise
|