File size: 5,170 Bytes
4b0f1a8
12efdad
4b0f1a8
12efdad
4b0f1a8
12efdad
 
4b0f1a8
12efdad
4b0f1a8
 
 
 
 
 
 
 
 
 
 
 
12efdad
70839bb
4b0f1a8
 
 
 
92abf33
 
4b0f1a8
 
 
 
92abf33
4b0f1a8
 
12efdad
 
4b0f1a8
 
92abf33
 
 
12efdad
 
4b0f1a8
 
 
 
 
12efdad
92abf33
4b0f1a8
92abf33
8e533b3
4b0f1a8
 
 
 
 
 
 
 
 
92abf33
4b0f1a8
92abf33
4b0f1a8
92abf33
4b0f1a8
92abf33
4b0f1a8
 
 
92abf33
4b0f1a8
92abf33
 
 
4b0f1a8
 
92abf33
4b0f1a8
 
 
92abf33
 
4b0f1a8
 
 
 
 
 
92abf33
 
4b0f1a8
92abf33
4b0f1a8
 
 
 
 
 
 
8e533b3
92abf33
4b0f1a8
 
92abf33
8e533b3
92abf33
4b0f1a8
 
92abf33
 
 
 
 
12efdad
4b0f1a8
 
 
 
92abf33
4b0f1a8
8e533b3
4b0f1a8
 
 
 
 
 
92abf33
4b0f1a8
 
 
8e533b3
4b0f1a8
 
 
 
 
8e533b3
4b0f1a8
 
92abf33
4b0f1a8
 
 
 
 
92abf33
4b0f1a8
 
 
 
8e533b3
 
70839bb
4b0f1a8
70839bb
4b0f1a8
 
92abf33
 
 
 
 
 
 
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
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

    def initialize_agent(self):
        if self.is_initialized:
            return "Model already initialized"
        
        try:
            self.agent = TxAgent(
                MODEL_CONFIG['model_name'],
                MODEL_CONFIG['rag_model_name'],
                tool_files_dict=MODEL_CONFIG['tool_files'],
                **MODEL_CONFIG['default_params']
            )
            self.agent.init_model()
            self.is_initialized = True
            return "TxAgent initialized successfully"
        except Exception as e:
            return f"Initialization failed: {str(e)}"

    def chat(self, message, chat_history):
        if not self.is_initialized:
            yield "Error: Please initialize the model first"
            return

        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})

            # Stream response
            full_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
            ):
                full_response += chunk
                yield [(message, full_response)]
                
        except Exception as e:
            yield [(message, f"Error: {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 (using modern messages format)
        chatbot = gr.Chatbot(
            height=600,
            label="Conversation",
            avatar_images=(
                "https://example.com/user.png",  # User avatar
                "https://example.com/bot.png"    # Bot avatar
            )
        )
        
        with gr.Row():
            msg = gr.Textbox(
                label="Your Question",
                placeholder="Ask about drug interactions or treatments...",
                scale=4
            )
            submit_btn = gr.Button("Submit", variant="primary", scale=1)
        
        # Examples
        gr.Examples(
            examples=EXAMPLE_QUESTIONS,
            inputs=msg,
            label="Try these examples:"
        )
        
        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__":
    interface = create_interface()
    
    # Correct launch configuration
    interface.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True,
        enable_queue=True  # Enable queue without deprecated parameters
    )