Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,13 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
API_KEY = os.getenv("
|
| 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 |
"Provide clear, thorough, and helpful responses to users about pets (dogs and cats). "
|
| 10 |
-
"Responses should be detailed
|
| 11 |
-
"
|
| 12 |
)
|
| 13 |
|
| 14 |
chat_history = []
|
|
@@ -63,13 +63,22 @@ def ask_ai(pet, breed, other_breed, age, gender, weight, topic, symptom, other_s
|
|
| 63 |
|
| 64 |
if topic == "Symptom Checker":
|
| 65 |
prompt_parts.append(f"Symptom: {final_symptom}")
|
| 66 |
-
prompt_parts.append(
|
|
|
|
|
|
|
|
|
|
| 67 |
elif topic == "Nutrition":
|
| 68 |
prompt_parts.append(f"Condition: {condition if condition else 'None'}")
|
| 69 |
-
prompt_parts.append(
|
|
|
|
|
|
|
|
|
|
| 70 |
elif topic == "Breed Info":
|
| 71 |
prompt_parts.append(f"Information Requested: {final_info}")
|
| 72 |
-
prompt_parts.append(
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
if user_detail:
|
| 75 |
prompt_parts.append(f"User Detail: {user_detail}")
|
|
@@ -85,7 +94,7 @@ def ask_ai(pet, breed, other_breed, age, gender, weight, topic, symptom, other_s
|
|
| 85 |
"model": "gpt-4o-mini-2024-07-18",
|
| 86 |
"messages": messages,
|
| 87 |
"temperature": 0.7,
|
| 88 |
-
"max_tokens":
|
| 89 |
}
|
| 90 |
|
| 91 |
headers = {
|
|
@@ -134,39 +143,38 @@ def handle_followup(user_input):
|
|
| 134 |
except Exception as e:
|
| 135 |
return f"β Error: {str(e)}"
|
| 136 |
|
| 137 |
-
# Launch UI
|
| 138 |
with gr.Blocks() as demo:
|
| 139 |
gr.Markdown("## πΎ Smart Paws AI Assistant")
|
| 140 |
|
| 141 |
with gr.Row():
|
| 142 |
-
pet = gr.Dropdown(["Cat", "Dog"], label="Pet Type")
|
| 143 |
-
breed = gr.Dropdown(label="Select Breed", visible=False)
|
| 144 |
-
other_breed = gr.Textbox(label="Enter Breed", visible=False)
|
| 145 |
|
| 146 |
pet.change(update_breed, pet, [breed, other_breed])
|
| 147 |
breed.change(handle_breed_other, breed, other_breed)
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
-
age = gr.Textbox(label="Age (e.g. 2 years or 8 months)")
|
| 151 |
-
gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
|
| 152 |
-
weight = gr.Textbox(label="Weight (e.g. 10 kg)")
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
topic = gr.Dropdown(["Symptom Checker", "Nutrition", "Breed Info"], label="Select Topic")
|
| 155 |
-
|
| 156 |
with gr.Column(visible=False) as symptom_section:
|
| 157 |
-
symptom = gr.Dropdown(symptom_list, label="Select Symptom")
|
| 158 |
-
other_symptom = gr.Textbox(label="Enter Symptom", visible=False)
|
| 159 |
symptom.change(handle_symptom_other, symptom, other_symptom)
|
| 160 |
|
| 161 |
with gr.Column(visible=False) as nutrition_section:
|
| 162 |
-
condition = gr.Textbox(label="Condition (Optional, e.g. slim, weak)")
|
| 163 |
|
| 164 |
with gr.Column(visible=False) as breed_info_section:
|
| 165 |
-
info_topic = gr.Dropdown(breed_info_topics, label="Select Info Type")
|
| 166 |
-
other_info = gr.Textbox(label="Enter Info Topic", visible=False)
|
| 167 |
info_topic.change(handle_info_other, info_topic, other_info)
|
| 168 |
|
| 169 |
-
user_detail = gr.Textbox(label="Extra Detail (optional, max 100 chars)", max_lines=2)
|
| 170 |
|
| 171 |
def show_topic_fields(selected_topic):
|
| 172 |
return [
|
|
@@ -178,12 +186,12 @@ with gr.Blocks() as demo:
|
|
| 178 |
topic.change(show_topic_fields, topic, [symptom_section, nutrition_section, breed_info_section])
|
| 179 |
|
| 180 |
submit_btn = gr.Button("Get Advice")
|
| 181 |
-
output = gr.Textbox(label="AI Response", lines=
|
| 182 |
|
| 183 |
gr.Markdown("### π Ask Follow-up")
|
| 184 |
-
followup_input = gr.Textbox(label="Follow-up Question")
|
| 185 |
followup_btn = gr.Button("Ask Follow-up")
|
| 186 |
-
followup_output = gr.Textbox(label="Follow-up Response", lines=
|
| 187 |
|
| 188 |
submit_btn.click(
|
| 189 |
ask_ai,
|
|
|
|
| 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 |
"Provide clear, thorough, and helpful responses to users about pets (dogs and cats). "
|
| 10 |
+
"Responses should be detailed, covering causes, symptoms, treatments, nutrition, precautions, "
|
| 11 |
+
"recovery time, and practical advice. Use friendly, empathetic language and encourage consulting a vet for serious concerns."
|
| 12 |
)
|
| 13 |
|
| 14 |
chat_history = []
|
|
|
|
| 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}")
|
|
|
|
| 94 |
"model": "gpt-4o-mini-2024-07-18",
|
| 95 |
"messages": messages,
|
| 96 |
"temperature": 0.7,
|
| 97 |
+
"max_tokens": 900
|
| 98 |
}
|
| 99 |
|
| 100 |
headers = {
|
|
|
|
| 143 |
except Exception as e:
|
| 144 |
return f"β Error: {str(e)}"
|
| 145 |
|
|
|
|
| 146 |
with gr.Blocks() as demo:
|
| 147 |
gr.Markdown("## πΎ Smart Paws AI Assistant")
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
+
pet = gr.Dropdown(["Cat", "Dog"], label="π Pet Type")
|
| 151 |
+
breed = gr.Dropdown(label="πΎ Select Breed", visible=False)
|
| 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 [
|
|
|
|
| 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,
|