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
0cc5955
 
 
9a2a3bc
0cc5955
 
 
 
9a2a3bc
0cc5955
 
deafbd7
 
9a2a3bc
 
 
0cc5955
9a2a3bc
0cc5955
 
9a2a3bc
 
0cc5955
9a2a3bc
 
 
 
 
0cc5955
9a2a3bc
0cc5955
 
9a2a3bc
0cc5955
 
9a2a3bc
0cc5955
 
9a2a3bc
 
 
0cc5955
9a2a3bc
 
0cc5955
 
 
9a2a3bc
 
 
 
 
 
 
 
0cc5955
9a2a3bc
 
 
0cc5955
 
9a2a3bc
0cc5955
9a2a3bc
 
0cc5955
deafbd7
0cc5955
deafbd7
 
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python
# coding=utf-8
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import mimetypes
import os
import re
import shutil
from typing import Optional
import tempfile # Added for PIL image saving
from PIL import Image as PILImage # Added for PIL image handling

from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
from smolagents.agents import ActionStep, MultiStepAgent 
from smolagents.memory import MemoryStep
from smolagents.utils import _is_package_available
import gradio as gr # Ensure gradio is imported at the top level


def pull_messages_from_step_dict(step_log: MemoryStep):
    """Extract messages as dicts for Gradio type='messages' Chatbot"""
    if isinstance(step_log, ActionStep):
        step_number_str = f"Step {step_log.step_number}" if step_log.step_number is not None else "Processing"
        yield {"role": "assistant", "content": f"**{step_number_str}**"}

        if hasattr(step_log, "model_output") and step_log.model_output is not None:
            model_output = step_log.model_output.strip()
            # More robust cleaning for <end_code> potentially wrapped in backticks or with newlines
            model_output = re.sub(r"```\s*<end_code>[\s\S]*|[\s\S]*<end_code>\s*```", "```", model_output, flags=re.DOTALL)
            model_output = re.sub(r"<end_code>", "", model_output) # Remove standalone tag
            model_output = model_output.strip()
            yield {"role": "assistant", "content": model_output}

        if hasattr(step_log, "tool_calls") and step_log.tool_calls:
            tc = step_log.tool_calls[0] # Process first tool call for simplicity in this format
            tool_info_md = f"🛠️ **Tool Used: {tc.name}**\n"
            
            args = tc.arguments
            if isinstance(args, dict):
                args_str = str(args.get("answer", str(args)))
            else:
                args_str = str(args).strip()
            
            if tc.name == "python_interpreter":
                code_content = args_str
                # Clean up common wrapping issues
                code_content = re.sub(r"^```python\s*\n?", "", code_content)
                code_content = re.sub(r"\n?```\s*$", "", code_content)
                code_content = re.sub(r"^\s*<end_code>\s*", "", code_content)
                code_content = re.sub(r"\s*<end_code>\s*$", "", code_content)
                code_content = code_content.strip()
                tool_info_md += f"Executing Code:\n```python\n{code_content}\n```\n"
            else:
                tool_info_md += f"Arguments: `{args_str}`\n"

            if hasattr(step_log, "observations") and step_log.observations and step_log.observations.strip():
                obs_content = step_log.observations.strip()
                # Remove "Execution logs:" prefix if present for cleaner display
                obs_content = re.sub(r"^Execution logs:\s*", "", obs_content).strip()
                if obs_content: # Only show if there's something after stripping
                    tool_info_md += f"📝 **Tool Output/Logs:**\n```\n{obs_content}\n```\n"
            
            if hasattr(step_log, "error") and step_log.error:
                tool_info_md += f"💥 **Error:** {str(step_log.error)}\n"
            
            yield {"role": "assistant", "content": tool_info_md.strip()}

        elif hasattr(step_log, "error") and step_log.error: # Standalone error not from a tool call
            yield {"role": "assistant", "content": f"💥 **Error:** {str(step_log.error)}"}
        
        # --- Minimal footnote for type="messages" ---
        footnote_parts = []
        if step_log.step_number is not None:
            footnote_parts.append(f"Step {step_log.step_number}")
        if hasattr(step_log, "duration") and step_log.duration is not None:
            footnote_parts.append(f"Duration: {round(float(step_log.duration), 2)}s")
        if hasattr(step_log, "input_token_count") and step_log.input_token_count is not None: # Check for None
             footnote_parts.append(f"InTokens: {step_log.input_token_count:,}")
        if hasattr(step_log, "output_token_count") and step_log.output_token_count is not None: # Check for None
             footnote_parts.append(f"OutTokens: {step_log.output_token_count:,}")
        
        if footnote_parts:
            footnote_text = " | ".join(footnote_parts)
            yield {"role": "assistant", "content": f"""<p style="color: #999; font-size: 0.8em; margin-top:0; margin-bottom:0;">{footnote_text}</p>"""}
        yield {"role": "assistant", "content": "---"} # Separator


def stream_to_gradio(
    agent,
    task: str,
    reset_agent_memory: bool = False,
    additional_args: Optional[dict] = None,
):
    """Runs an agent, yields message dicts for Gradio type='messages' Chatbot."""
    if not _is_package_available("gradio"):
        raise ModuleNotFoundError("Install 'gradio': `pip install 'smolagents[gradio]'`")

    if hasattr(agent, 'interaction_logs'): # Clear logs for this new agent run
        agent.interaction_logs.clear()
        print("DEBUG Gradio: Cleared agent interaction_logs for new run.")

    for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
        if hasattr(agent.model, "last_input_token_count") and agent.model.last_input_token_count is not None:
            if isinstance(step_log, ActionStep):
                step_log.input_token_count = agent.model.last_input_token_count
                step_log.output_token_count = agent.model.last_output_token_count
        
        for msg_dict in pull_messages_from_step_dict(step_log): # Use new dict-yielding function
            yield msg_dict

    final_answer_content = step_log # Last step_log is the final output/state

    # --- Handle final answer for type="messages" ---
    if isinstance(final_answer_content, PILImage.Image):
        print("DEBUG Gradio (stream_to_gradio): Final answer is raw PIL Image.")
        try:
            with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
                final_answer_content.save(tmp_file, format="PNG")
                image_path_for_gradio = tmp_file.name
            print(f"DEBUG Gradio: Saved PIL image to temp path: {image_path_for_gradio}")
            # For Gradio type="messages", image content is just the path string
            yield {"role": "assistant", "content": image_path_for_gradio} 
            return 
        except Exception as e:
            print(f"DEBUG Gradio: Error saving PIL image from final_answer_content: {e}")
            yield {"role": "assistant", "content": f"**Final Answer (Error displaying image):** {e}"}
            return

    final_answer_processed = handle_agent_output_types(final_answer_content)

    if isinstance(final_answer_processed, AgentText):
        yield {"role": "assistant", "content": f"**Final Answer:**\n{final_answer_processed.to_string()}"}
    elif isinstance(final_answer_processed, AgentImage):
        image_path = final_answer_processed.to_string()
        print(f"DEBUG Gradio (stream_to_gradio): AgentImage path: {image_path}")
        if image_path and os.path.exists(image_path):
             yield {"role": "assistant", "content": image_path} 
        else:
            err_msg = f"Error: Image path from AgentImage not found or invalid ('{image_path}')"
            print(f"DEBUG Gradio: {err_msg}")
            yield {"role": "assistant", "content": f"**Final Answer ({err_msg})**"}
    elif isinstance(final_answer_processed, AgentAudio):
        audio_path = final_answer_processed.to_string()
        print(f"DEBUG Gradio (stream_to_gradio): AgentAudio path: {audio_path}")
        if audio_path and os.path.exists(audio_path):
            yield {"role": "assistant", "content": audio_path}
        else:
            err_msg = f"Error: Audio path from AgentAudio not found ('{audio_path}')"
            print(f"DEBUG Gradio: {err_msg}")
            yield {"role": "assistant", "content": f"**Final Answer ({err_msg})**"}
    else: 
        yield {"role": "assistant", "content": f"**Final Answer:**\n{str(final_answer_processed)}"}


class GradioUI:
    """A one-line interface to launch your agent in Gradio"""

    def __init__(self, agent: MultiStepAgent, file_upload_folder: str | None = None):
        if not _is_package_available("gradio"):
            raise ModuleNotFoundError("Install 'gradio': `pip install 'smolagents[gradio]'`")
        self.agent = agent
        self.file_upload_folder = file_upload_folder
        if self.file_upload_folder is not None:
            if not os.path.exists(self.file_upload_folder):
                os.makedirs(self.file_upload_folder, exist_ok=True)
        self._latest_file_path_for_download = None

    def _check_for_created_file(self):
        self._latest_file_path_for_download = None 
        if hasattr(self.agent, 'interaction_logs') and self.agent.interaction_logs:
            print(f"DEBUG Gradio UI: Checking {len(self.agent.interaction_logs)} interaction log entries for created files.")
            for log_entry in reversed(self.agent.interaction_logs): # Check recent logs first
                if isinstance(log_entry, ActionStep) and hasattr(log_entry, 'tool_calls') and log_entry.tool_calls:
                    for tool_call in log_entry.tool_calls:
                        if tool_call.name == "create_document":
                            tool_output_value = getattr(log_entry, 'observations', None)
                            print(f"DEBUG Gradio UI: Log for 'create_document' call, observed output: {tool_output_value}")
                            if tool_output_value and isinstance(tool_output_value, str):
                                # Try to extract path if it's wrapped, e.g. by "Execution logs:"
                                cleaned_output = re.sub(r"^Execution logs:\s*", "", tool_output_value).strip()
                                path_match = re.search(r"(/tmp/[a-zA-Z0-9_]+/generated_document\.(?:docx|pdf|txt))", cleaned_output)
                                extracted_path = path_match.group(1) if path_match else cleaned_output

                                if not extracted_path.lower().startswith("error:"):
                                    normalized_path = os.path.normpath(extracted_path)
                                    if os.path.exists(normalized_path):
                                        self._latest_file_path_for_download = normalized_path
                                        print(f"DEBUG Gradio UI: File path for download set: {self._latest_file_path_for_download}")
                                        return True 
                                    else:
                                        print(f"DEBUG Gradio UI: Path from 'create_document' log ('{normalized_path}') does not exist.")
                                else:
                                    print(f"DEBUG Gradio UI: 'create_document' tool reported error in observations: {extracted_path}")
        print("DEBUG Gradio UI: No valid 'create_document' output found for download.")
        return False

    def interact_with_agent(self, prompt_text: str, current_chat_tuples: list):
        # current_chat_tuples is the history from the chatbot (list of lists/tuples)
        # Convert to 'messages' format if needed, or adapt stream_to_gradio if chatbot is not type="messages"
        # For type="messages", current_chat_tuples is already list of dicts.
        
        print(f"DEBUG Gradio: interact_with_agent called with prompt: '{prompt_text}'")
        print(f"DEBUG Gradio: Current chat history (input): {current_chat_tuples}")

        # Add user's new message to the chat history list
        current_chat_messages = current_chat_tuples + [{"role": "user", "content": prompt_text}]
        
        # Initial yield to show user message immediately and hide download items
        yield current_chat_messages, gr.update(visible=False), gr.update(value=None, visible=False)

        # Stream agent messages
        agent_responses_for_history = []
        for msg_dict in stream_to_gradio(self.agent, task=prompt_text, reset_agent_memory=False):
            agent_responses_for_history.append(msg_dict)
            # Yield progressively: current user message + all agent messages so far
            yield current_chat_messages + agent_responses_for_history, gr.update(visible=False), gr.update(value=None, visible=False) 

        # After streaming all agent messages, check for created file
        file_found = self._check_for_created_file()
        
        # Final state for UI components
        final_chat_display = current_chat_messages + agent_responses_for_history
        print(f"DEBUG Gradio: Final chat history for display: {final_chat_display}")
        yield final_chat_display, gr.update(visible=file_found), gr.update(value=None, visible=False)


    def upload_file(self, file, file_uploads_log_state):
        if file is None: # No file selected
            return gr.update(value="No file uploaded.", visible=True), file_uploads_log_state

        # Ensure file_upload_folder exists (it should from __init__)
        if not self.file_upload_folder or not os.path.exists(self.file_upload_folder):
            os.makedirs(self.file_upload_folder, exist_ok=True) # Defensive check

        allowed_file_types = [
            "application/pdf",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            "text/plain",
            "image/jpeg", "image/png", # Added image types
        ]
        
        # Gradio File object has 'name' (temp path) and 'orig_name'
        original_name = file.orig_name if hasattr(file, 'orig_name') else os.path.basename(file.name)
        
        # Try to guess mime type from temp file name first, then from original name if needed
        mime_type, _ = mimetypes.guess_type(file.name) 
        if mime_type is None: # Fallback
            mime_type, _ = mimetypes.guess_type(original_name)

        if mime_type not in allowed_file_types:
            return gr.update(value=f"File type '{mime_type or 'unknown'}' for '{original_name}' is disallowed.", visible=True), file_uploads_log_state

        sanitized_name = re.sub(r"[^\w\-.]", "_", original_name)
        base_name, current_ext = os.path.splitext(sanitized_name)
        
        type_to_ext_map = {v: k for k, v_list in mimetypes. প্রেফারেন্সেস.items() for v in v_list} # More robust ext map
        type_to_ext_map.update({ # Manual overrides / common types
            "application/pdf": ".pdf",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
            "text/plain": ".txt", "image/jpeg": ".jpg", "image/png": ".png"
        })
        expected_ext = type_to_ext_map.get(mime_type)

        if expected_ext and current_ext.lower() != expected_ext.lower():
            sanitized_name = base_name + expected_ext
        
        destination_path = os.path.join(self.file_upload_folder, sanitized_name)
        
        try:
            shutil.copy(file.name, destination_path) # file.name is the temp path from Gradio
            print(f"DEBUG Gradio: File '{original_name}' copied to '{destination_path}'")
            updated_log = file_uploads_log_state + [destination_path]
            return gr.update(value=f"Uploaded: {original_name} (as {sanitized_name})", visible=True), updated_log
        except Exception as e:
            print(f"DEBUG Gradio: Error copying uploaded file: {e}")
            return gr.update(value=f"Error uploading {original_name}: {e}", visible=True), file_uploads_log_state


    def log_user_message(self, text_input_value: str, current_file_uploads: list):
        full_prompt = text_input_value
        if current_file_uploads:
            files_str = ", ".join([os.path.basename(f) for f in current_file_uploads])
            full_prompt += f"\n\n[Uploaded files for context: {files_str}]"
        print(f"DEBUG Gradio: Prepared prompt for agent: {full_prompt}")
        return full_prompt, "" # Clears the text input box

    def prepare_and_show_download_file(self):
        if self._latest_file_path_for_download and os.path.exists(self._latest_file_path_for_download):
            print(f"DEBUG Gradio UI: Preparing download for UI component: {self._latest_file_path_for_download}")
            return gr.File.update(value=self._latest_file_path_for_download, 
                                  label=os.path.basename(self._latest_file_path_for_download), 
                                  visible=True)
        else:
            print("DEBUG Gradio UI: No valid file path to prepare for download component.")
            gr.Warning("No file available for download or path is invalid.")
            return gr.File.update(visible=False)

    def launch(self, **kwargs):
        with gr.Blocks(fill_height=True, theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue)) as demo:
            file_uploads_log_state = gr.State([]) 
            prepared_prompt_for_agent = gr.State("")

            gr.Markdown("#  agente inteligente")

            with gr.Row():
                with gr.Column(scale=3):
                    chatbot_display = gr.Chatbot(
                        label="Agent Interaction",
                        type="messages", 
                        avatar_images=(None, "https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo-round.png"),
                        height=600,
                        show_copy_button=True,
                        bubble_full_width=False
                    )
                    text_message_input = gr.Textbox(
                        lines=1, 
                        label="Your Message to the Agent", 
                        placeholder="Type your message and press Enter, or Shift+Enter for new line..."
                    )
                
                with gr.Column(scale=1):
                    if self.file_upload_folder is not None:
                        gr.Markdown("### File Upload")
                        file_uploader = gr.File(label="Upload a supporting file (PDF, DOCX, TXT, JPG, PNG)")
                        upload_status_text = gr.Textbox(label="Upload Status", interactive=False, lines=2, max_lines=4)
                        file_uploader.upload(
                            self.upload_file,
                            [file_uploader, file_uploads_log_state],
                            [upload_status_text, file_uploads_log_state],
                        )
                    
                    gr.Markdown("### Generated File")
                    download_action_button = gr.Button("Download Generated File", visible=False) 
                    file_download_display_component = gr.File(label="Downloadable Document", visible=False, interactive=False) 

            # Event Handling Chain for Text Submission
            text_message_input.submit(
                self.log_user_message, # Step 1: Prepare prompt, clear input
                [text_message_input, file_uploads_log_state],
                [prepared_prompt_for_agent, text_message_input] 
            ).then(
                self.interact_with_agent, # Step 2: Run agent, stream to chatbot, update download button
                [prepared_prompt_for_agent, chatbot_display], 
                [chatbot_display, download_action_button, file_download_display_component] 
            )

            download_action_button.click(
                self.prepare_and_show_download_file, 
                [], 
                [file_download_display_component] 
            )

        demo.launch(debug=True, share=kwargs.get("share", False), **kwargs)

__all__ = ["stream_to_gradio", "GradioUI"]