|
import streamlit as st |
|
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel |
|
from fpdf import FPDF |
|
import tempfile |
|
|
|
|
|
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"]) |
|
|
|
|
|
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") |
|
|
|
|
|
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.") |
|
|
|
|
|
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.") |