Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
"
|
| 10 |
-
"
|
| 11 |
-
"
|
| 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":
|
| 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 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|