nivra-ai-agent / app.py
datdevsteve's picture
Update app.py
12788e5 verified
import os
import uuid
import gradio as gr
from PIL import Image
from nivra_agent import nivra_chat, nivra_vision
# ==================================================
# Constants
# ==================================================
UPLOAD_DIR = "/tmp/uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
SPACE_HOST = os.environ.get(
"SPACE_HOST",
"https://datdevsteve-nivra-ai-agent.hf.space"
)
# ==================================================
# CHAT ENDPOINT
# ==================================================
def chat_fn(message, history):
history = history or []
history.append({"role": "user", "content": message})
response = nivra_chat(message, history)
history.append({"role": "assistant", "content": response})
return history, ""
# ==================================================
# 🆕 VISION ENDPOINT (FILE-BASED, NO BASE64)
# ==================================================
def vision_fn(image_url: str, hint_text: str):
return nivra_vision(image_url, hint_text)
# ==================================================
# UI + API
# ==================================================
with gr.Blocks(title="🩺 Nivra AI Agent") as demo:
gr.Markdown(
"# 🩺 Nivra AI Agent\n"
"_AI-powered healthcare assistant for preliminary guidance._"
)
# -------------------------------
# Chat UI
# -------------------------------
chatbot = gr.Chatbot(show_label=False)
txt = gr.Textbox(
placeholder="Describe your symptoms (e.g. fever, headache, rash)..."
)
send = gr.Button("Send")
send.click(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
txt.submit(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
# -------------------------------
# 🔒 Hidden Vision API (Flutter)
# -------------------------------
gr.Button(visible=False).click(
vision_fn,
inputs=[
gr.Textbox(label="image_url"),
gr.Textbox(label="hint_text"),
],
outputs=[gr.Textbox()],
api_name="vision_fn",
)
# ==================================================
# Launch (HF-safe)
# ==================================================
demo.launch(
server_name="0.0.0.0",
server_port=7860,
ssr_mode=False,
show_error=True
)