Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from medical_chatbot import ColabBioGPTChatbot | |
| # Instantiate the chatbot | |
| chatbot = ColabBioGPTChatbot(use_gpu=True, use_8bit=True) | |
| medical_file_uploaded = False | |
| def upload_and_initialize(file): | |
| try: | |
| chatbot.load_medical_file(file.name) | |
| return "✅ Medical file uploaded and chatbot initialized!", gr.update(visible=True), gr.update(visible=True), gr.update(visible=True) | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) | |
| def generate_response(user_input): | |
| if not medical_file_uploaded: | |
| return "⚠️ Please upload and initialize medical data first." | |
| return chatbot.chat(user_input) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🩺 Pediatric Medical Assistant\nUpload a medical .txt file and start chatting.") | |
| with gr.Row(): | |
| file_input = gr.File(label="📁 Upload Medical File", file_types=[".txt"]) | |
| upload_btn = gr.Button("📤 Upload and Initialize") | |
| upload_output = gr.Textbox(label="System Status", interactive=False) | |
| chatbot_ui = gr.Chatbot(label="🧠 Chat History") | |
| user_input = gr.Textbox(placeholder="Ask a pediatric health question...", lines=2, show_label=False) | |
| submit_btn = gr.Button("Send") | |
| chatbot_ui.visible = False | |
| user_input.visible = False | |
| submit_btn.visible = False | |
| upload_btn.click( | |
| fn=upload_and_initialize, | |
| inputs=[file_input], | |
| outputs=[upload_output, chatbot_ui, user_input, submit_btn] | |
| ) | |
| def on_submit(user_message, chat_history): | |
| bot_response = generate_response(user_message) | |
| chat_history.append((user_message, bot_response)) | |
| return "", chat_history | |
| user_input.submit(fn=on_submit, inputs=[user_input, chatbot_ui], outputs=[user_input, chatbot_ui]) | |
| submit_btn.click(fn=on_submit, inputs=[user_input, chatbot_ui], outputs=[user_input, chatbot_ui]) | |
| demo.launch(share=True) | |