Update app.py
Browse files
app.py
CHANGED
@@ -48,26 +48,43 @@ class TxAgentApplication:
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
return chat_history + [(message, "Error: Please initialize the model first")]
|
72 |
|
73 |
try:
|
@@ -93,7 +110,7 @@ class TxAgentApplication:
|
|
93 |
|
94 |
return chat_history + [(message, response)]
|
95 |
except Exception as e:
|
96 |
-
return chat_history + [(message, f"Error: {str(e)}")]
|
97 |
|
98 |
# ========== Gradio Interface ==========
|
99 |
def create_interface():
|
@@ -107,15 +124,11 @@ def create_interface():
|
|
107 |
init_btn = gr.Button("Initialize TxAgent", variant="primary")
|
108 |
init_status = gr.Textbox(label="Status", interactive=False)
|
109 |
|
110 |
-
# Chat Interface
|
111 |
chatbot = gr.Chatbot(
|
112 |
height=600,
|
113 |
label="Conversation",
|
114 |
show_label=True,
|
115 |
-
avatar_images=(
|
116 |
-
None, # User avatar (None for default)
|
117 |
-
None # Bot avatar (None for default)
|
118 |
-
),
|
119 |
show_copy_button=True
|
120 |
)
|
121 |
|
@@ -147,15 +160,13 @@ def create_interface():
|
|
147 |
msg.submit(
|
148 |
app.chat,
|
149 |
[msg, chatbot],
|
150 |
-
chatbot
|
151 |
-
queue=True # Enable queue for this event
|
152 |
)
|
153 |
|
154 |
submit_btn.click(
|
155 |
app.chat,
|
156 |
[msg, chatbot],
|
157 |
-
chatbot
|
158 |
-
queue=True # Enable queue for this event
|
159 |
).then(
|
160 |
lambda: "", None, msg
|
161 |
)
|
@@ -164,12 +175,19 @@ def create_interface():
|
|
164 |
|
165 |
# ========== Main Execution ==========
|
166 |
if __name__ == "__main__":
|
|
|
167 |
interface = create_interface()
|
168 |
|
169 |
-
#
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
def __init__(self):
|
49 |
self.agent = None
|
50 |
self.is_initialized = False
|
51 |
+
self.initialization_error = None
|
52 |
|
53 |
def initialize_agent(self):
|
54 |
if self.is_initialized:
|
55 |
return "Model already initialized"
|
56 |
|
57 |
try:
|
58 |
+
# Initialize the agent
|
59 |
self.agent = TxAgent(
|
60 |
MODEL_CONFIG['model_name'],
|
61 |
MODEL_CONFIG['rag_model_name'],
|
62 |
tool_files_dict=MODEL_CONFIG['tool_files'],
|
63 |
**MODEL_CONFIG['default_params']
|
64 |
)
|
65 |
+
|
66 |
+
# Initialize model with error handling
|
67 |
+
try:
|
68 |
+
self.agent.init_model()
|
69 |
+
except Exception as e:
|
70 |
+
# Handle specific tool embedding error
|
71 |
+
if "No such file or directory" in str(e) and "tool_embedding" in str(e):
|
72 |
+
return ("Error: Missing tool embedding file. "
|
73 |
+
"Please ensure the RAG model files are properly downloaded.")
|
74 |
+
raise
|
75 |
+
|
76 |
self.is_initialized = True
|
77 |
+
self.initialization_error = None
|
78 |
return "TxAgent initialized successfully"
|
79 |
+
|
80 |
except Exception as e:
|
81 |
+
self.initialization_error = str(e)
|
82 |
return f"Initialization failed: {str(e)}"
|
83 |
|
84 |
def chat(self, message, chat_history):
|
85 |
if not self.is_initialized:
|
86 |
+
if self.initialization_error:
|
87 |
+
return chat_history + [(message, f"System Error: {self.initialization_error}")]
|
88 |
return chat_history + [(message, "Error: Please initialize the model first")]
|
89 |
|
90 |
try:
|
|
|
110 |
|
111 |
return chat_history + [(message, response)]
|
112 |
except Exception as e:
|
113 |
+
return chat_history + [(message, f"Error during processing: {str(e)}")]
|
114 |
|
115 |
# ========== Gradio Interface ==========
|
116 |
def create_interface():
|
|
|
124 |
init_btn = gr.Button("Initialize TxAgent", variant="primary")
|
125 |
init_status = gr.Textbox(label="Status", interactive=False)
|
126 |
|
127 |
+
# Chat Interface
|
128 |
chatbot = gr.Chatbot(
|
129 |
height=600,
|
130 |
label="Conversation",
|
131 |
show_label=True,
|
|
|
|
|
|
|
|
|
132 |
show_copy_button=True
|
133 |
)
|
134 |
|
|
|
160 |
msg.submit(
|
161 |
app.chat,
|
162 |
[msg, chatbot],
|
163 |
+
chatbot
|
|
|
164 |
)
|
165 |
|
166 |
submit_btn.click(
|
167 |
app.chat,
|
168 |
[msg, chatbot],
|
169 |
+
chatbot
|
|
|
170 |
).then(
|
171 |
lambda: "", None, msg
|
172 |
)
|
|
|
175 |
|
176 |
# ========== Main Execution ==========
|
177 |
if __name__ == "__main__":
|
178 |
+
# Create and configure the interface
|
179 |
interface = create_interface()
|
180 |
|
181 |
+
# Launch configuration
|
182 |
+
launch_params = {
|
183 |
+
'server_name': '0.0.0.0',
|
184 |
+
'server_port': 7860,
|
185 |
+
'share': True
|
186 |
+
}
|
187 |
+
|
188 |
+
# Enable queue if needed (for production)
|
189 |
+
try:
|
190 |
+
interface.queue().launch(**launch_params)
|
191 |
+
except Exception as e:
|
192 |
+
print(f"Error launching interface: {e}")
|
193 |
+
interface.launch(**launch_params) # Fallback without queue
|