Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
from medical_chatbot import ColabBioGPTChatbot
|
| 5 |
+
|
| 6 |
+
# Instantiate the chatbot
|
| 7 |
+
chatbot = ColabBioGPTChatbot(use_gpu=True, use_8bit=True)
|
| 8 |
+
|
| 9 |
+
medical_file_uploaded = False
|
| 10 |
+
|
| 11 |
+
def upload_and_initialize(file):
|
| 12 |
+
global medical_file_uploaded
|
| 13 |
+
|
| 14 |
+
if file is None:
|
| 15 |
+
return (
|
| 16 |
+
"❌ Please upload a medical .txt file.",
|
| 17 |
+
gr.Chatbot.update(visible=False),
|
| 18 |
+
gr.Textbox.update(visible=False),
|
| 19 |
+
gr.Button.update(visible=False)
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
chatbot.load_medical_data(file.name)
|
| 24 |
+
medical_file_uploaded = True
|
| 25 |
+
return (
|
| 26 |
+
"✅ Medical data loaded and chatbot initialized!",
|
| 27 |
+
gr.Chatbot.update(visible=True),
|
| 28 |
+
gr.Textbox.update(visible=True),
|
| 29 |
+
gr.Button.update(visible=True)
|
| 30 |
+
)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return (
|
| 33 |
+
f"❌ Failed to load file: {str(e)}",
|
| 34 |
+
gr.Chatbot.update(visible=False),
|
| 35 |
+
gr.Textbox.update(visible=False),
|
| 36 |
+
gr.Button.update(visible=False)
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
def generate_response(user_input):
|
| 40 |
+
if not medical_file_uploaded:
|
| 41 |
+
return "⚠️ Please upload and initialize medical data first."
|
| 42 |
+
return chatbot.chat(user_input)
|
| 43 |
+
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("## 🩺 Pediatric Medical Assistant\nUpload a medical .txt file and start chatting.")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
file_input = gr.File(label="📁 Upload Medical File", file_types=[".txt"])
|
| 49 |
+
upload_btn = gr.Button("📤 Upload and Initialize")
|
| 50 |
+
|
| 51 |
+
upload_output = gr.Textbox(label="System Status", interactive=False)
|
| 52 |
+
|
| 53 |
+
chatbot_ui = gr.Chatbot(label="🧠 Chat History")
|
| 54 |
+
user_input = gr.Textbox(placeholder="Ask a pediatric health question...", lines=2, show_label=False)
|
| 55 |
+
submit_btn = gr.Button("Send")
|
| 56 |
+
|
| 57 |
+
chatbot_ui.visible = False
|
| 58 |
+
user_input.visible = False
|
| 59 |
+
submit_btn.visible = False
|
| 60 |
+
|
| 61 |
+
upload_btn.click(
|
| 62 |
+
fn=upload_and_initialize,
|
| 63 |
+
inputs=[file_input],
|
| 64 |
+
outputs=[upload_output, chatbot_ui, user_input, submit_btn]
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
def on_submit(user_message, chat_history):
|
| 68 |
+
bot_response = generate_response(user_message)
|
| 69 |
+
chat_history.append((user_message, bot_response))
|
| 70 |
+
return "", chat_history
|
| 71 |
+
|
| 72 |
+
user_input.submit(fn=on_submit, inputs=[user_input, chatbot_ui], outputs=[user_input, chatbot_ui])
|
| 73 |
+
submit_btn.click(fn=on_submit, inputs=[user_input, chatbot_ui], outputs=[user_input, chatbot_ui])
|
| 74 |
+
|
| 75 |
+
demo.launch(share=True)
|
| 76 |
+
|