chburhan64 commited on
Commit
bbbe7bc
·
verified ·
1 Parent(s): 9b82df0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -41
app.py CHANGED
@@ -16,42 +16,36 @@ animal_options = {
16
  }
17
 
18
  def show_only_selected(category):
19
- # Show only the selected category block
20
- # Return visibility and selected category name
21
  return (
22
- gr.update(visible=True, value=category),
23
  gr.update(visible=(category == "Pet")),
24
  gr.update(visible=(category == "Domestic")),
25
  gr.update(visible=(category == "Wild")),
26
- gr.update(visible=True), # animal dropdown visible
27
- gr.update(choices=animal_options.get(category, []), value=None),
28
- gr.update(visible=False), # other animal input hidden initially
29
- gr.update(visible=True), # form visible
30
- gr.update(visible=True), # Back to Home button visible
31
- gr.update(visible=False) # category selection blocks hidden
32
  )
33
 
34
  def update_animals(selected_animal):
35
  return gr.update(visible=(selected_animal == "Other"), value="")
36
 
37
  def back_to_home():
38
- # Show the 3 category blocks and hide the form & animal dropdown etc
39
  return (
40
- gr.update(visible=False), # selected_category_markdown hidden
41
- gr.update(visible=True), # pet_btn visible
42
- gr.update(visible=True), # domestic_btn visible
43
- gr.update(visible=True), # wild_btn visible
44
- gr.update(visible=False), # animal dropdown hidden
45
- gr.update(value=None, visible=False), # clear animal dropdown
46
- gr.update(visible=False, value=""), # other animal input hidden
47
- gr.update(visible=False), # form area hidden
48
- gr.update(visible=False), # back to home button hidden
49
- gr.update(visible=True) # category buttons visible
50
  )
51
 
52
- def ask_ai(category, selected_animal, other_animal, age, gender, height, weight, topic, user_detail):
53
  global chat_history, followup_count
54
  followup_count = 0
 
55
  user_detail = user_detail.strip()[:60]
56
  animal = other_animal.strip() if selected_animal == "Other" else selected_animal
57
 
@@ -120,11 +114,11 @@ def handle_followup(user_input):
120
  except Exception as e:
121
  return f"❌ Error: {str(e)}"
122
 
 
123
  with gr.Blocks() as demo:
124
 
125
  gr.Markdown("## 🐾 AI Vet Assistant")
126
 
127
- # Category selection buttons - visible on home
128
  with gr.Row():
129
  pet_btn = gr.Button("Pet", visible=True)
130
  domestic_btn = gr.Button("Domestic", visible=True)
@@ -132,11 +126,9 @@ with gr.Blocks() as demo:
132
 
133
  selected_category_markdown = gr.Markdown("", visible=False)
134
 
135
- # Animal dropdown and other input
136
  animal = gr.Dropdown(label="Select Animal", choices=[], visible=False)
137
  other_input = gr.Textbox(label="Enter Animal", visible=False)
138
 
139
- # Form fields - visible only after category selected
140
  with gr.Row(visible=False) as form_row:
141
  age = gr.Textbox(label="Age (e.g. 2 years)")
142
  gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
@@ -149,31 +141,46 @@ with gr.Blocks() as demo:
149
  submit_btn = gr.Button("Get Advice", visible=False)
150
  output = gr.Textbox(label="AI Advice", lines=8, visible=False)
151
 
152
- # Follow-up question section
153
- gr.Markdown("### 🔄 Follow-up Questions", visible=False, elem_id="followup_header")
154
  followup_input = gr.Textbox(label="Your Follow-up", visible=False)
155
  followup_btn = gr.Button("Ask Follow-up", visible=False)
156
  followup_output = gr.Textbox(label="Follow-up Response", lines=4, visible=False)
157
 
158
  back_home_btn = gr.Button("Back to Home", visible=False)
159
 
160
- # Events for category buttons
161
  pet_btn.click(show_only_selected, inputs=[], outputs=[
162
- selected_category_markdown, pet_btn, domestic_btn, wild_btn,
163
- animal, other_input, form_row, back_home_btn, pet_btn, domestic_btn
164
- ], _js=None) # Remove _js to avoid error
 
 
 
 
 
 
165
 
166
  domestic_btn.click(show_only_selected, inputs=[], outputs=[
167
- selected_category_markdown, pet_btn, domestic_btn, wild_btn,
168
- animal, other_input, form_row, back_home_btn, pet_btn, domestic_btn
169
- ], _js=None)
 
 
 
 
 
 
170
 
171
  wild_btn.click(show_only_selected, inputs=[], outputs=[
172
- selected_category_markdown, pet_btn, domestic_btn, wild_btn,
173
- animal, other_input, form_row, back_home_btn, pet_btn, domestic_btn
174
- ], _js=None)
 
 
 
 
 
 
175
 
176
- # Properly link animal dropdown to show/hide other input
177
  animal.change(update_animals, animal, other_input)
178
 
179
  submit_btn.click(
@@ -188,10 +195,33 @@ with gr.Blocks() as demo:
188
  back_to_home,
189
  inputs=[],
190
  outputs=[
191
- selected_category_markdown, pet_btn, domestic_btn, wild_btn,
192
- animal, other_input, form_row, back_home_btn, pet_btn, domestic_btn
 
 
 
 
 
 
193
  ]
194
  )
195
 
196
- if __name__ == "__main__":
197
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  }
17
 
18
  def show_only_selected(category):
 
 
19
  return (
20
+ gr.update(visible=True, value=f"**Selected Category:** {category}"),
21
  gr.update(visible=(category == "Pet")),
22
  gr.update(visible=(category == "Domestic")),
23
  gr.update(visible=(category == "Wild")),
24
+ gr.update(visible=True, choices=animal_options.get(category, []), value=None),
25
+ gr.update(visible=False),
26
+ gr.update(visible=True),
27
+ gr.update(visible=True),
 
 
28
  )
29
 
30
  def update_animals(selected_animal):
31
  return gr.update(visible=(selected_animal == "Other"), value="")
32
 
33
  def back_to_home():
 
34
  return (
35
+ gr.update(visible=False, value=""),
36
+ gr.update(visible=True),
37
+ gr.update(visible=True),
38
+ gr.update(visible=True),
39
+ gr.update(visible=False, value=None),
40
+ gr.update(visible=False, value=""),
41
+ gr.update(visible=False),
42
+ gr.update(visible=False),
 
 
43
  )
44
 
45
+ def ask_ai(category_md, selected_animal, other_animal, age, gender, height, weight, topic, user_detail):
46
  global chat_history, followup_count
47
  followup_count = 0
48
+ category = category_md.replace("**Selected Category:** ", "").strip()
49
  user_detail = user_detail.strip()[:60]
50
  animal = other_animal.strip() if selected_animal == "Other" else selected_animal
51
 
 
114
  except Exception as e:
115
  return f"❌ Error: {str(e)}"
116
 
117
+
118
  with gr.Blocks() as demo:
119
 
120
  gr.Markdown("## 🐾 AI Vet Assistant")
121
 
 
122
  with gr.Row():
123
  pet_btn = gr.Button("Pet", visible=True)
124
  domestic_btn = gr.Button("Domestic", visible=True)
 
126
 
127
  selected_category_markdown = gr.Markdown("", visible=False)
128
 
 
129
  animal = gr.Dropdown(label="Select Animal", choices=[], visible=False)
130
  other_input = gr.Textbox(label="Enter Animal", visible=False)
131
 
 
132
  with gr.Row(visible=False) as form_row:
133
  age = gr.Textbox(label="Age (e.g. 2 years)")
134
  gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
 
141
  submit_btn = gr.Button("Get Advice", visible=False)
142
  output = gr.Textbox(label="AI Advice", lines=8, visible=False)
143
 
144
+ followup_header = gr.Markdown("### 🔄 Follow-up Questions", visible=False)
 
145
  followup_input = gr.Textbox(label="Your Follow-up", visible=False)
146
  followup_btn = gr.Button("Ask Follow-up", visible=False)
147
  followup_output = gr.Textbox(label="Follow-up Response", lines=4, visible=False)
148
 
149
  back_home_btn = gr.Button("Back to Home", visible=False)
150
 
 
151
  pet_btn.click(show_only_selected, inputs=[], outputs=[
152
+ selected_category_markdown,
153
+ pet_btn,
154
+ domestic_btn,
155
+ wild_btn,
156
+ animal,
157
+ other_input,
158
+ form_row,
159
+ back_home_btn
160
+ ])
161
 
162
  domestic_btn.click(show_only_selected, inputs=[], outputs=[
163
+ selected_category_markdown,
164
+ pet_btn,
165
+ domestic_btn,
166
+ wild_btn,
167
+ animal,
168
+ other_input,
169
+ form_row,
170
+ back_home_btn
171
+ ])
172
 
173
  wild_btn.click(show_only_selected, inputs=[], outputs=[
174
+ selected_category_markdown,
175
+ pet_btn,
176
+ domestic_btn,
177
+ wild_btn,
178
+ animal,
179
+ other_input,
180
+ form_row,
181
+ back_home_btn
182
+ ])
183
 
 
184
  animal.change(update_animals, animal, other_input)
185
 
186
  submit_btn.click(
 
195
  back_to_home,
196
  inputs=[],
197
  outputs=[
198
+ selected_category_markdown,
199
+ pet_btn,
200
+ domestic_btn,
201
+ wild_btn,
202
+ animal,
203
+ other_input,
204
+ form_row,
205
+ back_home_btn
206
  ]
207
  )
208
 
209
+ def show_form_parts(show: bool):
210
+ return (
211
+ gr.update(visible=show),
212
+ gr.update(visible=show),
213
+ gr.update(visible=show),
214
+ gr.update(visible=show),
215
+ gr.update(visible=show),
216
+ gr.update(visible=show),
217
+ gr.update(visible=show),
218
+ gr.update(visible=show),
219
+ )
220
+
221
+ pet_btn.click(show_form_parts, inputs=[], outputs=[topic, user_detail, submit_btn, output, followup_header, followup_input, followup_btn, followup_output])
222
+ domestic_btn.click(show_form_parts, inputs=[], outputs=[topic, user_detail, submit_btn, output, followup_header, followup_input, followup_btn, followup_output])
223
+ wild_btn.click(show_form_parts, inputs=[], outputs=[topic, user_detail, submit_btn, output, followup_header, followup_input, followup_btn, followup_output])
224
+
225
+ back_home_btn.click(lambda: show_form_parts(False), inputs=[], outputs=[topic, user_detail, submit_btn, output, followup_header, followup_input, followup_btn, followup_output])
226
+
227
+ demo.launch()