Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
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 |
animal_options = {
|
| 10 |
"Pet": ["Dog", "Cat", "Rabbit", "Hamster", "Parrot", "Other"],
|
|
@@ -13,18 +11,20 @@ animal_options = {
|
|
| 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}", #
|
| 20 |
-
gr.update(visible=(category == "Pet")),
|
| 21 |
gr.update(visible=(category == "Domestic")),
|
| 22 |
gr.update(visible=(category == "Wild")),
|
| 23 |
-
gr.update(visible=True, choices=animal_options[category], value=None), #
|
| 24 |
-
gr.update(visible=False), #
|
| 25 |
-
gr.update(visible=True),
|
| 26 |
-
gr.update(visible=True),
|
| 27 |
-
gr.update(visible=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
def update_animals(selected_animal):
|
|
@@ -32,16 +32,26 @@ def update_animals(selected_animal):
|
|
| 32 |
|
| 33 |
def back_to_home():
|
| 34 |
return (
|
| 35 |
-
gr.update(value="", visible=False), #
|
| 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), # form
|
| 42 |
-
gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
gr.Markdown("## 🐾 AI Vet Assistant")
|
| 47 |
|
|
@@ -50,54 +60,78 @@ with gr.Blocks() as demo:
|
|
| 50 |
domestic_btn = gr.Button("Domestic")
|
| 51 |
wild_btn = gr.Button("Wild")
|
| 52 |
|
| 53 |
-
|
| 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")
|
| 60 |
height = gr.Textbox(label="Height (e.g. 30 cm)")
|
| 61 |
weight = gr.Textbox(label="Weight (e.g. 10 kg)")
|
| 62 |
|
|
|
|
| 63 |
topic = gr.Dropdown(["Health", "Nutrition", "Breed Info", "Natural Remedies"], label="Topic", visible=False)
|
| 64 |
user_detail = gr.Textbox(label="Problem Detail (max 60 characters)", max_lines=2, visible=False)
|
| 65 |
-
|
| 66 |
submit_btn = gr.Button("Get Advice", visible=False)
|
| 67 |
output = gr.Textbox(label="AI Advice", lines=8, visible=False)
|
|
|
|
| 68 |
|
| 69 |
-
|
| 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 |
-
|
| 74 |
pet_btn, domestic_btn, wild_btn,
|
| 75 |
animal, other_input, form_row,
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
])
|
| 78 |
|
| 79 |
domestic_btn.click(show_only_selected, inputs=[gr.State("Domestic")], outputs=[
|
| 80 |
-
|
| 81 |
pet_btn, domestic_btn, wild_btn,
|
| 82 |
animal, other_input, form_row,
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
])
|
| 85 |
|
| 86 |
wild_btn.click(show_only_selected, inputs=[gr.State("Wild")], outputs=[
|
| 87 |
-
|
| 88 |
pet_btn, domestic_btn, wild_btn,
|
| 89 |
animal, other_input, form_row,
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
])
|
| 92 |
|
| 93 |
-
animal.change(update_animals, animal, other_input)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
pet_btn, domestic_btn, wild_btn,
|
| 98 |
animal, other_input, form_row,
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
])
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
# Placeholder for API key
|
| 5 |
API_KEY = os.getenv("AIMLAPI_API_KEY") or "your_aimlapi_key_here"
|
|
|
|
|
|
|
| 6 |
|
| 7 |
animal_options = {
|
| 8 |
"Pet": ["Dog", "Cat", "Rabbit", "Hamster", "Parrot", "Other"],
|
|
|
|
| 11 |
}
|
| 12 |
|
| 13 |
def show_only_selected(category):
|
|
|
|
|
|
|
| 14 |
return (
|
| 15 |
+
f"**Selected Category:** {category}", # Selected category display
|
| 16 |
+
gr.update(visible=(category == "Pet")),
|
| 17 |
gr.update(visible=(category == "Domestic")),
|
| 18 |
gr.update(visible=(category == "Wild")),
|
| 19 |
+
gr.update(visible=True, choices=animal_options[category], value=None), # Animal dropdown
|
| 20 |
+
gr.update(visible=False), # Other animal input
|
| 21 |
+
gr.update(visible=True), # Info form visible
|
| 22 |
+
gr.update(visible=True), # Back button
|
| 23 |
+
gr.update(visible=True), # Gender
|
| 24 |
+
gr.update(visible=True), # Topic
|
| 25 |
+
gr.update(visible=True), # User detail (problem)
|
| 26 |
+
gr.update(visible=True), # Submit button
|
| 27 |
+
gr.update(visible=False), # Output hidden initially
|
| 28 |
)
|
| 29 |
|
| 30 |
def update_animals(selected_animal):
|
|
|
|
| 32 |
|
| 33 |
def back_to_home():
|
| 34 |
return (
|
| 35 |
+
gr.update(value="", visible=False), # Category label
|
| 36 |
+
gr.update(visible=True), # Pet btn
|
| 37 |
+
gr.update(visible=True), # Domestic btn
|
| 38 |
+
gr.update(visible=True), # Wild btn
|
| 39 |
+
gr.update(visible=False, value=None), # Animal dropdown
|
| 40 |
+
gr.update(visible=False, value=""), # Other animal input
|
| 41 |
+
gr.update(visible=False), # Info form
|
| 42 |
+
gr.update(visible=False), # Back button
|
| 43 |
+
gr.update(visible=False), # Gender
|
| 44 |
+
gr.update(visible=False), # Topic
|
| 45 |
+
gr.update(visible=False), # Problem detail
|
| 46 |
+
gr.update(visible=False), # Submit button
|
| 47 |
+
gr.update(visible=False, value="") # Output box
|
| 48 |
)
|
| 49 |
|
| 50 |
+
def ask_ai(category, animal, other, age, gender, height, weight, topic, problem):
|
| 51 |
+
final_animal = other if animal == "Other" else animal
|
| 52 |
+
prompt = f"Category: {category}\nAnimal: {final_animal}\nAge: {age}\nGender: {gender}\nHeight: {height}\nWeight: {weight}\nTopic: {topic}\nProblem: {problem}\n"
|
| 53 |
+
return f"🔍 **You asked:**\n{prompt}\n\n🤖 **AI's advice:**\nThis is a placeholder AI response for the given details."
|
| 54 |
+
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
gr.Markdown("## 🐾 AI Vet Assistant")
|
| 57 |
|
|
|
|
| 60 |
domestic_btn = gr.Button("Domestic")
|
| 61 |
wild_btn = gr.Button("Wild")
|
| 62 |
|
| 63 |
+
selected_category = gr.Markdown("", visible=False)
|
| 64 |
animal = gr.Dropdown(label="Select Animal", choices=[], visible=False)
|
| 65 |
other_input = gr.Textbox(label="Enter Animal", visible=False)
|
| 66 |
+
|
| 67 |
with gr.Row(visible=False) as form_row:
|
| 68 |
age = gr.Textbox(label="Age (e.g. 2 years)")
|
|
|
|
| 69 |
height = gr.Textbox(label="Height (e.g. 30 cm)")
|
| 70 |
weight = gr.Textbox(label="Weight (e.g. 10 kg)")
|
| 71 |
|
| 72 |
+
gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender", visible=False)
|
| 73 |
topic = gr.Dropdown(["Health", "Nutrition", "Breed Info", "Natural Remedies"], label="Topic", visible=False)
|
| 74 |
user_detail = gr.Textbox(label="Problem Detail (max 60 characters)", max_lines=2, visible=False)
|
|
|
|
| 75 |
submit_btn = gr.Button("Get Advice", visible=False)
|
| 76 |
output = gr.Textbox(label="AI Advice", lines=8, visible=False)
|
| 77 |
+
back_btn = gr.Button("Back to Home", visible=False)
|
| 78 |
|
| 79 |
+
# Button Click Events
|
|
|
|
|
|
|
| 80 |
pet_btn.click(show_only_selected, inputs=[gr.State("Pet")], outputs=[
|
| 81 |
+
selected_category,
|
| 82 |
pet_btn, domestic_btn, wild_btn,
|
| 83 |
animal, other_input, form_row,
|
| 84 |
+
back_btn,
|
| 85 |
+
gender, topic, user_detail,
|
| 86 |
+
submit_btn, output
|
| 87 |
])
|
| 88 |
|
| 89 |
domestic_btn.click(show_only_selected, inputs=[gr.State("Domestic")], outputs=[
|
| 90 |
+
selected_category,
|
| 91 |
pet_btn, domestic_btn, wild_btn,
|
| 92 |
animal, other_input, form_row,
|
| 93 |
+
back_btn,
|
| 94 |
+
gender, topic, user_detail,
|
| 95 |
+
submit_btn, output
|
| 96 |
])
|
| 97 |
|
| 98 |
wild_btn.click(show_only_selected, inputs=[gr.State("Wild")], outputs=[
|
| 99 |
+
selected_category,
|
| 100 |
pet_btn, domestic_btn, wild_btn,
|
| 101 |
animal, other_input, form_row,
|
| 102 |
+
back_btn,
|
| 103 |
+
gender, topic, user_detail,
|
| 104 |
+
submit_btn, output
|
| 105 |
])
|
| 106 |
|
| 107 |
+
animal.change(update_animals, inputs=animal, outputs=other_input)
|
| 108 |
|
| 109 |
+
back_btn.click(back_to_home, outputs=[
|
| 110 |
+
selected_category,
|
| 111 |
pet_btn, domestic_btn, wild_btn,
|
| 112 |
animal, other_input, form_row,
|
| 113 |
+
back_btn,
|
| 114 |
+
gender, topic, user_detail,
|
| 115 |
+
submit_btn, output
|
| 116 |
])
|
| 117 |
|
| 118 |
+
submit_btn.click(
|
| 119 |
+
ask_ai,
|
| 120 |
+
inputs=[
|
| 121 |
+
selected_category,
|
| 122 |
+
animal,
|
| 123 |
+
other_input,
|
| 124 |
+
age,
|
| 125 |
+
gender,
|
| 126 |
+
height,
|
| 127 |
+
weight,
|
| 128 |
+
topic,
|
| 129 |
+
user_detail
|
| 130 |
+
],
|
| 131 |
+
outputs=output
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
output.change(lambda x: gr.update(visible=True), inputs=output, outputs=output)
|
| 135 |
+
|
| 136 |
if __name__ == "__main__":
|
| 137 |
demo.launch()
|