File size: 4,442 Bytes
56c94e1 5e3e11c ef98296 56c94e1 5e3e11c ef98296 5fc93c2 5e3e11c ef98296 5fc93c2 5e3e11c 5fc93c2 5e3e11c 5fc93c2 5e3e11c 5fc93c2 5e3e11c 5fc93c2 5e3e11c 5fc93c2 da46551 5e3e11c da46551 5e3e11c 5fc93c2 5e3e11c 5fc93c2 5e3e11c 5fc93c2 5e3e11c 503457f 5e3e11c 503457f 5e3e11c 503457f 5e3e11c 503457f 5e3e11c 503457f 5e3e11c 503457f 5e3e11c 56c94e1 5e3e11c |
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 |
import streamlit as st
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
from fpdf import FPDF
import tempfile
# Initialize content generation agent
content_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
def save_as_text(content, filename):
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp_file:
tmp_file.write(content.encode())
return tmp_file.name, filename
def save_as_pdf(content, filename):
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 Generator")
tabs = st.tabs(["Generated Content", "Generate Summary", "Agent Activity"])
# Tab 1: Generated Content
with tabs[0]:
st.header("Generate and View Content")
prompt = st.text_area("Enter your topic or prompt:")
tone = st.selectbox("Choose the tone:", ["Professional", "Casual", "Persuasive", "Informative"])
if st.button("Generate Content"):
if prompt:
with st.spinner("Generating content..."):
try:
agent_prompt = (
f"Write a detailed blog post on the topic: {prompt}.\n"
f"Structure: Introduction, Key Points, Applications, Ethical Considerations, Conclusion.\n"
f"Tone: {tone}"
)
result = content_agent.run(agent_prompt)
st.session_state["generated_content"] = result
st.subheader("Generated Content")
st.write(result)
except Exception as e:
st.error("Error generating content.")
else:
st.warning("Please enter a valid prompt.")
if "generated_content" in st.session_state:
content = st.session_state["generated_content"]
text_file_path, text_filename = save_as_text(content, "content.txt")
pdf_file_path, pdf_filename = save_as_pdf(content, "content.pdf")
with open(text_file_path, "rb") as file:
st.download_button("Download as TXT", data=file, file_name=text_filename, mime="text/plain")
with open(pdf_file_path, "rb") as file:
st.download_button("Download as PDF", data=file, file_name=pdf_filename, mime="application/pdf")
# Tab 2: Generate Summary
with tabs[1]:
st.header("Generate and View Summary")
if "generated_content" in st.session_state:
content = st.session_state["generated_content"]
if st.button("Generate Summary"):
with st.spinner("Generating summary..."):
try:
summary_prompt = f"Summarize the following content:\n{content}"
summary = content_agent.run(summary_prompt)
st.session_state["content_summary"] = summary
st.subheader("Summary")
st.markdown(summary)
except Exception as e:
st.error("Error generating summary.")
if "content_summary" in st.session_state:
summary = st.session_state["content_summary"]
text_file_path, text_filename = save_as_text(summary, "summary.txt")
pdf_file_path, pdf_filename = save_as_pdf(summary, "summary.pdf")
with open(text_file_path, "rb") as file:
st.download_button("Download Summary as TXT", data=file, file_name=text_filename, mime="text/plain")
with open(pdf_file_path, "rb") as file:
st.download_button("Download Summary as PDF", data=file, file_name=pdf_filename, mime="application/pdf")
else:
st.write("No content generated yet. Go to the first tab to create content.")
# Tab 3: Agent Activity
with tabs[2]:
st.header("Agent Activity")
if "generated_content" in st.session_state:
st.write("**Prompt:**")
st.code(prompt, language="text")
st.write("**Generated Content:**")
st.code(st.session_state["generated_content"], language="text")
if "content_summary" in st.session_state:
st.write("**Generated Summary:**")
st.code(st.session_state["content_summary"], language="text")
else:
st.write("No activity to display.") |