Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,13 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
animal_options = {
|
| 8 |
"Pet": ["Dog", "Cat", "Rabbit", "Hamster", "Parrot", "Other"],
|
|
@@ -10,128 +15,147 @@ animal_options = {
|
|
| 10 |
"Wild": ["Tiger", "Lion", "Elephant", "Deer", "Bear", "Leopard", "Wolf", "Giraffe", "Other"]
|
| 11 |
}
|
| 12 |
|
| 13 |
-
def
|
|
|
|
| 14 |
return (
|
| 15 |
-
|
| 16 |
-
gr.update(visible=
|
| 17 |
-
gr.update(visible=
|
| 18 |
-
gr.update(visible=
|
| 19 |
-
gr.update(visible=
|
| 20 |
-
gr.update(visible=False), #
|
| 21 |
-
gr.update(visible=
|
| 22 |
-
gr.update(visible=True
|
| 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
|
| 31 |
-
return gr.update(visible=(
|
| 32 |
-
|
| 33 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 40 |
-
gr.update(visible=False
|
| 41 |
-
gr.update(visible=False), #
|
| 42 |
-
gr.update(visible=False
|
| 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
|
|
|
|
|
|
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
-
pet_btn = gr.Button("
|
| 60 |
domestic_btn = gr.Button("Domestic")
|
| 61 |
wild_btn = gr.Button("Wild")
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
domestic_btn.click(
|
| 90 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
demo.launch()
|
|
|
|
| 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. Give short but useful answers regarding pet and animal health, nutrition, breeds, and natural remedies. Keep it simple and consult a vet in critical cases."
|
| 8 |
+
|
| 9 |
+
chat_history = []
|
| 10 |
+
followup_count = 0
|
| 11 |
|
| 12 |
animal_options = {
|
| 13 |
"Pet": ["Dog", "Cat", "Rabbit", "Hamster", "Parrot", "Other"],
|
|
|
|
| 15 |
"Wild": ["Tiger", "Lion", "Elephant", "Deer", "Bear", "Leopard", "Wolf", "Giraffe", "Other"]
|
| 16 |
}
|
| 17 |
|
| 18 |
+
def update_animals(category):
|
| 19 |
+
animals = animal_options.get(category, [])
|
| 20 |
return (
|
| 21 |
+
gr.update(choices=animals, visible=True, value=None),
|
| 22 |
+
gr.update(visible=False, value=""),
|
| 23 |
+
gr.update(visible=True), # form visible
|
| 24 |
+
gr.update(visible=True), # back button visible
|
| 25 |
+
gr.update(visible=False), # Pet button hidden
|
| 26 |
+
gr.update(visible=False), # Domestic button hidden
|
| 27 |
+
gr.update(visible=False), # Wild button hidden
|
| 28 |
+
gr.update(visible=True, value=category) # Show only selected category at top
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
+
def handle_other_animal(selected):
|
| 32 |
+
return gr.update(visible=(selected == "Other"), value="")
|
| 33 |
+
|
| 34 |
+
def ask_ai(category, selected_animal, other_animal, age, gender, height, weight, topic, user_detail):
|
| 35 |
+
global chat_history, followup_count
|
| 36 |
+
followup_count = 0
|
| 37 |
+
user_detail = user_detail.strip()[:60]
|
| 38 |
+
animal = other_animal.strip() if selected_animal == "Other" else selected_animal
|
| 39 |
+
|
| 40 |
+
user_prompt = f"""Animal Category: {category}
|
| 41 |
+
Animal: {animal}
|
| 42 |
+
Age: {age}, Gender: {gender}, Height: {height}, Weight: {weight}
|
| 43 |
+
Topic: {topic}
|
| 44 |
+
Detail: {user_detail}
|
| 45 |
+
Give a short and relevant response."""
|
| 46 |
+
|
| 47 |
+
messages = [
|
| 48 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 49 |
+
{"role": "user", "content": user_prompt}
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
payload = {
|
| 53 |
+
"model": "gpt-4o-mini-2024-07-18",
|
| 54 |
+
"messages": messages,
|
| 55 |
+
"temperature": 0.7,
|
| 56 |
+
"max_tokens": 500
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
headers = {
|
| 60 |
+
"Content-Type": "application/json",
|
| 61 |
+
"Authorization": f"Bearer {API_KEY}"
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
response = requests.post(API_URL, json=payload, headers=headers)
|
| 66 |
+
response.raise_for_status()
|
| 67 |
+
result = response.json()
|
| 68 |
+
answer = result['choices'][0]['message']['content'].strip()
|
| 69 |
+
chat_history.clear()
|
| 70 |
+
chat_history.extend(messages + [{"role": "assistant", "content": answer}])
|
| 71 |
+
return answer
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"❌ Error: {str(e)}"
|
| 74 |
+
|
| 75 |
+
def handle_followup(user_input):
|
| 76 |
+
global chat_history, followup_count
|
| 77 |
+
if followup_count >= 5:
|
| 78 |
+
return "⚠️ Max of 5 follow-up questions reached."
|
| 79 |
+
|
| 80 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 81 |
+
|
| 82 |
+
payload = {
|
| 83 |
+
"model": "gpt-4o-mini-2024-07-18",
|
| 84 |
+
"messages": chat_history,
|
| 85 |
+
"temperature": 0.5,
|
| 86 |
+
"max_tokens": 200
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
headers = {
|
| 90 |
+
"Content-Type": "application/json",
|
| 91 |
+
"Authorization": f"Bearer {API_KEY}"
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
response = requests.post(API_URL, json=payload, headers=headers)
|
| 96 |
+
response.raise_for_status()
|
| 97 |
+
result = response.json()
|
| 98 |
+
reply = result['choices'][0]['message']['content'].strip()
|
| 99 |
+
chat_history.append({"role": "assistant", "content": reply})
|
| 100 |
+
followup_count += 1
|
| 101 |
+
return reply
|
| 102 |
+
except Exception as e:
|
| 103 |
+
return f"❌ Error: {str(e)}"
|
| 104 |
+
|
| 105 |
+
def go_home():
|
| 106 |
return (
|
| 107 |
+
gr.update(visible=False), # form hidden
|
| 108 |
+
gr.update(visible=True), # Pet visible
|
| 109 |
+
gr.update(visible=True), # Domestic visible
|
| 110 |
+
gr.update(visible=True), # Wild visible
|
| 111 |
+
gr.update(visible=False), # animal dropdown
|
| 112 |
+
gr.update(visible=False), # other input
|
| 113 |
+
gr.update(visible=False), # back button
|
| 114 |
+
gr.update(visible=False, value="") # selected category label hidden
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
)
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
with gr.Blocks() as demo:
|
| 118 |
+
gr.Markdown("## 🐾 AI Animal Assistant")
|
| 119 |
+
|
| 120 |
+
selected_label = gr.Textbox(visible=False, interactive=False)
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
+
pet_btn = gr.Button("Pets")
|
| 124 |
domestic_btn = gr.Button("Domestic")
|
| 125 |
wild_btn = gr.Button("Wild")
|
| 126 |
|
| 127 |
+
with gr.Column(visible=False) as form_section:
|
| 128 |
+
category = selected_label
|
| 129 |
+
animal = gr.Dropdown(label="Select Animal")
|
| 130 |
+
other_input = gr.Textbox(label="Enter Animal", visible=False)
|
| 131 |
+
animal.change(handle_other_animal, animal, other_input)
|
| 132 |
+
|
| 133 |
+
with gr.Row():
|
| 134 |
+
age = gr.Textbox(label="Age (e.g. 2 years)")
|
| 135 |
+
gender = gr.Dropdown(["Male", "Female", "Unknown"], label="Gender")
|
| 136 |
+
height = gr.Textbox(label="Height (e.g. 30 cm)")
|
| 137 |
+
weight = gr.Textbox(label="Weight (e.g. 10 kg)")
|
| 138 |
+
|
| 139 |
+
topic = gr.Dropdown(["Health", "Nutrition", "Breed Info", "Natural Remedies"], label="Topic")
|
| 140 |
+
user_detail = gr.Textbox(label="Problem Detail (max 60 characters)", max_lines=2)
|
| 141 |
+
|
| 142 |
+
submit_btn = gr.Button("Get Advice")
|
| 143 |
+
output = gr.Textbox(label="AI Advice", lines=8)
|
| 144 |
+
|
| 145 |
+
gr.Markdown("### 🔄 Follow-up Questions")
|
| 146 |
+
followup_input = gr.Textbox(label="Your Follow-up")
|
| 147 |
+
followup_btn = gr.Button("Ask Follow-up")
|
| 148 |
+
followup_output = gr.Textbox(label="Follow-up Response", lines=4)
|
| 149 |
+
|
| 150 |
+
back_btn = gr.Button("🔙 Back to Home")
|
| 151 |
+
|
| 152 |
+
pet_btn.click(fn=update_animals, inputs=[pet_btn], outputs=[animal, other_input, form_section, back_btn, pet_btn, domestic_btn, wild_btn, selected_label], show_progress=False)
|
| 153 |
+
domestic_btn.click(fn=update_animals, inputs=[domestic_btn], outputs=[animal, other_input, form_section, back_btn, pet_btn, domestic_btn, wild_btn, selected_label], show_progress=False)
|
| 154 |
+
wild_btn.click(fn=update_animals, inputs=[wild_btn], outputs=[animal, other_input, form_section, back_btn, pet_btn, domestic_btn, wild_btn, selected_label], show_progress=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
+
submit_btn.click(ask_ai, inputs=[category, animal, other_input, age, gender, height, weight, topic, user_detail], outputs=output)
|
| 157 |
+
followup_btn.click(handle_followup, inputs=followup_input, outputs=followup_output)
|
| 158 |
+
back_btn.click(fn=go_home, outputs=[form_section, pet_btn, domestic_btn, wild_btn, animal, other_input, back_btn, selected_label], show_progress=False)
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
demo.launch()
|