Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import os
|
2 |
-
import random
|
3 |
import gradio as gr
|
4 |
from txagent import TxAgent
|
5 |
|
@@ -26,22 +25,22 @@ MODEL_CONFIG = {
|
|
26 |
UI_CONFIG = {
|
27 |
'description': '''
|
28 |
<div>
|
29 |
-
<h1 style="text-align: center;">TxAgent:
|
30 |
-
<p style="text-align: center;">Precision therapeutics
|
31 |
</div>
|
32 |
''',
|
33 |
'disclaimer': '''
|
34 |
<div style="color: #666; font-size: 0.9em; margin-top: 20px;">
|
35 |
-
<strong>Disclaimer:</strong>
|
36 |
</div>
|
37 |
'''
|
38 |
}
|
39 |
|
40 |
# Example questions
|
41 |
EXAMPLE_QUESTIONS = [
|
42 |
-
"How should dosage be adjusted for
|
43 |
-
"Is Xolremdi suitable
|
44 |
-
"What are
|
45 |
]
|
46 |
|
47 |
# ========== Application Class ==========
|
@@ -49,114 +48,91 @@ class TxAgentApplication:
|
|
49 |
def __init__(self):
|
50 |
self.agent = None
|
51 |
self.is_initialized = False
|
52 |
-
self.loading = False
|
53 |
|
54 |
-
def initialize_agent(self
|
55 |
if self.is_initialized:
|
56 |
-
return
|
57 |
|
58 |
-
self.loading = True
|
59 |
try:
|
60 |
-
progress(0.1, desc="Initializing TxAgent...")
|
61 |
-
|
62 |
-
# Initialize the agent
|
63 |
self.agent = TxAgent(
|
64 |
MODEL_CONFIG['model_name'],
|
65 |
MODEL_CONFIG['rag_model_name'],
|
66 |
tool_files_dict=MODEL_CONFIG['tool_files'],
|
67 |
**MODEL_CONFIG['default_params']
|
68 |
)
|
69 |
-
|
70 |
-
progress(0.3, desc="Loading language model...")
|
71 |
self.agent.init_model()
|
72 |
-
|
73 |
-
progress(0.8, desc="Finalizing setup...")
|
74 |
self.is_initialized = True
|
75 |
-
|
76 |
-
|
77 |
-
return True, "TxAgent initialized successfully"
|
78 |
except Exception as e:
|
79 |
-
|
80 |
-
return False, f"Initialization failed: {str(e)}"
|
81 |
|
82 |
-
def chat(self, message, chat_history
|
83 |
if not self.is_initialized:
|
84 |
-
yield "Error:
|
85 |
return
|
86 |
|
87 |
try:
|
88 |
-
# Convert
|
89 |
messages = []
|
90 |
-
for
|
91 |
-
messages.append({"role": "user", "content":
|
92 |
-
messages.append({"role": "assistant", "content":
|
93 |
messages.append({"role": "user", "content": message})
|
94 |
|
95 |
-
# Stream
|
96 |
full_response = ""
|
97 |
for chunk in self.agent.run_gradio_chat(
|
98 |
messages,
|
99 |
-
temperature=
|
100 |
-
max_new_tokens=
|
101 |
max_tokens=8192,
|
102 |
multi_agent=False,
|
103 |
conversation=[],
|
104 |
max_round=30
|
105 |
):
|
106 |
full_response += chunk
|
107 |
-
yield full_response
|
108 |
-
|
109 |
except Exception as e:
|
110 |
-
yield f"Error
|
111 |
|
112 |
# ========== Gradio Interface ==========
|
113 |
def create_interface():
|
114 |
app = TxAgentApplication()
|
115 |
|
116 |
with gr.Blocks(title="TxAgent", theme=gr.themes.Soft()) as demo:
|
117 |
-
# Header Section
|
118 |
gr.Markdown(UI_CONFIG['description'])
|
119 |
|
120 |
-
# Initialization
|
121 |
with gr.Row():
|
122 |
init_btn = gr.Button("Initialize TxAgent", variant="primary")
|
123 |
-
init_status = gr.Textbox(label="
|
124 |
|
125 |
-
# Chat Interface
|
126 |
chatbot = gr.Chatbot(
|
127 |
height=600,
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
130 |
)
|
131 |
|
132 |
with gr.Row():
|
133 |
msg = gr.Textbox(
|
134 |
label="Your Question",
|
135 |
-
placeholder="Ask about drug interactions
|
136 |
scale=4
|
137 |
)
|
138 |
submit_btn = gr.Button("Submit", variant="primary", scale=1)
|
139 |
|
140 |
-
# Settings
|
141 |
-
with gr.Accordion("Advanced Settings", open=False):
|
142 |
-
with gr.Row():
|
143 |
-
temperature = gr.Slider(
|
144 |
-
minimum=0.1, maximum=1.0, value=0.3, step=0.1,
|
145 |
-
label="Temperature (higher = more creative)"
|
146 |
-
)
|
147 |
-
max_new_tokens = gr.Slider(
|
148 |
-
minimum=128, maximum=4096, value=1024, step=128,
|
149 |
-
label="Max Response Length"
|
150 |
-
)
|
151 |
-
|
152 |
# Examples
|
153 |
gr.Examples(
|
154 |
examples=EXAMPLE_QUESTIONS,
|
155 |
inputs=msg,
|
156 |
-
label="
|
157 |
)
|
158 |
|
159 |
-
# Footer
|
160 |
gr.Markdown(UI_CONFIG['disclaimer'])
|
161 |
|
162 |
# Event Handlers
|
@@ -167,13 +143,13 @@ def create_interface():
|
|
167 |
|
168 |
msg.submit(
|
169 |
app.chat,
|
170 |
-
[msg, chatbot
|
171 |
[chatbot]
|
172 |
)
|
173 |
|
174 |
submit_btn.click(
|
175 |
app.chat,
|
176 |
-
[msg, chatbot
|
177 |
[chatbot]
|
178 |
).then(
|
179 |
lambda: "", None, msg
|
@@ -183,20 +159,12 @@ def create_interface():
|
|
183 |
|
184 |
# ========== Main Execution ==========
|
185 |
if __name__ == "__main__":
|
186 |
-
# Create and launch the interface
|
187 |
interface = create_interface()
|
188 |
|
189 |
-
#
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
'auth_message': None,
|
197 |
-
'enable_queue': True,
|
198 |
-
'max_threads': 40,
|
199 |
-
'show_error': True
|
200 |
-
}
|
201 |
-
|
202 |
-
interface.queue(concurrency_count=5).launch(**launch_config)
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
from txagent import TxAgent
|
4 |
|
|
|
25 |
UI_CONFIG = {
|
26 |
'description': '''
|
27 |
<div>
|
28 |
+
<h1 style="text-align: center;">TxAgent: Therapeutic Reasoning AI</h1>
|
29 |
+
<p style="text-align: center;">Precision therapeutics with multi-step reasoning</p>
|
30 |
</div>
|
31 |
''',
|
32 |
'disclaimer': '''
|
33 |
<div style="color: #666; font-size: 0.9em; margin-top: 20px;">
|
34 |
+
<strong>Disclaimer:</strong> For informational purposes only, not medical advice.
|
35 |
</div>
|
36 |
'''
|
37 |
}
|
38 |
|
39 |
# Example questions
|
40 |
EXAMPLE_QUESTIONS = [
|
41 |
+
"How should dosage be adjusted for hepatic impairment with Journavx?",
|
42 |
+
"Is Xolremdi suitable with Prozac for WHIM syndrome?",
|
43 |
+
"What are Warfarin-Amiodarone contraindications?"
|
44 |
]
|
45 |
|
46 |
# ========== Application Class ==========
|
|
|
48 |
def __init__(self):
|
49 |
self.agent = None
|
50 |
self.is_initialized = False
|
|
|
51 |
|
52 |
+
def initialize_agent(self):
|
53 |
if self.is_initialized:
|
54 |
+
return "Model already initialized"
|
55 |
|
|
|
56 |
try:
|
|
|
|
|
|
|
57 |
self.agent = TxAgent(
|
58 |
MODEL_CONFIG['model_name'],
|
59 |
MODEL_CONFIG['rag_model_name'],
|
60 |
tool_files_dict=MODEL_CONFIG['tool_files'],
|
61 |
**MODEL_CONFIG['default_params']
|
62 |
)
|
|
|
|
|
63 |
self.agent.init_model()
|
|
|
|
|
64 |
self.is_initialized = True
|
65 |
+
return "TxAgent initialized successfully"
|
|
|
|
|
66 |
except Exception as e:
|
67 |
+
return f"Initialization failed: {str(e)}"
|
|
|
68 |
|
69 |
+
def chat(self, message, chat_history):
|
70 |
if not self.is_initialized:
|
71 |
+
yield "Error: Please initialize the model first"
|
72 |
return
|
73 |
|
74 |
try:
|
75 |
+
# Convert to messages format
|
76 |
messages = []
|
77 |
+
for user, assistant in chat_history:
|
78 |
+
messages.append({"role": "user", "content": user})
|
79 |
+
messages.append({"role": "assistant", "content": assistant})
|
80 |
messages.append({"role": "user", "content": message})
|
81 |
|
82 |
+
# Stream response
|
83 |
full_response = ""
|
84 |
for chunk in self.agent.run_gradio_chat(
|
85 |
messages,
|
86 |
+
temperature=0.3,
|
87 |
+
max_new_tokens=1024,
|
88 |
max_tokens=8192,
|
89 |
multi_agent=False,
|
90 |
conversation=[],
|
91 |
max_round=30
|
92 |
):
|
93 |
full_response += chunk
|
94 |
+
yield [(message, full_response)]
|
95 |
+
|
96 |
except Exception as e:
|
97 |
+
yield [(message, f"Error: {str(e)}")]
|
98 |
|
99 |
# ========== Gradio Interface ==========
|
100 |
def create_interface():
|
101 |
app = TxAgentApplication()
|
102 |
|
103 |
with gr.Blocks(title="TxAgent", theme=gr.themes.Soft()) as demo:
|
|
|
104 |
gr.Markdown(UI_CONFIG['description'])
|
105 |
|
106 |
+
# Initialization
|
107 |
with gr.Row():
|
108 |
init_btn = gr.Button("Initialize TxAgent", variant="primary")
|
109 |
+
init_status = gr.Textbox(label="Status", interactive=False)
|
110 |
|
111 |
+
# Chat Interface (using modern messages format)
|
112 |
chatbot = gr.Chatbot(
|
113 |
height=600,
|
114 |
+
label="Conversation",
|
115 |
+
avatar_images=(
|
116 |
+
"https://example.com/user.png", # User avatar
|
117 |
+
"https://example.com/bot.png" # Bot avatar
|
118 |
+
)
|
119 |
)
|
120 |
|
121 |
with gr.Row():
|
122 |
msg = gr.Textbox(
|
123 |
label="Your Question",
|
124 |
+
placeholder="Ask about drug interactions or treatments...",
|
125 |
scale=4
|
126 |
)
|
127 |
submit_btn = gr.Button("Submit", variant="primary", scale=1)
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
# Examples
|
130 |
gr.Examples(
|
131 |
examples=EXAMPLE_QUESTIONS,
|
132 |
inputs=msg,
|
133 |
+
label="Try these examples:"
|
134 |
)
|
135 |
|
|
|
136 |
gr.Markdown(UI_CONFIG['disclaimer'])
|
137 |
|
138 |
# Event Handlers
|
|
|
143 |
|
144 |
msg.submit(
|
145 |
app.chat,
|
146 |
+
[msg, chatbot],
|
147 |
[chatbot]
|
148 |
)
|
149 |
|
150 |
submit_btn.click(
|
151 |
app.chat,
|
152 |
+
[msg, chatbot],
|
153 |
[chatbot]
|
154 |
).then(
|
155 |
lambda: "", None, msg
|
|
|
159 |
|
160 |
# ========== Main Execution ==========
|
161 |
if __name__ == "__main__":
|
|
|
162 |
interface = create_interface()
|
163 |
|
164 |
+
# Correct launch configuration
|
165 |
+
interface.launch(
|
166 |
+
server_name="0.0.0.0",
|
167 |
+
server_port=7860,
|
168 |
+
share=True,
|
169 |
+
enable_queue=True # Enable queue without deprecated parameters
|
170 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|