Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,7 @@ 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.
|
| 8 |
-
|
| 9 |
-
chat_history = []
|
| 10 |
-
followup_count = 0
|
| 11 |
|
| 12 |
animal_options = {
|
| 13 |
"Pet": ["Dog", "Cat", "Rabbit", "Hamster", "Parrot", "Other"],
|
|
@@ -16,15 +13,18 @@ animal_options = {
|
|
| 16 |
}
|
| 17 |
|
| 18 |
def show_only_selected(category):
|
|
|
|
|
|
|
| 19 |
return (
|
| 20 |
-
|
| 21 |
-
gr.update(visible=(category == "Pet")),
|
| 22 |
gr.update(visible=(category == "Domestic")),
|
| 23 |
gr.update(visible=(category == "Wild")),
|
| 24 |
-
gr.update(visible=True, choices=animal_options
|
| 25 |
-
gr.update(visible=False),
|
| 26 |
-
gr.update(visible=True),
|
| 27 |
-
gr.update(visible=True),
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
def update_animals(selected_animal):
|
|
@@ -32,103 +32,28 @@ def update_animals(selected_animal):
|
|
| 32 |
|
| 33 |
def back_to_home():
|
| 34 |
return (
|
| 35 |
-
gr.update(
|
| 36 |
-
gr.update(visible=True),
|
| 37 |
-
gr.update(visible=True),
|
| 38 |
-
gr.update(visible=True),
|
| 39 |
-
gr.update(visible=False, value=None),
|
| 40 |
-
gr.update(visible=False, value=""),
|
| 41 |
-
gr.update(visible=False),
|
| 42 |
-
gr.update(visible=False)
|
| 43 |
)
|
| 44 |
|
| 45 |
-
def ask_ai(category_md, selected_animal, other_animal, age, gender, height, weight, topic, user_detail):
|
| 46 |
-
global chat_history, followup_count
|
| 47 |
-
followup_count = 0
|
| 48 |
-
category = category_md.replace("**Selected Category:** ", "").strip()
|
| 49 |
-
user_detail = user_detail.strip()[:60]
|
| 50 |
-
animal = other_animal.strip() if selected_animal == "Other" else selected_animal
|
| 51 |
-
|
| 52 |
-
user_prompt = f"""Animal Category: {category}
|
| 53 |
-
Animal: {animal}
|
| 54 |
-
Age: {age}, Gender: {gender}, Height: {height}, Weight: {weight}
|
| 55 |
-
Topic: {topic}
|
| 56 |
-
Detail: {user_detail}
|
| 57 |
-
Give a short and relevant response."""
|
| 58 |
-
|
| 59 |
-
messages = [
|
| 60 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
| 61 |
-
{"role": "user", "content": user_prompt}
|
| 62 |
-
]
|
| 63 |
-
|
| 64 |
-
payload = {
|
| 65 |
-
"model": "gpt-4o-mini-2024-07-18",
|
| 66 |
-
"messages": messages,
|
| 67 |
-
"temperature": 0.7,
|
| 68 |
-
"max_tokens": 500
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
headers = {
|
| 72 |
-
"Content-Type": "application/json",
|
| 73 |
-
"Authorization": f"Bearer {API_KEY}"
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
try:
|
| 77 |
-
response = requests.post(API_URL, json=payload, headers=headers)
|
| 78 |
-
response.raise_for_status()
|
| 79 |
-
result = response.json()
|
| 80 |
-
answer = result['choices'][0]['message']['content'].strip()
|
| 81 |
-
chat_history.clear()
|
| 82 |
-
chat_history.extend(messages + [{"role": "assistant", "content": answer}])
|
| 83 |
-
return answer
|
| 84 |
-
except Exception as e:
|
| 85 |
-
return f"❌ Error: {str(e)}"
|
| 86 |
-
|
| 87 |
-
def handle_followup(user_input):
|
| 88 |
-
global chat_history, followup_count
|
| 89 |
-
if followup_count >= 5:
|
| 90 |
-
return "⚠️ Max of 5 follow-up questions reached."
|
| 91 |
-
|
| 92 |
-
chat_history.append({"role": "user", "content": user_input})
|
| 93 |
-
|
| 94 |
-
payload = {
|
| 95 |
-
"model": "gpt-4o-mini-2024-07-18",
|
| 96 |
-
"messages": chat_history,
|
| 97 |
-
"temperature": 0.5,
|
| 98 |
-
"max_tokens": 200
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
headers = {
|
| 102 |
-
"Content-Type": "application/json",
|
| 103 |
-
"Authorization": f"Bearer {API_KEY}"
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
try:
|
| 107 |
-
response = requests.post(API_URL, json=payload, headers=headers)
|
| 108 |
-
response.raise_for_status()
|
| 109 |
-
result = response.json()
|
| 110 |
-
reply = result['choices'][0]['message']['content'].strip()
|
| 111 |
-
chat_history.append({"role": "assistant", "content": reply})
|
| 112 |
-
followup_count += 1
|
| 113 |
-
return reply
|
| 114 |
-
except Exception as e:
|
| 115 |
-
return f"❌ Error: {str(e)}"
|
| 116 |
-
|
| 117 |
-
|
| 118 |
with gr.Blocks() as demo:
|
| 119 |
-
|
| 120 |
gr.Markdown("## 🐾 AI Vet Assistant")
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
-
pet_btn = gr.Button("Pet"
|
| 124 |
-
domestic_btn = gr.Button("Domestic"
|
| 125 |
-
wild_btn = gr.Button("Wild"
|
| 126 |
|
| 127 |
selected_category_markdown = gr.Markdown("", visible=False)
|
| 128 |
-
|
| 129 |
animal = gr.Dropdown(label="Select Animal", choices=[], visible=False)
|
| 130 |
other_input = gr.Textbox(label="Enter Animal", visible=False)
|
| 131 |
-
|
| 132 |
with gr.Row(visible=False) as form_row:
|
| 133 |
age = gr.Textbox(label="Age (e.g. 2 years)")
|
| 134 |
gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
|
|
@@ -141,87 +66,38 @@ with gr.Blocks() as demo:
|
|
| 141 |
submit_btn = gr.Button("Get Advice", visible=False)
|
| 142 |
output = gr.Textbox(label="AI Advice", lines=8, visible=False)
|
| 143 |
|
| 144 |
-
followup_header = gr.Markdown("### 🔄 Follow-up Questions", visible=False)
|
| 145 |
-
followup_input = gr.Textbox(label="Your Follow-up", visible=False)
|
| 146 |
-
followup_btn = gr.Button("Ask Follow-up", visible=False)
|
| 147 |
-
followup_output = gr.Textbox(label="Follow-up Response", lines=4, visible=False)
|
| 148 |
-
|
| 149 |
back_home_btn = gr.Button("Back to Home", visible=False)
|
| 150 |
|
| 151 |
-
|
|
|
|
| 152 |
selected_category_markdown,
|
| 153 |
-
pet_btn,
|
| 154 |
-
|
| 155 |
-
wild_btn,
|
| 156 |
-
animal,
|
| 157 |
-
other_input,
|
| 158 |
-
form_row,
|
| 159 |
back_home_btn
|
| 160 |
])
|
| 161 |
|
| 162 |
-
domestic_btn.click(show_only_selected, inputs=[], outputs=[
|
| 163 |
selected_category_markdown,
|
| 164 |
-
pet_btn,
|
| 165 |
-
|
| 166 |
-
wild_btn,
|
| 167 |
-
animal,
|
| 168 |
-
other_input,
|
| 169 |
-
form_row,
|
| 170 |
back_home_btn
|
| 171 |
])
|
| 172 |
|
| 173 |
-
wild_btn.click(show_only_selected, inputs=[], outputs=[
|
| 174 |
selected_category_markdown,
|
| 175 |
-
pet_btn,
|
| 176 |
-
|
| 177 |
-
wild_btn,
|
| 178 |
-
animal,
|
| 179 |
-
other_input,
|
| 180 |
-
form_row,
|
| 181 |
back_home_btn
|
| 182 |
])
|
| 183 |
|
| 184 |
animal.change(update_animals, animal, other_input)
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
followup_btn.click(handle_followup, inputs=followup_input, outputs=followup_output)
|
| 193 |
-
|
| 194 |
-
back_home_btn.click(
|
| 195 |
-
back_to_home,
|
| 196 |
-
inputs=[],
|
| 197 |
-
outputs=[
|
| 198 |
-
selected_category_markdown,
|
| 199 |
-
pet_btn,
|
| 200 |
-
domestic_btn,
|
| 201 |
-
wild_btn,
|
| 202 |
-
animal,
|
| 203 |
-
other_input,
|
| 204 |
-
form_row,
|
| 205 |
-
back_home_btn
|
| 206 |
-
]
|
| 207 |
-
)
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
gr.update(visible=show),
|
| 212 |
-
gr.update(visible=show),
|
| 213 |
-
gr.update(visible=show),
|
| 214 |
-
gr.update(visible=show),
|
| 215 |
-
gr.update(visible=show),
|
| 216 |
-
gr.update(visible=show),
|
| 217 |
-
gr.update(visible=show),
|
| 218 |
-
gr.update(visible=show),
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
-
pet_btn.click(show_form_parts, inputs=[], outputs=[topic, user_detail, submit_btn, output, followup_header, followup_input, followup_btn, followup_output])
|
| 222 |
-
domestic_btn.click(show_form_parts, inputs=[], outputs=[topic, user_detail, submit_btn, output, followup_header, followup_input, followup_btn, followup_output])
|
| 223 |
-
wild_btn.click(show_form_parts, inputs=[], outputs=[topic, user_detail, submit_btn, output, followup_header, followup_input, followup_btn, followup_output])
|
| 224 |
-
|
| 225 |
-
back_home_btn.click(lambda: show_form_parts(False), inputs=[], outputs=[topic, user_detail, submit_btn, output, followup_header, followup_input, followup_btn, followup_output])
|
| 226 |
-
|
| 227 |
-
demo.launch()
|
|
|
|
| 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..."
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
animal_options = {
|
| 10 |
"Pet": ["Dog", "Cat", "Rabbit", "Hamster", "Parrot", "Other"],
|
|
|
|
| 13 |
}
|
| 14 |
|
| 15 |
def show_only_selected(category):
|
| 16 |
+
# Return the selected category markdown text
|
| 17 |
+
# plus visibility and choices updates for UI components
|
| 18 |
return (
|
| 19 |
+
f"**Selected Category:** {category}", # Markdown text
|
| 20 |
+
gr.update(visible=(category == "Pet")), # pet_btn visible if not selected
|
| 21 |
gr.update(visible=(category == "Domestic")),
|
| 22 |
gr.update(visible=(category == "Wild")),
|
| 23 |
+
gr.update(visible=True, choices=animal_options[category], value=None), # animal dropdown
|
| 24 |
+
gr.update(visible=False), # other_input hidden initially
|
| 25 |
+
gr.update(visible=True), # form visible
|
| 26 |
+
gr.update(visible=True), # back to home visible
|
| 27 |
+
gr.update(visible=False), # category buttons hidden when one selected
|
| 28 |
)
|
| 29 |
|
| 30 |
def update_animals(selected_animal):
|
|
|
|
| 32 |
|
| 33 |
def back_to_home():
|
| 34 |
return (
|
| 35 |
+
gr.update(value="", visible=False), # selected_category_markdown
|
| 36 |
+
gr.update(visible=True), # pet_btn visible
|
| 37 |
+
gr.update(visible=True), # domestic_btn visible
|
| 38 |
+
gr.update(visible=True), # wild_btn visible
|
| 39 |
+
gr.update(visible=False, value=None), # animal dropdown hidden and cleared
|
| 40 |
+
gr.update(visible=False, value=""), # other input hidden and cleared
|
| 41 |
+
gr.update(visible=False), # form hidden
|
| 42 |
+
gr.update(visible=False) # back home hidden
|
| 43 |
)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
with gr.Blocks() as demo:
|
|
|
|
| 46 |
gr.Markdown("## 🐾 AI Vet Assistant")
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
+
pet_btn = gr.Button("Pet")
|
| 50 |
+
domestic_btn = gr.Button("Domestic")
|
| 51 |
+
wild_btn = gr.Button("Wild")
|
| 52 |
|
| 53 |
selected_category_markdown = gr.Markdown("", visible=False)
|
|
|
|
| 54 |
animal = gr.Dropdown(label="Select Animal", choices=[], visible=False)
|
| 55 |
other_input = gr.Textbox(label="Enter Animal", visible=False)
|
| 56 |
+
|
| 57 |
with gr.Row(visible=False) as form_row:
|
| 58 |
age = gr.Textbox(label="Age (e.g. 2 years)")
|
| 59 |
gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
|
|
|
|
| 66 |
submit_btn = gr.Button("Get Advice", visible=False)
|
| 67 |
output = gr.Textbox(label="AI Advice", lines=8, visible=False)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
back_home_btn = gr.Button("Back to Home", visible=False)
|
| 70 |
|
| 71 |
+
# Here is the key: pass the category name explicitly as input
|
| 72 |
+
pet_btn.click(show_only_selected, inputs=[gr.State("Pet")], outputs=[
|
| 73 |
selected_category_markdown,
|
| 74 |
+
pet_btn, domestic_btn, wild_btn,
|
| 75 |
+
animal, other_input, form_row,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
back_home_btn
|
| 77 |
])
|
| 78 |
|
| 79 |
+
domestic_btn.click(show_only_selected, inputs=[gr.State("Domestic")], outputs=[
|
| 80 |
selected_category_markdown,
|
| 81 |
+
pet_btn, domestic_btn, wild_btn,
|
| 82 |
+
animal, other_input, form_row,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
back_home_btn
|
| 84 |
])
|
| 85 |
|
| 86 |
+
wild_btn.click(show_only_selected, inputs=[gr.State("Wild")], outputs=[
|
| 87 |
selected_category_markdown,
|
| 88 |
+
pet_btn, domestic_btn, wild_btn,
|
| 89 |
+
animal, other_input, form_row,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
back_home_btn
|
| 91 |
])
|
| 92 |
|
| 93 |
animal.change(update_animals, animal, other_input)
|
| 94 |
|
| 95 |
+
back_home_btn.click(back_to_home, inputs=[], outputs=[
|
| 96 |
+
selected_category_markdown,
|
| 97 |
+
pet_btn, domestic_btn, wild_btn,
|
| 98 |
+
animal, other_input, form_row,
|
| 99 |
+
back_home_btn
|
| 100 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|