|
import streamlit as st |
|
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel |
|
from io import BytesIO |
|
|
|
|
|
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 enhance_content(content): |
|
return f"{content}\n\n---\nGenerated with insights and enhanced formatting." |
|
|
|
|
|
def generate_text_download(content, filename="generated_blog.txt"): |
|
b = BytesIO() |
|
b.write(content.encode()) |
|
b.seek(0) |
|
return b, filename |
|
|
|
|
|
def generate_pdf_download(content, filename="generated_blog.pdf"): |
|
from fpdf import FPDF |
|
pdf = FPDF() |
|
pdf.add_page() |
|
pdf.set_font("Arial", size=12) |
|
pdf.multi_cell(0, 10, content) |
|
b = BytesIO() |
|
pdf.output(b) |
|
b.seek(0) |
|
return b, filename |
|
|
|
|
|
st.title("SmolAgent Content Writer") |
|
|
|
|
|
tabs = st.tabs(["Generate Content", "Agent Activity", "Download Options"]) |
|
|
|
|
|
with tabs[0]: |
|
st.header("Generate Content") |
|
st.write("Generate high-quality blog content enriched with real-time insights using SmolAgent.") |
|
|
|
|
|
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: |
|
|
|
blog_result = content_agent.run(f"{blog_prompt}\nTone: {tone}") |
|
|
|
|
|
blog_result = enhance_content(blog_result) |
|
|
|
|
|
st.subheader("Generated Blog Content") |
|
st.write(blog_result) |
|
|
|
|
|
if include_summary: |
|
summary_prompt = f"Summarize this content:\n{blog_result}" |
|
summary_result = content_agent.run(summary_prompt) |
|
st.subheader("Content Summary") |
|
st.write(summary_result) |
|
|
|
|
|
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.") |
|
|
|
|
|
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.") |
|
|
|
|
|
with tabs[2]: |
|
st.header("Download Options") |
|
if "last_content" in st.session_state: |
|
content = st.session_state["last_content"] |
|
|
|
|
|
text_file, text_filename = generate_text_download(content) |
|
st.download_button(label="Download as Text File", data=text_file, file_name=text_filename, mime="text/plain") |
|
|
|
|
|
pdf_file, pdf_filename = generate_pdf_download(content) |
|
st.download_button(label="Download as PDF", data=pdf_file, file_name=pdf_filename, mime="application/pdf") |
|
else: |
|
st.write("No content available for download. Generate content first.") |