chburhan64 commited on
Commit
c5417b4
·
verified ·
1 Parent(s): 89fbd3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -40
app.py CHANGED
@@ -2,51 +2,45 @@ 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 health, nutrition, breeds, and symptoms. Keep it simple and consult a vet in critical cases."
8
 
9
  chat_history = []
10
  followup_count = 0
11
 
12
  breeds = {
13
- "Cat": ["Persian", "Maine Coon", "Siamese", "Turkish Van"],
14
- "Dog": ["German Shepherd", "Labrador", "Bulldog", "Pomeranian"]
15
  }
16
 
17
- symptoms_list = ["Vomiting", "Loss of Appetite", "Limping", "Itchy Skin", "Coughing", "Diarrhea", "Lethargy"]
18
-
19
  def update_breeds(pet):
20
- return gr.update(choices=breeds.get(pet, []), visible=True, value=None)
21
-
22
- def update_ui_by_topic(topic):
23
- symptom_visible = topic == "Symptom Checker"
24
- condition_visible = topic == "Nutrition"
25
- breed_info = topic == "Breed Info"
26
- return (
27
- gr.update(visible=symptom_visible),
28
- gr.update(visible=condition_visible),
29
- gr.update(visible=breed_info)
30
- )
31
 
32
- def ask_ai(pet, breed, age, gender, weight, topic, symptom, condition, user_detail):
33
  global chat_history, followup_count
34
  followup_count = 0
35
  user_detail = user_detail.strip()[:60]
 
 
 
 
 
 
36
 
37
  if topic == "Symptom Checker":
38
- user_prompt = f"Pet: {pet}, Breed: {breed}, Age: {age}, Gender: {gender}, Symptom: {symptom}\nDiagnose possible diseases and suggest if vet is needed."
39
  elif topic == "Nutrition":
40
- user_prompt = f"Pet: {pet}, Breed: {breed}, Age: {age}, Gender: {gender}, Weight: {weight}, Condition: {condition}\nSuggest appropriate diet and supplements in Pakistan."
41
- elif topic == "Breed Info":
42
- user_prompt = f"Give detailed breed info for a {breed} {pet} including favorite food, common diseases, allergies, activity level, precautions, grooming needs, and health care."
43
-
44
- if user_detail:
45
- user_prompt += f"\nExtra detail: {user_detail}"
46
 
47
  messages = [
48
  {"role": "system", "content": SYSTEM_PROMPT},
49
- {"role": "user", "content": user_prompt}
50
  ]
51
 
52
  payload = {
@@ -76,21 +70,17 @@ def handle_followup(user_input):
76
  global chat_history, followup_count
77
  if followup_count >= 5:
78
  return "⚠️ Max of 5 follow-up questions reached."
79
-
80
  chat_history.append({"role": "user", "content": user_input})
81
-
82
  payload = {
83
  "model": "gpt-4o-mini-2024-07-18",
84
  "messages": chat_history,
85
  "temperature": 0.5,
86
  "max_tokens": 200
87
  }
88
-
89
  headers = {
90
  "Content-Type": "application/json",
91
  "Authorization": f"Bearer {API_KEY}"
92
  }
93
-
94
  try:
95
  response = requests.post(API_URL, json=payload, headers=headers)
96
  response.raise_for_status()
@@ -102,29 +92,35 @@ def handle_followup(user_input):
102
  except Exception as e:
103
  return f"❌ Error: {str(e)}"
104
 
105
- # Launch app
106
  with gr.Blocks() as demo:
107
  gr.Markdown("## 🐾 Smart Paws AI Assistant")
108
 
109
  with gr.Row():
110
  pet = gr.Dropdown(["Cat", "Dog"], label="Pet")
111
- breed = gr.Dropdown(label="Select Breed")
 
112
 
113
- pet.change(update_breeds, pet, breed)
 
114
 
115
  with gr.Row():
116
- age = gr.Textbox(label="Age (e.g. 2 years or 6 months)")
117
  gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
118
  weight = gr.Textbox(label="Weight (e.g. 10 kg)")
119
 
120
  topic = gr.Dropdown(["Symptom Checker", "Nutrition", "Breed Info"], label="Topic")
121
- symptom = gr.Dropdown(choices=symptoms_list, label="Select Symptom", visible=False)
122
- condition = gr.Textbox(label="Condition (optional - e.g. weak, slim)", visible=False)
123
- breed_info_trigger = gr.Textbox(visible=False)
124
 
125
- topic.change(update_ui_by_topic, topic, [symptom, condition, breed_info_trigger])
 
 
 
 
 
 
126
 
127
- user_detail = gr.Textbox(label="Extra Detail (optional, max 60 characters)", max_lines=2)
128
  submit_btn = gr.Button("Get Advice")
129
  output = gr.Textbox(label="AI Advice", lines=8)
130
 
@@ -135,9 +131,10 @@ with gr.Blocks() as demo:
135
 
136
  submit_btn.click(
137
  ask_ai,
138
- inputs=[pet, breed, age, gender, weight, topic, symptom, condition, user_detail],
139
  outputs=output
140
  )
 
141
  followup_btn.click(handle_followup, inputs=followup_input, outputs=followup_output)
142
 
143
  if __name__ == "__main__":
 
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 conditions. Keep it simple and consult a vet in critical cases."
8
 
9
  chat_history = []
10
  followup_count = 0
11
 
12
  breeds = {
13
+ "Cat": ["Persian", "Maine Coon", "Siamese", "Turkish Van", "Other"],
14
+ "Dog": ["German Shepherd", "Labrador", "Bulldog", "Pomeranian", "Other"]
15
  }
16
 
 
 
17
  def update_breeds(pet):
18
+ options = breeds.get(pet, [])
19
+ return gr.update(choices=options, visible=True, value=None), gr.update(visible=False, value="")
20
+
21
+ def show_custom_breed(breed_selected):
22
+ return gr.update(visible=(breed_selected == "Other"), value="")
 
 
 
 
 
 
23
 
24
+ def ask_ai(pet, breed, custom_breed, age, gender, weight, topic, symptom, condition, user_detail):
25
  global chat_history, followup_count
26
  followup_count = 0
27
  user_detail = user_detail.strip()[:60]
28
+ breed_name = custom_breed if breed == "Other" and custom_breed else breed
29
+
30
+ prompt = f"""Pet: {pet}
31
+ Breed: {breed_name}
32
+ Age: {age}, Gender: {gender}, Weight: {weight}
33
+ Topic: {topic}"""
34
 
35
  if topic == "Symptom Checker":
36
+ prompt += f"\nSymptom: {symptom}"
37
  elif topic == "Nutrition":
38
+ prompt += f"\nCondition: {condition}"
39
+ prompt += f"\nDetail: {user_detail}\nGive a short and relevant response. If critical, recommend a vet visit."
 
 
 
 
40
 
41
  messages = [
42
  {"role": "system", "content": SYSTEM_PROMPT},
43
+ {"role": "user", "content": prompt}
44
  ]
45
 
46
  payload = {
 
70
  global chat_history, followup_count
71
  if followup_count >= 5:
72
  return "⚠️ Max of 5 follow-up questions reached."
 
73
  chat_history.append({"role": "user", "content": user_input})
 
74
  payload = {
75
  "model": "gpt-4o-mini-2024-07-18",
76
  "messages": chat_history,
77
  "temperature": 0.5,
78
  "max_tokens": 200
79
  }
 
80
  headers = {
81
  "Content-Type": "application/json",
82
  "Authorization": f"Bearer {API_KEY}"
83
  }
 
84
  try:
85
  response = requests.post(API_URL, json=payload, headers=headers)
86
  response.raise_for_status()
 
92
  except Exception as e:
93
  return f"❌ Error: {str(e)}"
94
 
 
95
  with gr.Blocks() as demo:
96
  gr.Markdown("## 🐾 Smart Paws AI Assistant")
97
 
98
  with gr.Row():
99
  pet = gr.Dropdown(["Cat", "Dog"], label="Pet")
100
+ breed = gr.Dropdown(label="Select Breed", visible=False)
101
+ custom_breed = gr.Textbox(label="Enter Breed Name", visible=False)
102
 
103
+ pet.change(update_breeds, pet, [breed, custom_breed])
104
+ breed.change(show_custom_breed, breed, custom_breed)
105
 
106
  with gr.Row():
107
+ age = gr.Textbox(label="Age (e.g. 2 years or 8 months)")
108
  gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
109
  weight = gr.Textbox(label="Weight (e.g. 10 kg)")
110
 
111
  topic = gr.Dropdown(["Symptom Checker", "Nutrition", "Breed Info"], label="Topic")
112
+ symptom = gr.Dropdown(["Vomiting", "Coughing", "Itching", "Diarrhea", "Loss of Appetite", "Limping", "Sneezing"], label="Select Symptom", visible=False)
113
+ condition = gr.Textbox(label="Condition (optional: slim, weak, etc.)", visible=False)
114
+ user_detail = gr.Textbox(label="Problem Detail (max 60 characters)", max_lines=2)
115
 
116
+ def toggle_topic_fields(selected_topic):
117
+ return (
118
+ gr.update(visible=(selected_topic == "Symptom Checker")),
119
+ gr.update(visible=(selected_topic == "Nutrition"))
120
+ )
121
+
122
+ topic.change(toggle_topic_fields, topic, [symptom, condition])
123
 
 
124
  submit_btn = gr.Button("Get Advice")
125
  output = gr.Textbox(label="AI Advice", lines=8)
126
 
 
131
 
132
  submit_btn.click(
133
  ask_ai,
134
+ inputs=[pet, breed, custom_breed, age, gender, weight, topic, symptom, condition, user_detail],
135
  outputs=output
136
  )
137
+
138
  followup_btn.click(handle_followup, inputs=followup_input, outputs=followup_output)
139
 
140
  if __name__ == "__main__":