|
import streamlit as st |
|
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel |
|
import tempfile |
|
|
|
|
|
content_agent = CodeAgent( |
|
tools=[DuckDuckGoSearchTool()], |
|
model=HfApiModel() |
|
) |
|
|
|
|
|
def display_agent_activity(prompt, result): |
|
st.write("### Agent Activity:") |
|
st.write("**Prompt Sent to Agent:**") |
|
st.code(prompt, language="text") |
|
st.write("**Agent Output:**") |
|
st.code(result, language="text") |
|
|
|
|
|
def generate_text_download(content, filename="generated_blog.txt"): |
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp_file: |
|
tmp_file.write(content.encode()) |
|
return tmp_file.name, filename |
|
|
|
|
|
def generate_pdf_download(content, filename="generated_blog.pdf"): |
|
from fpdf import FPDF |
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file: |
|
pdf = FPDF() |
|
pdf.add_page() |
|
pdf.set_font("Arial", size=12) |
|
pdf.multi_cell(0, 10, content) |
|
pdf.output(tmp_file.name) |
|
return tmp_file.name, filename |
|
|
|
|
|
st.title("SmolAgents Content Writer") |
|
|
|
|
|
tabs = st.tabs(["Generated Content", "Generated Summary", "Agent Activity"]) |
|
|
|
|
|
with tabs[0]: |
|
|
|
st.write("Generate high-quality blog content enriched with real-time insights using SmolAgents.") |
|
|
|
|
|
blog_prompt = st.text_area("Enter your blog topic or prompt:", placeholder="E.g., The Future of Technology in Healthcare") |
|
|
|
|
|
tone = st.selectbox("Choose the tone of the content:", ["Professional", "Casual", "Persuasive", "Informative"]) |
|
|
|
|
|
include_summary = st.checkbox("Include a summary of the generated content") |
|
|
|
|
|
if st.button("Generate Blog Content"): |
|
if blog_prompt: |
|
with st.spinner("Generating blog content, please wait..."): |
|
try: |
|
|
|
agent_prompt = ( |
|
f"Write a well-structured and detailed blog post on the following topic:\n" |
|
f"{blog_prompt}\n" |
|
f"Structure it with: Introduction, Key Trends, Applications, Ethical Considerations, and Conclusion." |
|
f" Use numbered or bulleted points for key takeaways. Tone: {tone}" |
|
) |
|
blog_result = content_agent.run(agent_prompt) |
|
|
|
|
|
|
|
st.write(blog_result) |
|
|
|
|
|
st.session_state["last_content"] = blog_result |
|
|
|
|
|
if include_summary: |
|
summary_prompt = ( |
|
f"Provide a concise and well-ordered summary of the following content with key points as bullet points:\n" |
|
f"{blog_result}" |
|
) |
|
summary_result = content_agent.run(summary_prompt) |
|
st.session_state["last_summary"] = summary_result |
|
except Exception as e: |
|
st.error("An error occurred while generating content. Please try again.") |
|
else: |
|
st.warning("Please enter a valid blog topic or prompt.") |
|
|
|
|
|
if "last_content" in st.session_state: |
|
content = st.session_state["last_content"] |
|
text_file_path, text_filename = generate_text_download(content) |
|
with open(text_file_path, "rb") as file: |
|
st.download_button(label="Download Content as Text File", data=file, file_name=text_filename, mime="text/plain") |
|
pdf_file_path, pdf_filename = generate_pdf_download(content) |
|
with open(pdf_file_path, "rb") as file: |
|
st.download_button(label="Download Content as PDF", data=file, file_name=pdf_filename, mime="application/pdf") |
|
|
|
|
|
with tabs[1]: |
|
|
|
if "last_summary" in st.session_state: |
|
st.subheader("Content Summary") |
|
st.markdown(st.session_state["last_summary"]) |
|
|
|
|
|
summary = st.session_state["last_summary"] |
|
summary_text_file_path, summary_text_filename = generate_text_download(summary, filename="generated_summary.txt") |
|
with open(summary_text_file_path, "rb") as file: |
|
st.download_button(label="Download Summary as Text File", data=file, file_name=summary_text_filename, mime="text/plain") |
|
summary_pdf_file_path, summary_pdf_filename = generate_pdf_download(summary, filename="generated_summary.pdf") |
|
with open(summary_pdf_file_path, "rb") as file: |
|
st.download_button(label="Download Summary as PDF", data=file, file_name=summary_pdf_filename, mime="application/pdf") |
|
else: |
|
st.write("No summary available. Generate content and include a summary to view it here.") |
|
|
|
|
|
with tabs[2]: |
|
st.header("Agent Activity") |
|
if "last_content" in st.session_state: |
|
display_agent_activity(blog_prompt, st.session_state["last_content"]) |
|
else: |
|
st.write("No recent activity to display.") |