Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel,
|
| 2 |
-
from smolagents import
|
| 3 |
-
from smolagents import GradioUI
|
| 4 |
import datetime
|
| 5 |
import pytz
|
| 6 |
import yaml
|
|
@@ -9,59 +8,38 @@ import gradio as gr
|
|
| 9 |
import os
|
| 10 |
import re
|
| 11 |
|
|
|
|
| 12 |
from tools.final_answer import FinalAnswerTool
|
| 13 |
from tools.visit_webpage import VisitWebpageTool
|
| 14 |
from tools.create_presentation import create_presentation
|
| 15 |
-
|
| 16 |
|
| 17 |
# =========================
|
| 18 |
-
#
|
| 19 |
# =========================
|
| 20 |
|
| 21 |
-
|
| 22 |
@smol_tool
|
| 23 |
def create_document(text: str, format: str = "docx") -> str:
|
| 24 |
"""
|
| 25 |
Creates a document file (DOCX or PDF) containing the provided text.
|
| 26 |
-
|
| 27 |
Args:
|
| 28 |
text: The full textual content that will be written into the document.
|
| 29 |
format: The output file format. Must be either "docx" or "pdf".
|
| 30 |
-
|
| 31 |
-
Returns:
|
| 32 |
-
The absolute file path to the generated document.
|
| 33 |
"""
|
| 34 |
try:
|
| 35 |
import docx
|
| 36 |
from docx.shared import Pt
|
| 37 |
-
|
| 38 |
temp_dir = tempfile.mkdtemp()
|
| 39 |
doc = docx.Document()
|
| 40 |
doc.add_heading("Generated Document", 0)
|
| 41 |
-
|
| 42 |
-
style = doc.styles["Normal"]
|
| 43 |
-
font = style.font
|
| 44 |
-
font.name = "Calibri"
|
| 45 |
-
font.size = Pt(11)
|
| 46 |
-
|
| 47 |
for paragraph in text.split("\n"):
|
| 48 |
if paragraph.strip():
|
| 49 |
doc.add_paragraph(paragraph)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
doc.save(
|
| 53 |
-
|
| 54 |
-
if format.lower() == "pdf":
|
| 55 |
-
try:
|
| 56 |
-
from docx2pdf import convert
|
| 57 |
-
pdf_path = os.path.join(temp_dir, "generated_document.pdf")
|
| 58 |
-
convert(docx_path, pdf_path)
|
| 59 |
-
return pdf_path
|
| 60 |
-
except ImportError:
|
| 61 |
-
return docx_path
|
| 62 |
-
|
| 63 |
-
return docx_path
|
| 64 |
-
|
| 65 |
except Exception as e:
|
| 66 |
return f"Error creating document: {str(e)}"
|
| 67 |
|
|
@@ -69,45 +47,36 @@ def create_document(text: str, format: str = "docx") -> str:
|
|
| 69 |
def create_presentation_preview(title: str, slides: list[str]) -> list[str]:
|
| 70 |
"""
|
| 71 |
Create a presentation and return slide previews as images.
|
| 72 |
-
|
| 73 |
Args:
|
| 74 |
title: Presentation title
|
| 75 |
slides: List of slide contents
|
| 76 |
-
|
| 77 |
-
Returns:
|
| 78 |
-
List of image paths (PNG), one per slide
|
| 79 |
"""
|
| 80 |
-
from
|
| 81 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 82 |
-
import tempfile, os
|
| 83 |
-
|
| 84 |
temp_dir = tempfile.mkdtemp()
|
| 85 |
image_paths = []
|
| 86 |
-
|
| 87 |
for i, slide_text in enumerate(slides, start=1):
|
| 88 |
-
img = Image.new("RGB", (
|
| 89 |
draw = ImageDraw.Draw(img)
|
| 90 |
-
draw.text((
|
| 91 |
-
|
| 92 |
path = os.path.join(temp_dir, f"slide_{i}.png")
|
| 93 |
img.save(path)
|
| 94 |
image_paths.append(path)
|
| 95 |
-
|
| 96 |
return image_paths
|
| 97 |
|
| 98 |
-
|
| 99 |
# =========================
|
| 100 |
# AGENT SETUP
|
| 101 |
# =========================
|
| 102 |
final_answer = FinalAnswerTool()
|
| 103 |
visit_webpage = VisitWebpageTool()
|
|
|
|
| 104 |
|
| 105 |
model = HfApiModel(
|
| 106 |
max_tokens=2096,
|
| 107 |
temperature=0.5,
|
| 108 |
-
model_id="Qwen/
|
| 109 |
)
|
| 110 |
|
|
|
|
| 111 |
with open("prompts.yaml", "r") as stream:
|
| 112 |
prompt_templates = yaml.safe_load(stream)
|
| 113 |
|
|
@@ -116,76 +85,62 @@ agent = CodeAgent(
|
|
| 116 |
tools=[
|
| 117 |
final_answer,
|
| 118 |
visit_webpage,
|
|
|
|
| 119 |
create_document,
|
| 120 |
create_presentation,
|
| 121 |
create_presentation_preview
|
| 122 |
],
|
| 123 |
-
max_steps=
|
| 124 |
-
verbosity_level=
|
| 125 |
prompt_templates=prompt_templates,
|
| 126 |
)
|
| 127 |
|
| 128 |
-
|
| 129 |
# =========================
|
| 130 |
-
#
|
| 131 |
# =========================
|
| 132 |
class CustomGradioUI(GradioUI):
|
| 133 |
def build_interface(self):
|
| 134 |
-
with gr.Blocks() as interface:
|
| 135 |
-
gr.Markdown("# AI
|
|
|
|
| 136 |
|
| 137 |
-
chatbot = gr.Chatbot(height=
|
| 138 |
-
|
| 139 |
-
placeholder="
|
| 140 |
-
|
| 141 |
-
)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
|
|
|
| 145 |
|
| 146 |
self._latest_file_path = None
|
| 147 |
|
| 148 |
def respond(message, chat_history):
|
|
|
|
| 149 |
agent_response = self.agent.run(message)
|
| 150 |
-
chat_history.append((message, agent_response))
|
| 151 |
|
|
|
|
| 152 |
self._latest_file_path = None
|
| 153 |
show_download = False
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
match = re.search(
|
| 157 |
-
r"(/tmp/.+?/generated_[\w-]+\.(docx|pdf|pptx))",
|
| 158 |
-
agent_response,
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
if match:
|
| 162 |
self._latest_file_path = match.group(1)
|
| 163 |
show_download = True
|
| 164 |
|
| 165 |
-
return (
|
| 166 |
-
chat_history,
|
| 167 |
-
gr.Button.update(visible=show_download),
|
| 168 |
-
gr.File.update(visible=False),
|
| 169 |
-
)
|
| 170 |
|
| 171 |
def prepare_download():
|
| 172 |
if self._latest_file_path and os.path.exists(self._latest_file_path):
|
| 173 |
-
return gr.
|
| 174 |
-
|
| 175 |
-
visible=True,
|
| 176 |
-
)
|
| 177 |
-
return gr.File.update(visible=False)
|
| 178 |
|
| 179 |
msg.submit(respond, [msg, chatbot], [chatbot, download_btn, file_output])
|
|
|
|
| 180 |
download_btn.click(prepare_download, [], [file_output])
|
| 181 |
|
| 182 |
-
gr.Markdown("Powered by smolagents + Qwen")
|
| 183 |
-
|
| 184 |
return interface
|
| 185 |
|
| 186 |
-
|
| 187 |
-
# =========================
|
| 188 |
-
# 🚀 ENTRY POINT (CRITICAL)
|
| 189 |
-
# =========================
|
| 190 |
if __name__ == "__main__":
|
| 191 |
-
CustomGradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, tool as smol_tool, GradioUI
|
| 2 |
+
from smolagents.tools import Tool
|
|
|
|
| 3 |
import datetime
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 8 |
import os
|
| 9 |
import re
|
| 10 |
|
| 11 |
+
# Import the tools from your local folder
|
| 12 |
from tools.final_answer import FinalAnswerTool
|
| 13 |
from tools.visit_webpage import VisitWebpageTool
|
| 14 |
from tools.create_presentation import create_presentation
|
| 15 |
+
from tools.web_search import DuckDuckGoSearchTool
|
| 16 |
|
| 17 |
# =========================
|
| 18 |
+
# ADDITIONAL IN-LINE TOOLS
|
| 19 |
# =========================
|
| 20 |
|
|
|
|
| 21 |
@smol_tool
|
| 22 |
def create_document(text: str, format: str = "docx") -> str:
|
| 23 |
"""
|
| 24 |
Creates a document file (DOCX or PDF) containing the provided text.
|
|
|
|
| 25 |
Args:
|
| 26 |
text: The full textual content that will be written into the document.
|
| 27 |
format: The output file format. Must be either "docx" or "pdf".
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
import docx
|
| 31 |
from docx.shared import Pt
|
|
|
|
| 32 |
temp_dir = tempfile.mkdtemp()
|
| 33 |
doc = docx.Document()
|
| 34 |
doc.add_heading("Generated Document", 0)
|
| 35 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
for paragraph in text.split("\n"):
|
| 37 |
if paragraph.strip():
|
| 38 |
doc.add_paragraph(paragraph)
|
| 39 |
|
| 40 |
+
file_path = os.path.join(temp_dir, f"generated_document.{format.lower()}")
|
| 41 |
+
doc.save(file_path)
|
| 42 |
+
return file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
return f"Error creating document: {str(e)}"
|
| 45 |
|
|
|
|
| 47 |
def create_presentation_preview(title: str, slides: list[str]) -> list[str]:
|
| 48 |
"""
|
| 49 |
Create a presentation and return slide previews as images.
|
|
|
|
| 50 |
Args:
|
| 51 |
title: Presentation title
|
| 52 |
slides: List of slide contents
|
|
|
|
|
|
|
|
|
|
| 53 |
"""
|
| 54 |
+
from PIL import Image, ImageDraw
|
|
|
|
|
|
|
|
|
|
| 55 |
temp_dir = tempfile.mkdtemp()
|
| 56 |
image_paths = []
|
|
|
|
| 57 |
for i, slide_text in enumerate(slides, start=1):
|
| 58 |
+
img = Image.new("RGB", (800, 450), "white")
|
| 59 |
draw = ImageDraw.Draw(img)
|
| 60 |
+
draw.text((40, 40), f"{title}\n\nSlide {i}:\n{slide_text[:100]}", fill="black")
|
|
|
|
| 61 |
path = os.path.join(temp_dir, f"slide_{i}.png")
|
| 62 |
img.save(path)
|
| 63 |
image_paths.append(path)
|
|
|
|
| 64 |
return image_paths
|
| 65 |
|
|
|
|
| 66 |
# =========================
|
| 67 |
# AGENT SETUP
|
| 68 |
# =========================
|
| 69 |
final_answer = FinalAnswerTool()
|
| 70 |
visit_webpage = VisitWebpageTool()
|
| 71 |
+
web_search = DuckDuckGoSearchTool()
|
| 72 |
|
| 73 |
model = HfApiModel(
|
| 74 |
max_tokens=2096,
|
| 75 |
temperature=0.5,
|
| 76 |
+
model_id="Qwen/Qwen2.5-72B-Instruct", # Updated to a widely available stable ID
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# Load the prompt templates we created earlier
|
| 80 |
with open("prompts.yaml", "r") as stream:
|
| 81 |
prompt_templates = yaml.safe_load(stream)
|
| 82 |
|
|
|
|
| 85 |
tools=[
|
| 86 |
final_answer,
|
| 87 |
visit_webpage,
|
| 88 |
+
web_search, # Added from your tools folder
|
| 89 |
create_document,
|
| 90 |
create_presentation,
|
| 91 |
create_presentation_preview
|
| 92 |
],
|
| 93 |
+
max_steps=10,
|
| 94 |
+
verbosity_level=1,
|
| 95 |
prompt_templates=prompt_templates,
|
| 96 |
)
|
| 97 |
|
|
|
|
| 98 |
# =========================
|
| 99 |
+
# UI CUSTOMIZATION
|
| 100 |
# =========================
|
| 101 |
class CustomGradioUI(GradioUI):
|
| 102 |
def build_interface(self):
|
| 103 |
+
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
| 104 |
+
gr.Markdown("# 🚀 Enterprise AI Agent")
|
| 105 |
+
gr.Markdown("Search the web, create docs, and generate PowerPoints instantly.")
|
| 106 |
|
| 107 |
+
chatbot = gr.Chatbot(height=500, show_label=False)
|
| 108 |
+
with gr.Row():
|
| 109 |
+
msg = gr.Textbox(placeholder="E.g., 'Research AI trends and make a 5-slide PPT'", scale=9)
|
| 110 |
+
submit = gr.Button("Send", variant="primary", scale=1)
|
|
|
|
| 111 |
|
| 112 |
+
with gr.Row():
|
| 113 |
+
download_btn = gr.Button("📂 Download Generated File", visible=False)
|
| 114 |
+
file_output = gr.File(label="Ready for Download", visible=False)
|
| 115 |
|
| 116 |
self._latest_file_path = None
|
| 117 |
|
| 118 |
def respond(message, chat_history):
|
| 119 |
+
# Run the agent
|
| 120 |
agent_response = self.agent.run(message)
|
| 121 |
+
chat_history.append((message, str(agent_response)))
|
| 122 |
|
| 123 |
+
# File detection logic
|
| 124 |
self._latest_file_path = None
|
| 125 |
show_download = False
|
| 126 |
+
match = re.search(r"(/tmp/[\w\d_-]+/generated_[\w-]+\.(docx|pdf|pptx))", str(agent_response))
|
| 127 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
if match:
|
| 129 |
self._latest_file_path = match.group(1)
|
| 130 |
show_download = True
|
| 131 |
|
| 132 |
+
return chat_history, gr.update(visible=show_download), gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
def prepare_download():
|
| 135 |
if self._latest_file_path and os.path.exists(self._latest_file_path):
|
| 136 |
+
return gr.update(value=self._latest_file_path, visible=True)
|
| 137 |
+
return gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
msg.submit(respond, [msg, chatbot], [chatbot, download_btn, file_output])
|
| 140 |
+
submit.click(respond, [msg, chatbot], [chatbot, download_btn, file_output])
|
| 141 |
download_btn.click(prepare_download, [], [file_output])
|
| 142 |
|
|
|
|
|
|
|
| 143 |
return interface
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
if __name__ == "__main__":
|
| 146 |
+
CustomGradioUI(agent).launch()
|