Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from nivra_agent import nivra_chat
|
| 3 |
from indicTrans3Infer import translate_text
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def chat_fn(message, history):
|
| 7 |
history = history or []
|
| 8 |
-
|
| 9 |
-
# Add user message
|
| 10 |
-
history.append({
|
| 11 |
-
"role": "user",
|
| 12 |
-
"content": message
|
| 13 |
-
})
|
| 14 |
-
|
| 15 |
-
# Get assistant response
|
| 16 |
response = nivra_chat(message, history)
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"content": response
|
| 22 |
-
})
|
| 23 |
|
| 24 |
-
# Return updated history + clear textbox
|
| 25 |
-
return history, ""
|
| 26 |
-
|
| 27 |
def indictrans_translate(text: str, target_language: str):
|
| 28 |
-
|
| 29 |
-
Gradio-exposed translation endpoint
|
| 30 |
-
"""
|
| 31 |
-
translated = translate_text(text, target_language)
|
| 32 |
-
return translated
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"""
|
| 38 |
-
# 🩺 Nivra AI Agent
|
| 39 |
-
_AI-powered healthcare assistant for preliminary medical guidance._
|
| 40 |
-
"""
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
chatbot = gr.Chatbot(
|
| 44 |
-
elem_id="chatbot",
|
| 45 |
-
show_label=False,
|
| 46 |
-
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
translate_output = gr.Textbox(visible=False)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
translate_btn.click(
|
| 56 |
-
indictrans_translate,
|
| 57 |
-
inputs=[translate_input, translate_lang],
|
| 58 |
-
outputs=[translate_output],
|
| 59 |
-
api_name="indictrans_translate"
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
show_label=False,
|
| 66 |
-
scale=8,
|
| 67 |
-
)
|
| 68 |
-
send = gr.Button("Send", scale=1)
|
| 69 |
|
| 70 |
send.click(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
|
| 71 |
txt.submit(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
demo.launch(
|
| 78 |
server_name="0.0.0.0",
|
| 79 |
server_port=7860,
|
| 80 |
-
ssr_mode=False,
|
| 81 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from nivra_agent import nivra_chat, nivra_vision
|
| 3 |
from indicTrans3Infer import translate_text
|
| 4 |
|
| 5 |
+
# ===============================
|
| 6 |
+
# Chat endpoint
|
| 7 |
+
# ===============================
|
| 8 |
|
| 9 |
def chat_fn(message, history):
|
| 10 |
history = history or []
|
| 11 |
+
history.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
response = nivra_chat(message, history)
|
| 13 |
+
history.append({"role": "assistant", "content": response})
|
| 14 |
+
return history, ""
|
| 15 |
|
| 16 |
+
# ===============================
|
| 17 |
+
# Translation endpoint
|
| 18 |
+
# ===============================
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
def indictrans_translate(text: str, target_language: str):
|
| 21 |
+
return translate_text(text, target_language)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# ===============================
|
| 24 |
+
# 🆕 Vision endpoint
|
| 25 |
+
# ===============================
|
| 26 |
|
| 27 |
+
def vision_fn(image_base64: str, hint_text: str):
|
| 28 |
+
return nivra_vision(image_base64, hint_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# ===============================
|
| 31 |
+
# UI
|
| 32 |
+
# ===============================
|
|
|
|
| 33 |
|
| 34 |
+
with gr.Blocks(title="🩺 Nivra AI Agent") as demo:
|
| 35 |
+
gr.Markdown("# 🩺 Nivra AI Agent")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
chatbot = gr.Chatbot(show_label=False)
|
| 38 |
+
txt = gr.Textbox(placeholder="Describe your symptoms...")
|
| 39 |
+
send = gr.Button("Send")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
send.click(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
|
| 42 |
txt.submit(chat_fn, inputs=[txt, chatbot], outputs=[chatbot, txt])
|
| 43 |
|
| 44 |
+
# Hidden API endpoints
|
| 45 |
+
gr.Button(visible=False).click(
|
| 46 |
+
indictrans_translate,
|
| 47 |
+
inputs=[gr.Textbox(), gr.Textbox()],
|
| 48 |
+
outputs=[gr.Textbox()],
|
| 49 |
+
api_name="indictrans_translate",
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
gr.Button(visible=False).click(
|
| 53 |
+
vision_fn,
|
| 54 |
+
inputs=[gr.Textbox(), gr.Textbox()],
|
| 55 |
+
outputs=[gr.Textbox()],
|
| 56 |
+
api_name="vision_fn",
|
| 57 |
)
|
| 58 |
|
| 59 |
demo.launch(
|
| 60 |
server_name="0.0.0.0",
|
| 61 |
server_port=7860,
|
| 62 |
+
ssr_mode=False,
|
| 63 |
)
|