chburhan64 commited on
Commit
3deccb7
Β·
verified Β·
1 Parent(s): 031e071

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -63
app.py CHANGED
@@ -1,22 +1,24 @@
1
  import gradio as gr
2
  import requests
3
  import os
 
 
4
 
 
5
  API_KEY = os.getenv("AIML_API_KEY") or "your_aimlapi_key_here"
6
  API_URL = "https://api.aimlapi.com/v1/chat/completions"
7
  SYSTEM_PROMPT = (
8
  "You are a highly knowledgeable and friendly veterinary assistant AI. "
9
- "Always provide clear, complete, and well-structured responses about dogs and cats. "
10
- "Include all relevant details such as symptoms, causes, treatments, precautions, nutrition, and recovery time where applicable. "
11
- "Format the response cleanly using markdown: use bold headings, emojis for clarity, and bullet points where needed. "
12
- "Avoid using symbols like ** or ## unless part of markdown. Make the text friendly, professional, and easy to read. "
13
- "Always provide a complete and relevant answer within the given token limit. Avoid cutting off in the middle of a sentence."
14
  )
15
 
16
-
17
  chat_history = []
18
  followup_count = 0
19
 
 
20
  breed_options = {
21
  "Cat": ["Persian", "Siamese", "Maine Coon", "British Shorthair", "Other"],
22
  "Dog": ["German Shepherd", "Labrador", "Bulldog", "Pomeranian", "Other"]
@@ -30,6 +32,7 @@ breed_info_topics = [
30
  "Favourite Food", "Most Common Diseases", "Precautions", "Allergies", "Favourite Activities", "Other"
31
  ]
32
 
 
33
  def update_breed(pet_type):
34
  return gr.update(choices=breed_options.get(pet_type, []), value=None, visible=True), gr.update(visible=False, value="")
35
 
@@ -97,7 +100,7 @@ def ask_ai(pet, breed, other_breed, age, gender, weight, topic, symptom, other_s
97
  "model": "gpt-4o-mini-2024-07-18",
98
  "messages": messages,
99
  "temperature": 0.7,
100
- "max_tokens": 300
101
  }
102
 
103
  headers = {
@@ -146,63 +149,123 @@ def handle_followup(user_input):
146
  except Exception as e:
147
  return f"❌ Error: {str(e)}"
148
 
149
- with gr.Blocks() as demo:
150
- gr.Markdown("## 🐾 Smart Paws AI Assistant")
151
-
152
- with gr.Row():
153
- pet = gr.Dropdown(["Cat", "Dog"], label="πŸ• Pet Type")
154
- breed = gr.Dropdown(label="🐾 Select Breed", visible=False)
155
- other_breed = gr.Textbox(label="✍️ Enter Breed", visible=False)
156
-
157
- pet.change(update_breed, pet, [breed, other_breed])
158
- breed.change(handle_breed_other, breed, other_breed)
159
-
160
- with gr.Row():
161
- age = gr.Textbox(label="πŸŽ‚ Age (e.g. 2 years or 8 months)")
162
- gender = gr.Dropdown(["Male", "Female", "Unknown"], label="🚻 Gender")
163
- weight = gr.Textbox(label="βš–οΈ Weight (e.g. 10 kg)")
164
-
165
- topic = gr.Dropdown(["Symptom Checker", "Nutrition", "Breed Info"], label="πŸ“š Select Topic")
166
-
167
- with gr.Column(visible=False) as symptom_section:
168
- symptom = gr.Dropdown(symptom_list, label="πŸ€’ Select Symptom")
169
- other_symptom = gr.Textbox(label="✍️ Enter Symptom", visible=False)
170
- symptom.change(handle_symptom_other, symptom, other_symptom)
171
-
172
- with gr.Column(visible=False) as nutrition_section:
173
- condition = gr.Textbox(label="🩺 Condition (Optional, e.g. slim, weak)")
174
-
175
- with gr.Column(visible=False) as breed_info_section:
176
- info_topic = gr.Dropdown(breed_info_topics, label="ℹ️ Select Info Type")
177
- other_info = gr.Textbox(label="✍️ Enter Info Topic", visible=False)
178
- info_topic.change(handle_info_other, info_topic, other_info)
179
-
180
- user_detail = gr.Textbox(label="πŸ“ Extra Detail (optional, max 100 chars)", max_lines=2)
181
-
182
- def show_topic_fields(selected_topic):
183
- return [
184
- gr.update(visible=(selected_topic == "Symptom Checker")),
185
- gr.update(visible=(selected_topic == "Nutrition")),
186
- gr.update(visible=(selected_topic == "Breed Info"))
187
- ]
188
-
189
- topic.change(show_topic_fields, topic, [symptom_section, nutrition_section, breed_info_section])
190
-
191
- submit_btn = gr.Button("Get Advice")
192
- output = gr.Textbox(label="πŸ’¬ AI Response", lines=15)
193
-
194
- gr.Markdown("### πŸ”„ Ask Follow-up")
195
- followup_input = gr.Textbox(label="πŸ—£οΈ Follow-up Question")
196
- followup_btn = gr.Button("Ask Follow-up")
197
- followup_output = gr.Textbox(label="πŸ’‘ Follow-up Response", lines=6)
198
-
199
- submit_btn.click(
200
- ask_ai,
201
- inputs=[pet, breed, other_breed, age, gender, weight, topic, symptom, other_symptom, condition, info_topic, other_info, user_detail],
202
- outputs=output
203
- )
204
 
205
- followup_btn.click(handle_followup, inputs=followup_input, outputs=followup_output)
 
 
 
 
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  if __name__ == "__main__":
208
  demo.launch()
 
1
  import gradio as gr
2
  import requests
3
  import os
4
+ from datetime import date
5
+ import pandas as pd
6
 
7
+ # ---------------------- API Config ----------------------
8
  API_KEY = os.getenv("AIML_API_KEY") or "your_aimlapi_key_here"
9
  API_URL = "https://api.aimlapi.com/v1/chat/completions"
10
  SYSTEM_PROMPT = (
11
  "You are a highly knowledgeable and friendly veterinary assistant AI. "
12
+ "Provide clear, thorough, and helpful responses to users about pets (dogs and cats). "
13
+ "Responses should be detailed, covering causes, symptoms, treatments, nutrition, precautions, "
14
+ "recovery time, and practical advice. Use friendly, empathetic language and encourage consulting a vet for serious concerns."
 
 
15
  )
16
 
17
+ # ---------------------- AI State ----------------------
18
  chat_history = []
19
  followup_count = 0
20
 
21
+ # ---------------------- Dropdowns ----------------------
22
  breed_options = {
23
  "Cat": ["Persian", "Siamese", "Maine Coon", "British Shorthair", "Other"],
24
  "Dog": ["German Shepherd", "Labrador", "Bulldog", "Pomeranian", "Other"]
 
32
  "Favourite Food", "Most Common Diseases", "Precautions", "Allergies", "Favourite Activities", "Other"
33
  ]
34
 
35
+ # ---------------------- Helper Functions ----------------------
36
  def update_breed(pet_type):
37
  return gr.update(choices=breed_options.get(pet_type, []), value=None, visible=True), gr.update(visible=False, value="")
38
 
 
100
  "model": "gpt-4o-mini-2024-07-18",
101
  "messages": messages,
102
  "temperature": 0.7,
103
+ "max_tokens": 500
104
  }
105
 
106
  headers = {
 
149
  except Exception as e:
150
  return f"❌ Error: {str(e)}"
151
 
152
+ # ---------------------- Vaccination Tracker ----------------------
153
+ vaccination_records = []
154
+
155
+ def save_vaccination_record(pet_type, breed, pet_name, age, last_date, total_doses, upcoming_date, vaccine_type, notes):
156
+ record = {
157
+ "Pet Type": pet_type,
158
+ "Breed": breed,
159
+ "Pet Name": pet_name,
160
+ "Age": age,
161
+ "Last Vaccination Date": last_date,
162
+ "Total Doses": total_doses,
163
+ "Upcoming Dose Date": upcoming_date,
164
+ "Vaccine Type": vaccine_type,
165
+ "Notes": notes
166
+ }
167
+ vaccination_records.append(record)
168
+ return f"βœ… Record saved for {pet_name} ({breed})"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
+ def view_all_vaccination_records():
171
+ if not vaccination_records:
172
+ return "πŸ“­ No records saved yet."
173
+ df = pd.DataFrame(vaccination_records)
174
+ return df
175
 
176
+ # ---------------------- UI ----------------------
177
+ with gr.Blocks() as demo:
178
+ gr.Markdown("## 🐾 Smart Paws: AI Assistant + Vaccination Tracker")
179
+
180
+ with gr.Tabs():
181
+ # --- Tab 1: AI Assistant ---
182
+ with gr.TabItem("🧠 Smart Paws AI Assistant"):
183
+ with gr.Row():
184
+ pet = gr.Dropdown(["Cat", "Dog"], label="πŸ• Pet Type")
185
+ breed = gr.Dropdown(label="🐾 Select Breed", visible=False)
186
+ other_breed = gr.Textbox(label="✍️ Enter Breed", visible=False)
187
+ pet.change(update_breed, pet, [breed, other_breed])
188
+ breed.change(handle_breed_other, breed, other_breed)
189
+
190
+ with gr.Row():
191
+ age = gr.Textbox(label="πŸŽ‚ Age (e.g. 2 years or 8 months)")
192
+ gender = gr.Dropdown(["Male", "Female", "Unknown"], label="🚻 Gender")
193
+ weight = gr.Textbox(label="βš–οΈ Weight (e.g. 10 kg)")
194
+
195
+ topic = gr.Dropdown(["Symptom Checker", "Nutrition", "Breed Info"], label="πŸ“š Select Topic")
196
+
197
+ with gr.Column(visible=False) as symptom_section:
198
+ symptom = gr.Dropdown(symptom_list, label="πŸ€’ Select Symptom")
199
+ other_symptom = gr.Textbox(label="✍️ Enter Symptom", visible=False)
200
+ symptom.change(handle_symptom_other, symptom, other_symptom)
201
+
202
+ with gr.Column(visible=False) as nutrition_section:
203
+ condition = gr.Textbox(label="🩺 Condition (Optional, e.g. slim, weak)")
204
+
205
+ with gr.Column(visible=False) as breed_info_section:
206
+ info_topic = gr.Dropdown(breed_info_topics, label="ℹ️ Select Info Type")
207
+ other_info = gr.Textbox(label="✍️ Enter Info Topic", visible=False)
208
+ info_topic.change(handle_info_other, info_topic, other_info)
209
+
210
+ user_detail = gr.Textbox(label="πŸ“ Extra Detail (optional, max 100 chars)", max_lines=2)
211
+
212
+ def show_topic_fields(selected_topic):
213
+ return [
214
+ gr.update(visible=(selected_topic == "Symptom Checker")),
215
+ gr.update(visible=(selected_topic == "Nutrition")),
216
+ gr.update(visible=(selected_topic == "Breed Info"))
217
+ ]
218
+
219
+ topic.change(show_topic_fields, topic, [symptom_section, nutrition_section, breed_info_section])
220
+
221
+ submit_btn = gr.Button("Get Advice")
222
+ output = gr.Textbox(label="πŸ’¬ AI Response", lines=15)
223
+
224
+ gr.Markdown("### πŸ”„ Ask Follow-up")
225
+ followup_input = gr.Textbox(label="πŸ—£οΈ Follow-up Question")
226
+ followup_btn = gr.Button("Ask Follow-up")
227
+ followup_output = gr.Textbox(label="πŸ’‘ Follow-up Response", lines=6)
228
+
229
+ submit_btn.click(
230
+ ask_ai,
231
+ inputs=[pet, breed, other_breed, age, gender, weight, topic, symptom, other_symptom, condition, info_topic, other_info, user_detail],
232
+ outputs=output
233
+ )
234
+
235
+ followup_btn.click(handle_followup, inputs=followup_input, outputs=followup_output)
236
+
237
+ # --- Tab 2: Vaccination Records ---
238
+ with gr.TabItem("πŸ’‰ Pet Vaccination Records"):
239
+ gr.Markdown("### πŸ’‰ Enter Vaccination Details")
240
+
241
+ with gr.Row():
242
+ v_pet_type = gr.Dropdown(["Dog", "Cat"], label="Pet Type")
243
+ v_breed = gr.Textbox(label="Breed")
244
+ v_pet_name = gr.Textbox(label="Pet Name")
245
+ v_age = gr.Textbox(label="Age")
246
+
247
+ with gr.Row():
248
+ v_last_vaccination = gr.Textbox(label="Last Vaccination Date (YYYY-MM-DD)")
249
+ v_total_doses = gr.Number(label="Total Doses Taken")
250
+ v_upcoming_dose = gr.Textbox(label="Upcoming Dose Date (YYYY-MM-DD)")
251
+
252
+ v_vaccine_type = gr.Dropdown(["Rabies", "DHPP", "FVRCP", "Bordetella", "Leptospirosis", "Other"], label="Vaccine Type")
253
+ v_notes = gr.Textbox(label="Additional Notes", lines=3)
254
+
255
+ save_btn = gr.Button("Save Record")
256
+ save_output = gr.Textbox(label="πŸ“‹ Status")
257
+
258
+ view_btn = gr.Button("πŸ“‘ View All Records")
259
+ table_output = gr.Dataframe(label="πŸ“Š Saved Records", visible=True)
260
+
261
+ save_btn.click(
262
+ save_vaccination_record,
263
+ inputs=[v_pet_type, v_breed, v_pet_name, v_age, v_last_vaccination, v_total_doses, v_upcoming_dose, v_vaccine_type, v_notes],
264
+ outputs=save_output
265
+ )
266
+
267
+ view_btn.click(view_all_vaccination_records, inputs=[], outputs=table_output)
268
+
269
+ # ---------------------- Run App ----------------------
270
  if __name__ == "__main__":
271
  demo.launch()