File size: 9,845 Bytes
4b0f1a8
167b103
 
b8c0ae3
59ced24
 
d206f24
167b103
35da672
d206f24
 
 
849209d
12efdad
59ced24
 
 
 
58353ee
59ced24
 
167b103
35da672
849209d
 
 
 
 
 
 
12efdad
70839bb
58353ee
849209d
 
 
 
167b103
 
849209d
 
d206f24
849209d
 
 
d206f24
849209d
d206f24
849209d
d206f24
849209d
d206f24
849209d
 
d206f24
849209d
d206f24
849209d
 
d206f24
 
167b103
 
 
58353ee
167b103
 
 
 
 
 
849209d
 
d206f24
849209d
35da672
849209d
 
 
 
 
 
 
 
 
 
 
 
 
 
35da672
 
 
 
 
d206f24
849209d
 
d206f24
35da672
849209d
 
35da672
849209d
35da672
849209d
 
 
35da672
 
849209d
 
 
35da672
59ced24
 
849209d
35da672
 
849209d
35da672
 
 
849209d
35da672
 
849209d
35da672
 
 
849209d
35da672
 
 
 
 
58353ee
35da672
 
 
 
 
 
 
849209d
35da672
58353ee
59ced24
58353ee
 
 
59ced24
35da672
59ced24
35da672
59ced24
12efdad
59ced24
4b0f1a8
 
 
12efdad
59ced24
4b0f1a8
849209d
 
4b0f1a8
849209d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167b103
4b0f1a8
35da672
167b103
4b0f1a8
59ced24
4b0f1a8
849209d
 
4b0f1a8
e014e82
4b0f1a8
167b103
 
92abf33
 
4b0f1a8
 
 
 
 
e014e82
849209d
 
4b0f1a8
35da672
849209d
4b0f1a8
 
59ced24
849209d
 
 
 
 
 
 
 
35da672
849209d
 
35da672
849209d
 
4b0f1a8
59ced24
849209d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35da672
 
849209d
35da672
849209d
35da672
 
 
 
 
849209d
 
 
 
 
 
8e533b3
70839bb
 
35da672
849209d
 
 
35da672
 
849209d
35da672
 
849209d
35da672
 
 
 
849209d
35da672
 
849209d
35da672
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import json
import logging
import torch
from txagent import TxAgent
import gradio as gr
from huggingface_hub import snapshot_download
from tooluniverse import ToolUniverse
import time
from requests.adapters import HTTPAdapter
from requests import Session
from urllib3.util.retry import Retry
from tqdm import tqdm

# Configuration
CONFIG = {
    "model_name": "mims-harvard/TxAgent-T1-Llama-3.1-8B",
    "rag_model_name": "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
    "embedding_filename": "ToolRAG-T1-GTE-Qwen2-1.5Btool_embedding_47dc56b3e3ddeb31af4f19defdd538d984de1500368852a0fab80bc2e826c944.pt",
    "local_dir": "./models",
    "tool_files": {
        "new_tool": "./data/new_tool.json"
    },
    "download_settings": {
        "timeout": 600,  # 10 minutes per request
        "max_retries": 5,
        "retry_delay": 30,  # seconds between retries
        "chunk_size": 1024 * 1024 * 10,  # 10MB chunks
        "max_concurrent": 2  # concurrent downloads
    }
}

# Logging setup
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def create_optimized_session():
    """Create a session optimized for large file downloads"""
    session = Session()
    
    retry_strategy = Retry(
        total=CONFIG["download_settings"]["max_retries"],
        backoff_factor=1,
        status_forcelist=[408, 429, 500, 502, 503, 504]
    )
    
    adapter = HTTPAdapter(
        max_retries=retry_strategy,
        pool_connections=10,
        pool_maxsize=10,
        pool_block=True
    )
    
    session.mount("https://", adapter)
    session.mount("http://", adapter)
    
    return session

def prepare_tool_files():
    os.makedirs("./data", exist_ok=True)
    if not os.path.exists(CONFIG["tool_files"]["new_tool"]):
        logger.info("Generating tool list using ToolUniverse...")
        tu = ToolUniverse()
        tools = tu.get_all_tools()
        with open(CONFIG["tool_files"]["new_tool"], "w") as f:
            json.dump(tools, f, indent=2)
        logger.info(f"Saved {len(tools)} tools to {CONFIG['tool_files']['new_tool']}")

def download_model_with_progress(repo_id, local_dir):
    custom_session = create_optimized_session()
    
    for attempt in range(CONFIG["download_settings"]["max_retries"] + 1):
        try:
            logger.info(f"Download attempt {attempt + 1} for {repo_id}")
            
            # Create progress bar
            progress = tqdm(
                unit="B",
                unit_scale=True,
                unit_divisor=1024,
                miniters=1,
                desc=f"Downloading {repo_id.split('/')[-1]}"
            )
            
            def update_progress(monitor):
                progress.update(monitor.bytes_read - progress.n)
            
            snapshot_download(
                repo_id=repo_id,
                local_dir=local_dir,
                resume_download=True,
                local_dir_use_symlinks=False,
                use_auth_token=True,
                max_workers=CONFIG["download_settings"]["max_concurrent"],
                tqdm_class=None,  # We handle progress ourselves
                session=custom_session
            )
            
            progress.close()
            return True
            
        except Exception as e:
            logger.error(f"Attempt {attempt + 1} failed: {str(e)}")
            if attempt < CONFIG["download_settings"]["max_retries"]:
                wait_time = CONFIG["download_settings"]["retry_delay"] * (attempt + 1)
                logger.info(f"Waiting {wait_time} seconds before retry...")
                time.sleep(wait_time)
            else:
                progress.close()
                return False

def download_model_files():
    os.makedirs(CONFIG["local_dir"], exist_ok=True)
    logger.info("Starting model downloads...")

    # Download main model
    if not download_model_with_progress(
        CONFIG["model_name"],
        os.path.join(CONFIG["local_dir"], CONFIG["model_name"])
    ):
        raise RuntimeError(f"Failed to download {CONFIG['model_name']}")

    # Download RAG model
    if not download_model_with_progress(
        CONFIG["rag_model_name"],
        os.path.join(CONFIG["local_dir"], CONFIG["rag_model_name"])
    ):
        raise RuntimeError(f"Failed to download {CONFIG['rag_model_name']}")

    logger.info("All model files downloaded successfully")

def load_embeddings(agent):
    embedding_path = CONFIG["embedding_filename"]
    if os.path.exists(embedding_path):
        logger.info("✅ Loading pre-generated embeddings file")
        try:
            embeddings = torch.load(embedding_path)
            agent.rag_model.tool_desc_embedding = embeddings
            return
        except Exception as e:
            logger.error(f"Failed to load embeddings: {e}")
    
    logger.info("Generating tool embeddings...")
    try:
        tools = agent.tooluniverse.get_all_tools()
        descriptions = [tool["description"] for tool in tools]
        embeddings = agent.rag_model.generate_embeddings(descriptions)
        torch.save(embeddings, embedding_path)
        agent.rag_model.tool_desc_embedding = embeddings
        logger.info(f"Embeddings saved to {embedding_path}")
    except Exception as e:
        logger.error(f"Failed to generate embeddings: {e}")
        raise

class TxAgentApp:
    def __init__(self):
        self.agent = None
        self.is_initialized = False

    def initialize(self):
        if self.is_initialized:
            return "✅ Already initialized"
        
        try:
            # Initialize with progress tracking
            with tqdm(total=4, desc="Initializing TxAgent") as pbar:
                logger.info("Creating TxAgent instance...")
                self.agent = TxAgent(
                    CONFIG["model_name"],
                    CONFIG["rag_model_name"],
                    tool_files_dict=CONFIG["tool_files"],
                    force_finish=True,
                    enable_checker=True,
                    step_rag_num=10,
                    seed=100,
                    additional_default_tools=["DirectResponse", "RequireClarification"]
                )
                pbar.update(1)
                
                logger.info("Initializing models...")
                self.agent.init_model()
                pbar.update(1)
                
                logger.info("Loading embeddings...")
                load_embeddings(self.agent)
                pbar.update(1)
                
                self.is_initialized = True
                pbar.update(1)
                
            return "✅ TxAgent initialized successfully"
        except Exception as e:
            logger.error(f"Initialization failed: {str(e)}")
            return f"❌ Initialization failed: {str(e)}"

    def chat(self, message, history):
        if not self.is_initialized:
            return history + [(message, "⚠️ Please initialize the model first")]
        
        try:
            response = ""
            for chunk in self.agent.run_gradio_chat(
                message=message,
                history=history,
                temperature=0.3,
                max_new_tokens=1024,
                max_tokens=8192,
                multi_agent=False,
                conversation=[],
                max_round=30
            ):
                response += chunk
                yield history + [(message, response)]
                
        except Exception as e:
            logger.error(f"Chat error: {str(e)}")
            yield history + [(message, f"Error: {str(e)}")]

def create_interface():
    app = TxAgentApp()
    
    with gr.Blocks(
        title="TxAgent",
        css="""
        .gradio-container {max-width: 900px !important}
        .progress-bar {height: 20px !important}
        """
    ) as demo:
        gr.Markdown("""
        # � TxAgent: Therapeutic Reasoning AI
        ### Specialized for clinical decision support
        """)
        
        # Initialization section
        with gr.Row():
            init_btn = gr.Button("Initialize Model", variant="primary")
            init_status = gr.Textbox(label="Status", interactive=False)
            download_progress = gr.Textbox(visible=False)
        
        # Chat interface
        chatbot = gr.Chatbot(height=500, label="Conversation")
        msg = gr.Textbox(label="Your clinical question", placeholder="Ask about drug interactions, dosing, etc...")
        clear_btn = gr.Button("Clear Chat")
        
        # Examples
        gr.Examples(
            examples=[
                "How to adjust Journavx for renal impairment?",
                "Xolremdi and Prozac interaction in WHIM syndrome?",
                "Alternative to Warfarin for patient with amiodarone?"
            ],
            inputs=msg,
            label="Example Questions"
        )
        
        # Event handlers
        init_btn.click(
            fn=app.initialize,
            outputs=init_status
        )
        
        msg.submit(
            fn=app.chat,
            inputs=[msg, chatbot],
            outputs=chatbot
        )
        
        clear_btn.click(
            fn=lambda: ([], ""),
            outputs=[chatbot, msg]
        )
    
    return demo

if __name__ == "__main__":
    try:
        logger.info("Starting application setup...")
        
        # Prepare files
        prepare_tool_files()
        
        # Download models with progress tracking
        download_model_files()
        
        # Launch interface
        interface = create_interface()
        interface.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False
        )
    except Exception as e:
        logger.error(f"Fatal error: {str(e)}")
        raise