Santhosh1705kumar commited on
Commit
d475230
·
verified ·
1 Parent(s): decc13a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -1,12 +1,8 @@
1
  import gradio as gr
2
  import pandas as pd
3
  from collections import defaultdict
4
- from transformers import pipeline
5
 
6
- # Load the Phi-2 model for symptom normalization
7
- symptom_normalizer = pipeline("text-classification", model="microsoft/phi-2")
8
-
9
- # Predefined symptoms
10
  symptom_data = {
11
  "Shortness of breath": {
12
  "questions": [
@@ -63,12 +59,7 @@ symptom_data = {
63
  # Global variables to track user state
64
  user_state = {}
65
 
66
- def normalize_symptom(user_input):
67
- labels = list(symptom_data.keys())
68
- predictions = symptom_normalizer(user_input, labels=labels)
69
- return predictions[0]['label']
70
-
71
- def chatbot(user_input):
72
  if "state" not in user_state:
73
  user_state["state"] = "greet"
74
 
@@ -77,10 +68,9 @@ def chatbot(user_input):
77
  return "Hello! I'm a medical AI assistant. Please describe your primary symptom."
78
 
79
  elif user_state["state"] == "ask_symptom":
80
- normalized_symptom = normalize_symptom(user_input)
81
- if normalized_symptom not in symptom_data:
82
  return "I don't recognize that symptom. Please enter one of these: " + ", ".join(symptom_data.keys())
83
- user_state["symptom"] = normalized_symptom
84
  user_state["state"] = "ask_duration"
85
  return "How long have you been experiencing this symptom? (Less than a week / More than a week)"
86
 
@@ -118,7 +108,18 @@ def chatbot(user_input):
118
  user_state.clear()
119
  return f"Based on your symptoms, the most likely condition is: {probable_disease}. Please consult a doctor for confirmation."
120
 
121
- # Gradio UI
122
- demo = gr.Interface(fn=chatbot, inputs=gr.Textbox(placeholder="Enter your response..."), outputs="text", title="Symptom Chatbot")
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  demo.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
  from collections import defaultdict
 
4
 
5
+ # Load the symptom-to-disease mapping
 
 
 
6
  symptom_data = {
7
  "Shortness of breath": {
8
  "questions": [
 
59
  # Global variables to track user state
60
  user_state = {}
61
 
62
+ def chatbot(user_input, history):
 
 
 
 
 
63
  if "state" not in user_state:
64
  user_state["state"] = "greet"
65
 
 
68
  return "Hello! I'm a medical AI assistant. Please describe your primary symptom."
69
 
70
  elif user_state["state"] == "ask_symptom":
71
+ if user_input not in symptom_data:
 
72
  return "I don't recognize that symptom. Please enter one of these: " + ", ".join(symptom_data.keys())
73
+ user_state["symptom"] = user_input
74
  user_state["state"] = "ask_duration"
75
  return "How long have you been experiencing this symptom? (Less than a week / More than a week)"
76
 
 
108
  user_state.clear()
109
  return f"Based on your symptoms, the most likely condition is: {probable_disease}. Please consult a doctor for confirmation."
110
 
111
+ # Gradio Chatbot UI
112
+ with gr.Blocks() as demo:
113
+ gr.Markdown("# Symptom Chatbot")
114
+ chatbot_ui = gr.Chatbot()
115
+ user_input = gr.Textbox(placeholder="Enter your response...")
116
+ submit = gr.Button("Send")
117
+
118
+ def respond(user_message, history):
119
+ bot_response = chatbot(user_message, history)
120
+ history.append((user_message, bot_response))
121
+ return history, ""
122
+
123
+ submit.click(respond, [user_input, chatbot_ui], [chatbot_ui, user_input])
124
 
125
  demo.launch()