Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,9 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
from tools.visit_webpage import VisitWebpageTool
|
| 8 |
#from tools.web_shearch import DuckDuckGoSearchTool
|
| 9 |
from smolagents import GradioUI
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
'''
|
| 12 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
@@ -36,6 +39,80 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 36 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 37 |
|
| 38 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
final_answer = FinalAnswerTool()
|
| 41 |
#web_search=DuckDuckGoSearchTool()
|
|
@@ -63,7 +140,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 63 |
|
| 64 |
agent = CodeAgent(
|
| 65 |
model=model,
|
| 66 |
-
tools=[final_answer,visit_webpage], ## add your tools here (don't remove final answer)
|
| 67 |
max_steps=6,
|
| 68 |
verbosity_level=1,
|
| 69 |
grammar=None,
|
|
@@ -73,5 +150,58 @@ agent = CodeAgent(
|
|
| 73 |
prompt_templates=prompt_templates
|
| 74 |
)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
GradioUI(agent).launch()
|
|
|
|
| 7 |
from tools.visit_webpage import VisitWebpageTool
|
| 8 |
#from tools.web_shearch import DuckDuckGoSearchTool
|
| 9 |
from smolagents import GradioUI
|
| 10 |
+
import tempfile
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import os
|
| 13 |
|
| 14 |
'''
|
| 15 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
| 39 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 40 |
|
| 41 |
'''
|
| 42 |
+
@tool
|
| 43 |
+
def create_document(text: str, format: str = "docx") -> str:
|
| 44 |
+
"""Creates a document with the provided text and allows download.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
text: The text content to write to the document
|
| 48 |
+
format: The output format, either 'docx' or 'pdf'
|
| 49 |
+
"""
|
| 50 |
+
try:
|
| 51 |
+
import docx
|
| 52 |
+
from docx.shared import Pt
|
| 53 |
+
|
| 54 |
+
# Create a temp directory to store files
|
| 55 |
+
temp_dir = tempfile.mkdtemp()
|
| 56 |
+
|
| 57 |
+
# Create a new document
|
| 58 |
+
doc = docx.Document()
|
| 59 |
+
|
| 60 |
+
# Add a heading
|
| 61 |
+
doc.add_heading('Generated Document', 0)
|
| 62 |
+
|
| 63 |
+
# Set font style for regular text
|
| 64 |
+
style = doc.styles['Normal']
|
| 65 |
+
font = style.font
|
| 66 |
+
font.name = 'Calibri'
|
| 67 |
+
font.size = Pt(11)
|
| 68 |
+
|
| 69 |
+
# Add paragraphs from the input text
|
| 70 |
+
# Split by newlines to maintain paragraph structure
|
| 71 |
+
for paragraph in text.split('\n'):
|
| 72 |
+
if paragraph.strip(): # Skip empty paragraphs
|
| 73 |
+
doc.add_paragraph(paragraph)
|
| 74 |
+
|
| 75 |
+
# Save the document
|
| 76 |
+
docx_path = os.path.join(temp_dir, "generated_document.docx")
|
| 77 |
+
doc.save(docx_path)
|
| 78 |
+
|
| 79 |
+
# Convert to PDF if requested
|
| 80 |
+
if format.lower() == "pdf":
|
| 81 |
+
try:
|
| 82 |
+
from docx2pdf import convert
|
| 83 |
+
pdf_path = os.path.join(temp_dir, "generated_document.pdf")
|
| 84 |
+
convert(docx_path, pdf_path)
|
| 85 |
+
return pdf_path
|
| 86 |
+
except ImportError:
|
| 87 |
+
return f"PDF conversion requires docx2pdf package. Document saved as DOCX instead at: {docx_path}"
|
| 88 |
+
|
| 89 |
+
return docx_path
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"Error creating document: {str(e)}"
|
| 93 |
+
|
| 94 |
+
# Custom file download tool to help with file handling
|
| 95 |
+
@tool
|
| 96 |
+
def get_file_download_link(file_path: str) -> str:
|
| 97 |
+
"""Creates a download link for a file.
|
| 98 |
+
|
| 99 |
+
Args:
|
| 100 |
+
file_path: Path to the file that should be made available for download
|
| 101 |
+
"""
|
| 102 |
+
if not os.path.exists(file_path):
|
| 103 |
+
return f"Error: File not found at {file_path}"
|
| 104 |
+
|
| 105 |
+
# Get file extension and set up appropriate mime type
|
| 106 |
+
_, file_extension = os.path.splitext(file_path)
|
| 107 |
+
mime_types = {
|
| 108 |
+
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
| 109 |
+
'.pdf': 'application/pdf',
|
| 110 |
+
}
|
| 111 |
+
mime_type = mime_types.get(file_extension.lower(), 'application/octet-stream')
|
| 112 |
+
|
| 113 |
+
# Return information that can be used by the agent to instruct the user
|
| 114 |
+
return f"File ready for download: {os.path.basename(file_path)} ({mime_type})"
|
| 115 |
+
|
| 116 |
|
| 117 |
final_answer = FinalAnswerTool()
|
| 118 |
#web_search=DuckDuckGoSearchTool()
|
|
|
|
| 140 |
|
| 141 |
agent = CodeAgent(
|
| 142 |
model=model,
|
| 143 |
+
tools=[final_answer,visit_webpage,create_document,get_file_download_link], ## add your tools here (don't remove final answer)
|
| 144 |
max_steps=6,
|
| 145 |
verbosity_level=1,
|
| 146 |
grammar=None,
|
|
|
|
| 150 |
prompt_templates=prompt_templates
|
| 151 |
)
|
| 152 |
|
| 153 |
+
# Custom Gradio UI with file download capability
|
| 154 |
+
class CustomGradioUI(GradioUI):
|
| 155 |
+
def build_interface(self):
|
| 156 |
+
with gr.Blocks() as interface:
|
| 157 |
+
with gr.Row():
|
| 158 |
+
with gr.Column(scale=1):
|
| 159 |
+
gr.Markdown("# AI Assistant")
|
| 160 |
+
|
| 161 |
+
chatbot = gr.Chatbot(height=600)
|
| 162 |
+
msg = gr.Textbox(
|
| 163 |
+
placeholder="Ask me anything...",
|
| 164 |
+
container=False,
|
| 165 |
+
scale=7,
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
# Add a file download component
|
| 169 |
+
download_btn = gr.Button("Download File", visible=False)
|
| 170 |
+
file_output = gr.File(label="Generated Document", visible=False)
|
| 171 |
+
|
| 172 |
+
# Store the latest file path
|
| 173 |
+
self._latest_file_path = None
|
| 174 |
+
|
| 175 |
+
def respond(message, chat_history):
|
| 176 |
+
agent_response = self.agent.run(message)
|
| 177 |
+
chat_history.append((message, agent_response))
|
| 178 |
+
|
| 179 |
+
# Check if response contains a file path
|
| 180 |
+
import re
|
| 181 |
+
file_paths = re.findall(r'File ready for download: .+ \((application/[\w.+-]+)\)', agent_response)
|
| 182 |
+
|
| 183 |
+
show_download = False
|
| 184 |
+
self._latest_file_path = None
|
| 185 |
+
|
| 186 |
+
# Look for generated file paths in the response
|
| 187 |
+
paths = re.findall(r'/tmp/\w+/generated_document\.(docx|pdf)', agent_response)
|
| 188 |
+
if paths:
|
| 189 |
+
self._latest_file_path = paths[0]
|
| 190 |
+
show_download = True
|
| 191 |
+
|
| 192 |
+
return chat_history, gr.Button.update(visible=show_download), gr.File.update(visible=False)
|
| 193 |
+
|
| 194 |
+
def prepare_download():
|
| 195 |
+
if self._latest_file_path:
|
| 196 |
+
return gr.File.update(value=self._latest_file_path, visible=True)
|
| 197 |
+
return gr.File.update(visible=False)
|
| 198 |
+
|
| 199 |
+
# Connect the components
|
| 200 |
+
msg.submit(respond, [msg, chatbot], [chatbot, download_btn, file_output])
|
| 201 |
+
download_btn.click(prepare_download, [], [file_output])
|
| 202 |
+
|
| 203 |
+
gr.Markdown("Powered by smolagents and Qwen")
|
| 204 |
+
|
| 205 |
+
return interface
|
| 206 |
|
| 207 |
GradioUI(agent).launch()
|