Update app.py
Browse files
app.py
CHANGED
|
@@ -161,65 +161,10 @@ agent = CodeAgent(
|
|
| 161 |
prompt_templates=prompt_templates
|
| 162 |
)
|
| 163 |
|
| 164 |
-
# Custom Gradio UI with file download capability
|
| 165 |
-
class CustomGradioUI(GradioUI):
|
| 166 |
-
def build_interface(self):
|
| 167 |
-
with gr.Blocks() as interface:
|
| 168 |
-
with gr.Row():
|
| 169 |
-
with gr.Column(scale=1):
|
| 170 |
-
gr.Markdown("# AI Assistant")
|
| 171 |
-
|
| 172 |
-
chatbot = gr.Chatbot(height=600)
|
| 173 |
-
msg = gr.Textbox(
|
| 174 |
-
placeholder="Ask me anything...",
|
| 175 |
-
container=False,
|
| 176 |
-
scale=7,
|
| 177 |
-
)
|
| 178 |
-
|
| 179 |
-
# Add a file download component
|
| 180 |
-
download_btn = gr.Button("Download File", visible=False)
|
| 181 |
-
file_output = gr.File(label="Generated Document", visible=False)
|
| 182 |
-
|
| 183 |
-
# Store the latest file path
|
| 184 |
-
self._latest_file_path = None # Instance variable to store path between calls
|
| 185 |
-
|
| 186 |
-
def respond(message, chat_history):
|
| 187 |
-
agent_response = self.agent.run(message)
|
| 188 |
-
chat_history.append((message, agent_response))
|
| 189 |
-
|
| 190 |
-
show_download = False
|
| 191 |
-
# Reset latest_file_path to ensure it's fresh for each response
|
| 192 |
-
# self._latest_file_path = None # Actually, should be set if file is found
|
| 193 |
|
| 194 |
-
# Look for generated file paths in the agent's response.
|
| 195 |
-
# tempfile.mkdtemp() usually creates paths like /tmp/some_random_string/
|
| 196 |
-
# The tool consistently names files "generated_document.ext"
|
| 197 |
-
paths = re.findall(r'/tmp/[^/]+/generated_document\.(docx|pdf|txt)', agent_response)
|
| 198 |
-
if paths:
|
| 199 |
-
self._latest_file_path = paths[0] # Store the full path
|
| 200 |
-
show_download = True
|
| 201 |
-
else:
|
| 202 |
-
self._latest_file_path = None # Clear if no path found in this response
|
| 203 |
-
|
| 204 |
-
return chat_history, gr.Button.update(visible=show_download), gr.File.update(visible=False) # Keep file output hidden until download clicked
|
| 205 |
-
|
| 206 |
-
def prepare_download():
|
| 207 |
-
if self._latest_file_path and os.path.exists(self._latest_file_path):
|
| 208 |
-
return gr.File.update(value=self._latest_file_path, visible=True)
|
| 209 |
-
# Handle case where file might have been cleaned up or path is stale
|
| 210 |
-
gr.Warning("File not available for download. It might have been temporary or an error occurred.")
|
| 211 |
-
return gr.File.update(visible=False)
|
| 212 |
-
|
| 213 |
-
# Connect the components
|
| 214 |
-
msg.submit(respond, [msg, chatbot], [chatbot, download_btn, file_output])
|
| 215 |
-
download_btn.click(prepare_download, [], [file_output])
|
| 216 |
-
|
| 217 |
-
gr.Markdown("Powered by smolagents and Qwen") # Qwen is mentioned, perhaps the HF endpoint uses it.
|
| 218 |
-
|
| 219 |
-
return interface
|
| 220 |
|
| 221 |
# Ensure this conditional execution if the script can also be imported
|
| 222 |
if __name__ == "__main__":
|
| 223 |
# Note: The original script had GradioUI(agent).launch() directly.
|
| 224 |
# Using the custom UI:
|
| 225 |
-
|
|
|
|
| 161 |
prompt_templates=prompt_templates
|
| 162 |
)
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
# Ensure this conditional execution if the script can also be imported
|
| 167 |
if __name__ == "__main__":
|
| 168 |
# Note: The original script had GradioUI(agent).launch() directly.
|
| 169 |
# Using the custom UI:
|
| 170 |
+
GradioUI(agent).launch()
|