Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,98 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
from medical_chatbot import ColabBioGPTChatbot
|
4 |
-
|
5 |
-
# Instantiate the chatbot with CPU settings for HF Spaces
|
6 |
-
chatbot = ColabBioGPTChatbot(use_gpu=False, use_8bit=False)
|
7 |
-
|
8 |
-
medical_file_uploaded = False
|
9 |
-
|
10 |
-
def upload_and_initialize(file):
|
11 |
-
global medical_file_uploaded
|
12 |
-
if file is None:
|
13 |
-
return (
|
14 |
-
"❌ Please upload a medical .txt file.",
|
15 |
-
gr.Chatbot(visible=False),
|
16 |
-
gr.Textbox(visible=False),
|
17 |
-
gr.Button(visible=False)
|
18 |
-
)
|
19 |
-
|
20 |
-
# Handle the file path correctly for Gradio
|
21 |
-
file_path = file.name if hasattr(file, 'name') else file
|
22 |
-
success = chatbot.load_medical_data(file_path)
|
23 |
-
|
24 |
-
if success:
|
25 |
-
medical_file_uploaded = True
|
26 |
-
model_name = type(chatbot.model).__name__ if chatbot.model else "Fallback Model"
|
27 |
-
status = f"✅ Medical data processed successfully!\n📦 Model in use: {model_name}"
|
28 |
-
return (
|
29 |
-
status,
|
30 |
-
gr.Chatbot(visible=True),
|
31 |
-
gr.Textbox(visible=True),
|
32 |
-
gr.Button(visible=True)
|
33 |
-
)
|
34 |
-
else:
|
35 |
-
return (
|
36 |
-
"❌ Failed to process uploaded file.",
|
37 |
-
gr.Chatbot(visible=False),
|
38 |
-
gr.Textbox(visible=False),
|
39 |
-
gr.Button(visible=False)
|
40 |
-
)
|
41 |
-
|
42 |
-
def generate_response(user_input):
|
43 |
-
if not medical_file_uploaded:
|
44 |
-
return "⚠️ Please upload and initialize medical data first."
|
45 |
-
return chatbot.chat(user_input)
|
46 |
-
|
47 |
-
# Create the Gradio interface
|
48 |
-
with gr.Blocks(title="🩺 Pediatric Medical Assistant") as demo:
|
49 |
-
gr.Markdown("## 🩺 Pediatric Medical Assistant\nUpload a medical .txt file and start chatting.")
|
50 |
-
|
51 |
-
with gr.Row():
|
52 |
-
file_input = gr.File(label="📁 Upload Medical File", file_types=[".txt"])
|
53 |
-
upload_btn = gr.Button("📤 Upload and Initialize")
|
54 |
-
|
55 |
-
upload_output = gr.Textbox(label="System Status", interactive=False)
|
56 |
-
|
57 |
-
chatbot_ui = gr.Chatbot(label="🧠 Chat History", visible=False)
|
58 |
-
user_input = gr.Textbox(
|
59 |
-
placeholder="Ask a pediatric health question...",
|
60 |
-
lines=2,
|
61 |
-
show_label=False,
|
62 |
-
visible=False
|
63 |
-
)
|
64 |
-
submit_btn = gr.Button("Send", visible=False)
|
65 |
-
|
66 |
-
upload_btn.click(
|
67 |
-
fn=upload_and_initialize,
|
68 |
-
inputs=[file_input],
|
69 |
-
outputs=[upload_output, chatbot_ui, user_input, submit_btn]
|
70 |
-
)
|
71 |
-
|
72 |
-
def on_submit(user_message, chat_history):
|
73 |
-
if not user_message.strip():
|
74 |
-
return "", chat_history
|
75 |
-
|
76 |
-
bot_response = generate_response(user_message)
|
77 |
-
chat_history.append((user_message, bot_response))
|
78 |
-
return "", chat_history
|
79 |
-
|
80 |
-
user_input.submit(
|
81 |
-
fn=on_submit,
|
82 |
-
inputs=[user_input, chatbot_ui],
|
83 |
-
outputs=[user_input, chatbot_ui]
|
84 |
-
)
|
85 |
-
submit_btn.click(
|
86 |
-
fn=on_submit,
|
87 |
-
inputs=[user_input, chatbot_ui],
|
88 |
-
outputs=[user_input, chatbot_ui]
|
89 |
-
)
|
90 |
-
|
91 |
-
# Launch with proper settings for Hugging Face Spaces
|
92 |
-
if __name__ == "__main__":
|
93 |
-
demo.launch(
|
94 |
-
share=False, # Don't need share=True on HF Spaces
|
95 |
-
server_name="0.0.0.0", # Listen on all interfaces for HF Spaces
|
96 |
-
server_port=7860, # Standard port for HF Spaces
|
97 |
-
show_error=True # Show detailed errors for debugging
|
98 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|