Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,38 +2,47 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
API_KEY = os.getenv("AIMLAPI_API_KEY") or "
|
| 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
|
| 8 |
|
| 9 |
chat_history = []
|
| 10 |
followup_count = 0
|
| 11 |
|
| 12 |
-
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
"Wild": ["Tiger", "Lion", "Elephant", "Deer", "Bear", "Leopard", "Wolf", "Giraffe", "Other"]
|
| 16 |
}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
animals = animal_options.get(category, [])
|
| 20 |
-
return gr.update(choices=animals, visible=True, value=None), gr.update(visible=False, value="")
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
return gr.update(
|
| 24 |
|
| 25 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
Give a
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
messages = [
|
| 39 |
{"role": "system", "content": SYSTEM_PROMPT},
|
|
@@ -95,25 +104,27 @@ def handle_followup(user_input):
|
|
| 95 |
|
| 96 |
# Launch app
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
-
gr.Markdown("## 🐾 AI
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
other_input = gr.Textbox(label="Enter Animal", visible=False)
|
| 104 |
|
| 105 |
-
|
| 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(["
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
|
|
|
| 117 |
submit_btn = gr.Button("Get Advice")
|
| 118 |
output = gr.Textbox(label="AI Advice", lines=8)
|
| 119 |
|
|
@@ -122,7 +133,11 @@ with gr.Blocks() as demo:
|
|
| 122 |
followup_btn = gr.Button("Ask Follow-up")
|
| 123 |
followup_output = gr.Textbox(label="Follow-up Response", lines=4)
|
| 124 |
|
| 125 |
-
submit_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
followup_btn.click(handle_followup, inputs=followup_input, outputs=followup_output)
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
|
|
|
| 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},
|
|
|
|
| 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 |
|
|
|
|
| 133 |
followup_btn = gr.Button("Ask Follow-up")
|
| 134 |
followup_output = gr.Textbox(label="Follow-up Response", lines=4)
|
| 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__":
|