chburhan64 commited on
Commit
2108566
·
verified ·
1 Parent(s): 2856131

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -114
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import requests
3
  import os
4
 
5
- API_KEY = os.getenv("AIMLAPI_API_KEY") or "42959a7da8904b0785ad2bb304172793"
6
  API_URL = "https://api.aimlapi.com/v1/chat/completions"
7
  SYSTEM_PROMPT = "You are a helpful and knowledgeable veterinary assistant AI. Give short but useful answers regarding pet and animal health, nutrition, breeds, and natural remedies. Keep it simple and consult a vet in critical cases."
8
 
@@ -15,60 +15,25 @@ animal_options = {
15
  "Wild": ["Tiger", "Lion", "Elephant", "Deer", "Bear", "Leopard", "Wolf", "Giraffe", "Other"]
16
  }
17
 
18
- def show_only_selected(category):
19
- return (
20
- gr.update(visible=True, value=f"### Selected Category: {category}"),
21
- gr.update(visible=False),
22
- gr.update(visible=False),
23
- gr.update(visible=False),
24
- gr.update(choices=animal_options.get(category, []), value=None, visible=True),
25
- gr.update(value="", visible=False), # other input
26
- gr.update(visible=True), # form row
27
- gr.update(visible=True), # topic
28
- gr.update(visible=True), # user detail
29
- gr.update(visible=True), # submit
30
- gr.update(visible=False), # output box hidden initially
31
- gr.update(visible=True), # back button
32
- gr.update(visible=False), # followup input
33
- gr.update(visible=False), # followup btn
34
- gr.update(visible=False), # followup output
35
- gr.update(visible=False) # followup header
36
- )
37
-
38
- def update_animals(selected_animal):
39
- return gr.update(visible=(selected_animal == "Other"), value="")
40
-
41
- def back_to_home():
42
- return (
43
- gr.update(visible=False), # category markdown
44
- gr.update(visible=True), # pet
45
- gr.update(visible=True), # domestic
46
- gr.update(visible=True), # wild
47
- gr.update(visible=False), # animal dropdown
48
- gr.update(visible=False, value=""), # other animal
49
- gr.update(visible=False), # form row
50
- gr.update(visible=False), # topic
51
- gr.update(visible=False), # user detail
52
- gr.update(visible=False), # submit
53
- gr.update(visible=False), # output
54
- gr.update(visible=False), # back
55
- gr.update(visible=False), # followup input
56
- gr.update(visible=False), # followup btn
57
- gr.update(visible=False), # followup output
58
- gr.update(visible=False) # followup header
59
- )
60
-
61
- def ask_ai(category_text, selected_animal, other_animal, age, gender, height, weight, topic, user_detail):
62
  global chat_history, followup_count
63
  followup_count = 0
64
- category = category_text.replace("### Selected Category: ", "").strip()
65
- animal = other_animal if selected_animal == "Other" else selected_animal
66
 
67
  user_prompt = f"""Animal Category: {category}
68
  Animal: {animal}
69
  Age: {age}, Gender: {gender}, Height: {height}, Weight: {weight}
70
  Topic: {topic}
71
- Detail: {user_detail.strip()[:60]}"""
 
72
 
73
  messages = [
74
  {"role": "system", "content": SYSTEM_PROMPT},
@@ -94,15 +59,9 @@ Detail: {user_detail.strip()[:60]}"""
94
  answer = result['choices'][0]['message']['content'].strip()
95
  chat_history.clear()
96
  chat_history.extend(messages + [{"role": "assistant", "content": answer}])
97
- return (
98
- answer,
99
- gr.update(visible=True),
100
- gr.update(visible=True),
101
- gr.update(visible=True),
102
- gr.update(visible=True)
103
- )
104
  except Exception as e:
105
- return (f"❌ Error: {str(e)}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
106
 
107
  def handle_followup(user_input):
108
  global chat_history, followup_count
@@ -134,73 +93,37 @@ def handle_followup(user_input):
134
  except Exception as e:
135
  return f"❌ Error: {str(e)}"
136
 
 
137
  with gr.Blocks() as demo:
138
  gr.Markdown("## 🐾 AI Vet Assistant")
139
 
140
  with gr.Row():
141
- pet_btn = gr.Button("Pet")
142
- domestic_btn = gr.Button("Domestic")
143
- wild_btn = gr.Button("Wild")
144
 
145
- selected_category = gr.Markdown(visible=False)
146
- animal = gr.Dropdown(label="Select Animal", choices=[], visible=False)
147
- other_input = gr.Textbox(label="Enter Animal", visible=False)
148
 
149
- with gr.Row(visible=False) as form_row:
150
- age = gr.Textbox(label="Age")
151
  gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
152
- height = gr.Textbox(label="Height")
153
- weight = gr.Textbox(label="Weight")
154
-
155
- topic = gr.Dropdown(["Health", "Nutrition", "Breed Info", "Natural Remedies"], label="Topic", visible=False)
156
- user_detail = gr.Textbox(label="Problem Detail", max_lines=2, visible=False)
157
- submit_btn = gr.Button("Get Advice", visible=False)
158
- output = gr.Textbox(label="AI Advice", lines=6, visible=False)
159
-
160
- followup_header = gr.Markdown("### 🔄 Follow-up Questions", visible=False)
161
- followup_input = gr.Textbox(label="Your Follow-up", visible=False)
162
- followup_btn = gr.Button("Ask Follow-up", visible=False)
163
- followup_output = gr.Textbox(label="Follow-up Response", lines=4, visible=False)
164
-
165
- back_btn = gr.Button("Back to Home", visible=False)
166
-
167
- pet_btn.click(show_only_selected, inputs=["Pet"], outputs=[
168
- selected_category, pet_btn, domestic_btn, wild_btn,
169
- animal, other_input, form_row, topic, user_detail,
170
- submit_btn, output, back_btn, followup_input,
171
- followup_btn, followup_output, followup_header
172
- ])
173
-
174
- domestic_btn.click(show_only_selected, inputs=["Domestic"], outputs=[
175
- selected_category, pet_btn, domestic_btn, wild_btn,
176
- animal, other_input, form_row, topic, user_detail,
177
- submit_btn, output, back_btn, followup_input,
178
- followup_btn, followup_output, followup_header
179
- ])
180
-
181
- wild_btn.click(show_only_selected, inputs=["Wild"], outputs=[
182
- selected_category, pet_btn, domestic_btn, wild_btn,
183
- animal, other_input, form_row, topic, user_detail,
184
- submit_btn, output, back_btn, followup_input,
185
- followup_btn, followup_output, followup_header
186
- ])
187
-
188
- animal.change(update_animals, inputs=animal, outputs=other_input)
189
-
190
- submit_btn.click(
191
- ask_ai,
192
- inputs=[selected_category, animal, other_input, age, gender, height, weight, topic, user_detail],
193
- outputs=[output, followup_input, followup_btn, followup_output, followup_header]
194
- )
195
 
196
- followup_btn.click(handle_followup, inputs=followup_input, outputs=followup_output)
 
 
 
 
197
 
198
- back_btn.click(back_to_home, outputs=[
199
- selected_category, pet_btn, domestic_btn, wild_btn,
200
- animal, other_input, form_row, topic, user_detail,
201
- submit_btn, output, back_btn, followup_input,
202
- followup_btn, followup_output, followup_header
203
- ])
 
204
 
205
  if __name__ == "__main__":
206
  demo.launch()
 
2
  import requests
3
  import os
4
 
5
+ API_KEY = os.getenv("AIMLAPI_API_KEY") or "your_aimlapi_key_here"
6
  API_URL = "https://api.aimlapi.com/v1/chat/completions"
7
  SYSTEM_PROMPT = "You are a helpful and knowledgeable veterinary assistant AI. Give short but useful answers regarding pet and animal health, nutrition, breeds, and natural remedies. Keep it simple and consult a vet in critical cases."
8
 
 
15
  "Wild": ["Tiger", "Lion", "Elephant", "Deer", "Bear", "Leopard", "Wolf", "Giraffe", "Other"]
16
  }
17
 
18
+ def update_animals(category):
19
+ animals = animal_options.get(category, [])
20
+ return gr.update(choices=animals, visible=True, value=None), gr.update(visible=False, value="")
21
+
22
+ def handle_other_animal(selected):
23
+ return gr.update(visible=(selected == "Other"), value="")
24
+
25
+ def ask_ai(category, selected_animal, other_animal, age, gender, height, weight, topic, user_detail):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  global chat_history, followup_count
27
  followup_count = 0
28
+ user_detail = user_detail.strip()[:60]
29
+ animal = other_animal.strip() if selected_animal == "Other" else selected_animal
30
 
31
  user_prompt = f"""Animal Category: {category}
32
  Animal: {animal}
33
  Age: {age}, Gender: {gender}, Height: {height}, Weight: {weight}
34
  Topic: {topic}
35
+ Detail: {user_detail}
36
+ Give a short and relevant response."""
37
 
38
  messages = [
39
  {"role": "system", "content": SYSTEM_PROMPT},
 
59
  answer = result['choices'][0]['message']['content'].strip()
60
  chat_history.clear()
61
  chat_history.extend(messages + [{"role": "assistant", "content": answer}])
62
+ return answer
 
 
 
 
 
 
63
  except Exception as e:
64
+ return f"❌ Error: {str(e)}"
65
 
66
  def handle_followup(user_input):
67
  global chat_history, followup_count
 
93
  except Exception as e:
94
  return f"❌ Error: {str(e)}"
95
 
96
+ # Launch app
97
  with gr.Blocks() as demo:
98
  gr.Markdown("## 🐾 AI Vet Assistant")
99
 
100
  with gr.Row():
101
+ category = gr.Dropdown(["Pet", "Domestic", "Wild"], label="Animal Category")
102
+ animal = gr.Dropdown(label="Select Animal", visible=False)
103
+ other_input = gr.Textbox(label="Enter Animal", visible=False)
104
 
105
+ category.change(update_animals, category, [animal, other_input])
106
+ animal.change(handle_other_animal, animal, other_input)
 
107
 
108
+ with gr.Row():
109
+ age = gr.Textbox(label="Age (e.g. 2 years)")
110
  gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
111
+ height = gr.Textbox(label="Height (e.g. 30 cm)")
112
+ weight = gr.Textbox(label="Weight (e.g. 10 kg)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
+ topic = gr.Dropdown(["Health", "Nutrition", "Breed Info", "Natural Remedies"], label="Topic")
115
+ user_detail = gr.Textbox(label="Problem Detail (max 60 characters)", max_lines=2)
116
+
117
+ submit_btn = gr.Button("Get Advice")
118
+ output = gr.Textbox(label="AI Advice", lines=8)
119
 
120
+ gr.Markdown("### 🔄 Follow-up Questions")
121
+ followup_input = gr.Textbox(label="Your Follow-up")
122
+ followup_btn = gr.Button("Ask Follow-up")
123
+ followup_output = gr.Textbox(label="Follow-up Response", lines=4)
124
+
125
+ submit_btn.click(ask_ai, inputs=[category, animal, other_input, age, gender, height, weight, topic, user_detail], outputs=output)
126
+ followup_btn.click(handle_followup, inputs=followup_input, outputs=followup_output)
127
 
128
  if __name__ == "__main__":
129
  demo.launch()