Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,57 +30,65 @@ breed_info_topics = [
|
|
| 30 |
def update_breed(pet_type):
|
| 31 |
return gr.update(choices=breed_options.get(pet_type, []), value=None, visible=True), gr.update(visible=False, value="")
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
return gr.update(visible=(
|
| 35 |
-
|
| 36 |
-
def
|
| 37 |
-
return
|
| 38 |
-
gr.update(visible=(topic == "Symptom Checker")),
|
| 39 |
-
gr.update(visible=(topic == "Nutrition")),
|
| 40 |
-
gr.update(visible=(topic == "Breed Info"))
|
| 41 |
-
]
|
| 42 |
|
| 43 |
-
def
|
| 44 |
-
|
| 45 |
-
return gr.update(interactive=False)
|
| 46 |
-
if topic == "Symptom Checker" and (not symptom or (symptom == "Other" and not other_symptom)):
|
| 47 |
-
return gr.update(interactive=False)
|
| 48 |
-
if topic == "Nutrition" and not condition:
|
| 49 |
-
return gr.update(interactive=False)
|
| 50 |
-
if topic == "Breed Info" and (not info_topic or (info_topic == "Other" and not other_info)):
|
| 51 |
-
return gr.update(interactive=False)
|
| 52 |
-
if breed == "Other" and not other_breed:
|
| 53 |
-
return gr.update(interactive=False)
|
| 54 |
-
return gr.update(interactive=True)
|
| 55 |
|
| 56 |
def ask_ai(pet, breed, other_breed, age, gender, weight, topic, symptom, other_symptom, condition, info_topic, other_info, user_detail):
|
| 57 |
global chat_history, followup_count
|
| 58 |
followup_count = 0
|
| 59 |
|
|
|
|
| 60 |
final_breed = other_breed.strip() if breed == "Other" else breed
|
| 61 |
final_symptom = other_symptom.strip() if symptom == "Other" else symptom
|
| 62 |
final_info = other_info.strip() if info_topic == "Other" else info_topic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
prompt_parts = [
|
| 65 |
-
f"Pet: {pet}",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
]
|
| 67 |
|
| 68 |
if topic == "Symptom Checker":
|
| 69 |
prompt_parts.append(f"Symptom: {final_symptom}")
|
| 70 |
-
prompt_parts.append(
|
|
|
|
|
|
|
|
|
|
| 71 |
elif topic == "Nutrition":
|
| 72 |
-
prompt_parts.append(f"Condition: {condition}")
|
| 73 |
-
prompt_parts.append(
|
|
|
|
|
|
|
|
|
|
| 74 |
elif topic == "Breed Info":
|
| 75 |
-
prompt_parts.append(f"Requested
|
| 76 |
-
prompt_parts.append(
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
if user_detail:
|
| 79 |
-
prompt_parts.append(f"
|
| 80 |
|
| 81 |
user_prompt = "\n".join(prompt_parts)
|
| 82 |
|
| 83 |
-
messages = [
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
payload = {
|
| 86 |
"model": "gpt-4o-mini-2024-07-18",
|
|
@@ -111,6 +119,7 @@ def handle_followup(user_input):
|
|
| 111 |
return "β οΈ Max of 5 follow-up questions reached."
|
| 112 |
|
| 113 |
chat_history.append({"role": "user", "content": user_input})
|
|
|
|
| 114 |
payload = {
|
| 115 |
"model": "gpt-4o-mini-2024-07-18",
|
| 116 |
"messages": chat_history,
|
|
@@ -143,43 +152,47 @@ with gr.Blocks() as demo:
|
|
| 143 |
other_breed = gr.Textbox(label="βοΈ Enter Breed", visible=False)
|
| 144 |
|
| 145 |
pet.change(update_breed, pet, [breed, other_breed])
|
| 146 |
-
breed.change(
|
| 147 |
|
| 148 |
with gr.Row():
|
| 149 |
-
age = gr.Textbox(label="π Age")
|
| 150 |
gender = gr.Dropdown(["Male", "Female", "Unknown"], label="π» Gender")
|
| 151 |
-
weight = gr.Textbox(label="βοΈ Weight (e.g.
|
| 152 |
|
| 153 |
topic = gr.Dropdown(["Symptom Checker", "Nutrition", "Breed Info"], label="π Select Topic")
|
| 154 |
|
| 155 |
with gr.Column(visible=False) as symptom_section:
|
| 156 |
symptom = gr.Dropdown(symptom_list, label="π€ Select Symptom")
|
| 157 |
other_symptom = gr.Textbox(label="βοΈ Enter Symptom", visible=False)
|
| 158 |
-
symptom.change(
|
| 159 |
|
| 160 |
with gr.Column(visible=False) as nutrition_section:
|
| 161 |
-
condition = gr.Textbox(label="π©Ί Condition")
|
| 162 |
|
| 163 |
with gr.Column(visible=False) as breed_info_section:
|
| 164 |
-
info_topic = gr.Dropdown(breed_info_topics, label="
|
| 165 |
-
other_info = gr.Textbox(label="βοΈ Enter Info", visible=False)
|
| 166 |
-
info_topic.change(
|
| 167 |
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
|
| 172 |
-
submit_btn = gr.Button("Get Advice"
|
| 173 |
output = gr.Textbox(label="π¬ AI Response", lines=15)
|
| 174 |
|
|
|
|
| 175 |
followup_input = gr.Textbox(label="π£οΈ Follow-up Question")
|
| 176 |
followup_btn = gr.Button("Ask Follow-up")
|
| 177 |
followup_output = gr.Textbox(label="π‘ Follow-up Response", lines=6)
|
| 178 |
|
| 179 |
-
inputs_to_check = [pet, breed, other_breed, age, gender, weight, topic, symptom, other_symptom, condition, info_topic, other_info]
|
| 180 |
-
for field in inputs_to_check:
|
| 181 |
-
field.change(fn=validate_inputs, inputs=inputs_to_check, outputs=submit_btn)
|
| 182 |
-
|
| 183 |
submit_btn.click(
|
| 184 |
ask_ai,
|
| 185 |
inputs=[pet, breed, other_breed, age, gender, weight, topic, symptom, other_symptom, condition, info_topic, other_info, user_detail],
|
|
|
|
| 30 |
def update_breed(pet_type):
|
| 31 |
return gr.update(choices=breed_options.get(pet_type, []), value=None, visible=True), gr.update(visible=False, value="")
|
| 32 |
|
| 33 |
+
def handle_breed_other(breed_choice):
|
| 34 |
+
return gr.update(visible=(breed_choice == "Other"), value="")
|
| 35 |
+
|
| 36 |
+
def handle_symptom_other(symptom):
|
| 37 |
+
return gr.update(visible=(symptom == "Other"), value="")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def handle_info_other(info):
|
| 40 |
+
return gr.update(visible=(info == "Other"), value="")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def ask_ai(pet, breed, other_breed, age, gender, weight, topic, symptom, other_symptom, condition, info_topic, other_info, user_detail):
|
| 43 |
global chat_history, followup_count
|
| 44 |
followup_count = 0
|
| 45 |
|
| 46 |
+
pet = pet.strip()
|
| 47 |
final_breed = other_breed.strip() if breed == "Other" else breed
|
| 48 |
final_symptom = other_symptom.strip() if symptom == "Other" else symptom
|
| 49 |
final_info = other_info.strip() if info_topic == "Other" else info_topic
|
| 50 |
+
age = age.strip()
|
| 51 |
+
gender = gender.strip()
|
| 52 |
+
weight = weight.strip()
|
| 53 |
+
condition = condition.strip()
|
| 54 |
+
user_detail = user_detail.strip()[:100]
|
| 55 |
|
| 56 |
prompt_parts = [
|
| 57 |
+
f"Pet: {pet}",
|
| 58 |
+
f"Breed: {final_breed}",
|
| 59 |
+
f"Age: {age}",
|
| 60 |
+
f"Gender: {gender}",
|
| 61 |
+
f"Weight: {weight}"
|
| 62 |
]
|
| 63 |
|
| 64 |
if topic == "Symptom Checker":
|
| 65 |
prompt_parts.append(f"Symptom: {final_symptom}")
|
| 66 |
+
prompt_parts.append(
|
| 67 |
+
"Please provide a detailed explanation including: typical causes, when and how this symptom usually appears, "
|
| 68 |
+
"recommended treatments and home care, expected recovery timeline, preventive measures, and when to seek a vet."
|
| 69 |
+
)
|
| 70 |
elif topic == "Nutrition":
|
| 71 |
+
prompt_parts.append(f"Condition: {condition if condition else 'None'}")
|
| 72 |
+
prompt_parts.append(
|
| 73 |
+
"Provide a comprehensive nutrition guide for this breed/condition: best foods available locally (Pakistan), benefits of each food, "
|
| 74 |
+
"recommended feeding quantity and frequency, and tips for maintaining optimal health."
|
| 75 |
+
)
|
| 76 |
elif topic == "Breed Info":
|
| 77 |
+
prompt_parts.append(f"Information Requested: {final_info}")
|
| 78 |
+
prompt_parts.append(
|
| 79 |
+
"Give detailed and interesting information on the selected topic related to the breed, covering key facts, practical tips, "
|
| 80 |
+
"common concerns, and any special advice."
|
| 81 |
+
)
|
| 82 |
|
| 83 |
if user_detail:
|
| 84 |
+
prompt_parts.append(f"User Detail: {user_detail}")
|
| 85 |
|
| 86 |
user_prompt = "\n".join(prompt_parts)
|
| 87 |
|
| 88 |
+
messages = [
|
| 89 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 90 |
+
{"role": "user", "content": user_prompt}
|
| 91 |
+
]
|
| 92 |
|
| 93 |
payload = {
|
| 94 |
"model": "gpt-4o-mini-2024-07-18",
|
|
|
|
| 119 |
return "β οΈ Max of 5 follow-up questions reached."
|
| 120 |
|
| 121 |
chat_history.append({"role": "user", "content": user_input})
|
| 122 |
+
|
| 123 |
payload = {
|
| 124 |
"model": "gpt-4o-mini-2024-07-18",
|
| 125 |
"messages": chat_history,
|
|
|
|
| 152 |
other_breed = gr.Textbox(label="βοΈ Enter Breed", visible=False)
|
| 153 |
|
| 154 |
pet.change(update_breed, pet, [breed, other_breed])
|
| 155 |
+
breed.change(handle_breed_other, breed, other_breed)
|
| 156 |
|
| 157 |
with gr.Row():
|
| 158 |
+
age = gr.Textbox(label="π Age (e.g. 2 years or 8 months)")
|
| 159 |
gender = gr.Dropdown(["Male", "Female", "Unknown"], label="π» Gender")
|
| 160 |
+
weight = gr.Textbox(label="βοΈ Weight (e.g. 10 kg)")
|
| 161 |
|
| 162 |
topic = gr.Dropdown(["Symptom Checker", "Nutrition", "Breed Info"], label="π Select Topic")
|
| 163 |
|
| 164 |
with gr.Column(visible=False) as symptom_section:
|
| 165 |
symptom = gr.Dropdown(symptom_list, label="π€ Select Symptom")
|
| 166 |
other_symptom = gr.Textbox(label="βοΈ Enter Symptom", visible=False)
|
| 167 |
+
symptom.change(handle_symptom_other, symptom, other_symptom)
|
| 168 |
|
| 169 |
with gr.Column(visible=False) as nutrition_section:
|
| 170 |
+
condition = gr.Textbox(label="π©Ί Condition (Optional, e.g. slim, weak)")
|
| 171 |
|
| 172 |
with gr.Column(visible=False) as breed_info_section:
|
| 173 |
+
info_topic = gr.Dropdown(breed_info_topics, label="βΉοΈ Select Info Type")
|
| 174 |
+
other_info = gr.Textbox(label="βοΈ Enter Info Topic", visible=False)
|
| 175 |
+
info_topic.change(handle_info_other, info_topic, other_info)
|
| 176 |
|
| 177 |
+
user_detail = gr.Textbox(label="π Extra Detail (optional, max 100 chars)", max_lines=2)
|
| 178 |
+
|
| 179 |
+
def show_topic_fields(selected_topic):
|
| 180 |
+
return [
|
| 181 |
+
gr.update(visible=(selected_topic == "Symptom Checker")),
|
| 182 |
+
gr.update(visible=(selected_topic == "Nutrition")),
|
| 183 |
+
gr.update(visible=(selected_topic == "Breed Info"))
|
| 184 |
+
]
|
| 185 |
|
| 186 |
+
topic.change(show_topic_fields, topic, [symptom_section, nutrition_section, breed_info_section])
|
| 187 |
|
| 188 |
+
submit_btn = gr.Button("Get Advice")
|
| 189 |
output = gr.Textbox(label="π¬ AI Response", lines=15)
|
| 190 |
|
| 191 |
+
gr.Markdown("### π Ask Follow-up")
|
| 192 |
followup_input = gr.Textbox(label="π£οΈ Follow-up Question")
|
| 193 |
followup_btn = gr.Button("Ask Follow-up")
|
| 194 |
followup_output = gr.Textbox(label="π‘ Follow-up Response", lines=6)
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
submit_btn.click(
|
| 197 |
ask_ai,
|
| 198 |
inputs=[pet, breed, other_breed, age, gender, weight, topic, symptom, other_symptom, condition, info_topic, other_info, user_detail],
|