File size: 4,817 Bytes
56c94e1 ef98296 56c94e1 5fc93c2 56c94e1 5fc93c2 ef98296 5fc93c2 ef98296 5fc93c2 56c94e1 da46551 5fc93c2 da46551 5fc93c2 da46551 5fc93c2 da46551 2b2e797 5fc93c2 da46551 5fc93c2 da46551 5fc93c2 ef98296 5fc93c2 ef98296 56c94e1 da46551 |
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 |
import streamlit as st
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
import tempfile
# Initialize the content generation agent with DuckDuckGo tool
content_agent = CodeAgent(
tools=[DuckDuckGoSearchTool()],
model=HfApiModel()
)
# Function to display agent activity
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")
# Function to save content as a downloadable text file
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
# Function to save content as a downloadable PDF
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
# Streamlit app title
st.title("SmolAgents Content Writer")
# Tabs for the interface
tabs = st.tabs(["Generate Content", "Agent Activity", "Download Options"])
# Tab 1: Generate Content
with tabs[0]:
st.header("Generate Content")
st.write("Generate high-quality blog content enriched with real-time insights using SmolAgents.")
# Input field for blog topic or prompt
blog_prompt = st.text_area("Enter your blog topic or prompt:", placeholder="E.g., The Future of AI in Healthcare")
# Input field for choosing content tone
tone = st.selectbox("Choose the tone of the content:", ["Professional", "Casual", "Persuasive", "Informative"])
# Option to include a summary
include_summary = st.checkbox("Include a summary of the generated content")
# Button to trigger blog content generation
if st.button("Generate Blog Content"):
if blog_prompt:
with st.spinner("Generating blog content, please wait..."):
try:
# Generate blog content using the agent
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)
# Display the generated blog content
st.subheader("Generated Blog Content")
st.write(blog_result)
# Display a summary if requested
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.subheader("Content Summary")
st.markdown(summary_result)
# Store result for download
st.session_state["last_content"] = blog_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.")
# Tab 2: Agent Activity
with tabs[1]:
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.")
# Tab 3: Download Options
with tabs[2]:
st.header("Download Options")
if "last_content" in st.session_state:
content = st.session_state["last_content"]
# Text file download
text_file_path, text_filename = generate_text_download(content)
with open(text_file_path, "rb") as file:
st.download_button(label="Download as Text File", data=file, file_name=text_filename, mime="text/plain")
# PDF file download
pdf_file_path, pdf_filename = generate_pdf_download(content)
with open(pdf_file_path, "rb") as file:
st.download_button(label="Download as PDF", data=file, file_name=pdf_filename, mime="application/pdf")
else:
st.write("No content available for download. Generate content first.")
|