File size: 6,136 Bytes
4b0f1a8
12efdad
4b0f1a8
12efdad
4b0f1a8
12efdad
 
4b0f1a8
12efdad
4b0f1a8
 
 
 
 
 
 
 
 
 
 
 
12efdad
70839bb
4b0f1a8
 
 
 
92abf33
 
4b0f1a8
 
 
 
92abf33
4b0f1a8
 
12efdad
 
4b0f1a8
 
92abf33
 
 
12efdad
 
4b0f1a8
 
 
 
 
a59a7be
12efdad
92abf33
4b0f1a8
92abf33
8e533b3
4b0f1a8
a59a7be
4b0f1a8
 
 
 
 
 
a59a7be
 
 
 
 
 
 
 
 
 
 
4b0f1a8
a59a7be
92abf33
a59a7be
4b0f1a8
a59a7be
92abf33
4b0f1a8
92abf33
4b0f1a8
a59a7be
 
e014e82
 
4b0f1a8
92abf33
4b0f1a8
92abf33
 
 
4b0f1a8
 
e014e82
 
4b0f1a8
 
92abf33
 
4b0f1a8
 
 
 
 
e014e82
 
 
4b0f1a8
a59a7be
4b0f1a8
 
 
 
 
 
 
8e533b3
92abf33
4b0f1a8
 
92abf33
8e533b3
a59a7be
4b0f1a8
 
92abf33
e014e82
 
12efdad
4b0f1a8
 
 
 
92abf33
e014e82
 
8e533b3
4b0f1a8
 
 
 
 
 
e014e82
 
4b0f1a8
 
 
8e533b3
4b0f1a8
 
 
 
 
8e533b3
4b0f1a8
 
92abf33
a59a7be
4b0f1a8
 
 
 
92abf33
a59a7be
4b0f1a8
 
 
8e533b3
 
70839bb
4b0f1a8
70839bb
a59a7be
4b0f1a8
 
a59a7be
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import gradio as gr
from txagent import TxAgent

# ========== Configuration ==========
current_dir = os.path.dirname(os.path.abspath(__file__))
os.environ["MKL_THREADING_LAYER"] = "GNU"
os.environ["TOKENIZERS_PARALLELISM"] = "false"

# Model configuration
MODEL_CONFIG = {
    'model_name': 'mims-harvard/TxAgent-T1-Llama-3.1-8B',
    'rag_model_name': 'mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B',
    'tool_files': {'new_tool': os.path.join(current_dir, 'data', 'new_tool.json')},
    'additional_tools': ['DirectResponse', 'RequireClarification'],
    'default_params': {
        'force_finish': True,
        'enable_checker': True,
        'step_rag_num': 10,
        'seed': 100
    }
}

# UI Configuration
UI_CONFIG = {
    'description': '''
    <div>
    <h1 style="text-align: center;">TxAgent: Therapeutic Reasoning AI</h1>
    <p style="text-align: center;">Precision therapeutics with multi-step reasoning</p>
    </div>
    ''',
    'disclaimer': '''
    <div style="color: #666; font-size: 0.9em; margin-top: 20px;">
    <strong>Disclaimer:</strong> For informational purposes only, not medical advice.
    </div>
    '''
}

# Example questions
EXAMPLE_QUESTIONS = [
    "How should dosage be adjusted for hepatic impairment with Journavx?",
    "Is Xolremdi suitable with Prozac for WHIM syndrome?",
    "What are Warfarin-Amiodarone contraindications?"
]

# ========== Application Class ==========
class TxAgentApplication:
    def __init__(self):
        self.agent = None
        self.is_initialized = False
        self.initialization_error = None

    def initialize_agent(self):
        if self.is_initialized:
            return "Model already initialized"
        
        try:
            # Initialize the agent
            self.agent = TxAgent(
                MODEL_CONFIG['model_name'],
                MODEL_CONFIG['rag_model_name'],
                tool_files_dict=MODEL_CONFIG['tool_files'],
                **MODEL_CONFIG['default_params']
            )
            
            # Initialize model with error handling
            try:
                self.agent.init_model()
            except Exception as e:
                # Handle specific tool embedding error
                if "No such file or directory" in str(e) and "tool_embedding" in str(e):
                    return ("Error: Missing tool embedding file. "
                           "Please ensure the RAG model files are properly downloaded.")
                raise
            
            self.is_initialized = True
            self.initialization_error = None
            return "TxAgent initialized successfully"
            
        except Exception as e:
            self.initialization_error = str(e)
            return f"Initialization failed: {str(e)}"

    def chat(self, message, chat_history):
        if not self.is_initialized:
            if self.initialization_error:
                return chat_history + [(message, f"System Error: {self.initialization_error}")]
            return chat_history + [(message, "Error: Please initialize the model first")]
        
        try:
            # Convert to messages format
            messages = []
            for user, assistant in chat_history:
                messages.append({"role": "user", "content": user})
                messages.append({"role": "assistant", "content": assistant})
            messages.append({"role": "user", "content": message})

            # Get response
            response = ""
            for chunk in self.agent.run_gradio_chat(
                messages,
                temperature=0.3,
                max_new_tokens=1024,
                max_tokens=8192,
                multi_agent=False,
                conversation=[],
                max_round=30
            ):
                response += chunk
            
            return chat_history + [(message, response)]
        except Exception as e:
            return chat_history + [(message, f"Error during processing: {str(e)}")]

# ========== Gradio Interface ==========
def create_interface():
    app = TxAgentApplication()
    
    with gr.Blocks(title="TxAgent", theme=gr.themes.Soft()) as demo:
        gr.Markdown(UI_CONFIG['description'])
        
        # Initialization
        with gr.Row():
            init_btn = gr.Button("Initialize TxAgent", variant="primary")
            init_status = gr.Textbox(label="Status", interactive=False)
        
        # Chat Interface
        chatbot = gr.Chatbot(
            height=600,
            label="Conversation",
            show_label=True,
            show_copy_button=True
        )
        
        with gr.Row():
            msg = gr.Textbox(
                label="Your Question",
                placeholder="Ask about drug interactions or treatments...",
                scale=4,
                container=False
            )
            submit_btn = gr.Button("Submit", variant="primary", scale=1)
        
        # Examples
        gr.Examples(
            examples=EXAMPLE_QUESTIONS,
            inputs=msg,
            label="Try these examples:",
            examples_per_page=3
        )
        
        gr.Markdown(UI_CONFIG['disclaimer'])

        # Event Handlers
        init_btn.click(
            app.initialize_agent,
            outputs=init_status
        )
        
        msg.submit(
            app.chat,
            [msg, chatbot],
            chatbot
        )
        
        submit_btn.click(
            app.chat,
            [msg, chatbot],
            chatbot
        ).then(
            lambda: "", None, msg
        )
    
    return demo

# ========== Main Execution ==========
if __name__ == "__main__":
    # Create and configure the interface
    interface = create_interface()
    
    # Launch configuration
    launch_params = {
        'server_name': '0.0.0.0',
        'server_port': 7860,
        'share': True
    }
    
    # Enable queue if needed (for production)
    try:
        interface.queue().launch(**launch_params)
    except Exception as e:
        print(f"Error launching interface: {e}")
        interface.launch(**launch_params)  # Fallback without queue