Spaces:
Runtime error
Runtime error
Update interm.py
Browse files
interm.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 3 |
-
|
| 4 |
|
| 5 |
# Initialize the content generation agent with DuckDuckGo tool
|
| 6 |
content_agent = CodeAgent(
|
|
@@ -16,31 +16,25 @@ def display_agent_activity(prompt, result):
|
|
| 16 |
st.write("**Agent Output:**")
|
| 17 |
st.code(result, language="text")
|
| 18 |
|
| 19 |
-
# Function to enhance generated content
|
| 20 |
-
def enhance_content(content):
|
| 21 |
-
return f"{content}\n\n---\nGenerated with insights and enhanced formatting."
|
| 22 |
-
|
| 23 |
# Function to save content as a downloadable text file
|
| 24 |
def generate_text_download(content, filename="generated_blog.txt"):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return b, filename
|
| 29 |
|
| 30 |
# Function to save content as a downloadable PDF
|
| 31 |
def generate_pdf_download(content, filename="generated_blog.pdf"):
|
| 32 |
from fpdf import FPDF
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return b, filename
|
| 41 |
|
| 42 |
# Streamlit app title
|
| 43 |
-
st.title("
|
| 44 |
|
| 45 |
# Tabs for the interface
|
| 46 |
tabs = st.tabs(["Generate Content", "Agent Activity", "Download Options"])
|
|
@@ -48,10 +42,10 @@ tabs = st.tabs(["Generate Content", "Agent Activity", "Download Options"])
|
|
| 48 |
# Tab 1: Generate Content
|
| 49 |
with tabs[0]:
|
| 50 |
st.header("Generate Content")
|
| 51 |
-
st.write("Generate high-quality blog content enriched with real-time insights using
|
| 52 |
|
| 53 |
# Input field for blog topic or prompt
|
| 54 |
-
blog_prompt = st.text_area("Enter your blog topic or prompt:", placeholder="E.g., The Future of
|
| 55 |
|
| 56 |
# Input field for choosing content tone
|
| 57 |
tone = st.selectbox("Choose the tone of the content:", ["Professional", "Casual", "Persuasive", "Informative"])
|
|
@@ -65,10 +59,13 @@ with tabs[0]:
|
|
| 65 |
with st.spinner("Generating blog content, please wait..."):
|
| 66 |
try:
|
| 67 |
# Generate blog content using the agent
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# Display the generated blog content
|
| 74 |
st.subheader("Generated Blog Content")
|
|
@@ -76,10 +73,13 @@ with tabs[0]:
|
|
| 76 |
|
| 77 |
# Display a summary if requested
|
| 78 |
if include_summary:
|
| 79 |
-
summary_prompt =
|
|
|
|
|
|
|
|
|
|
| 80 |
summary_result = content_agent.run(summary_prompt)
|
| 81 |
st.subheader("Content Summary")
|
| 82 |
-
st.
|
| 83 |
|
| 84 |
# Store result for download
|
| 85 |
st.session_state["last_content"] = blog_result
|
|
@@ -103,11 +103,13 @@ with tabs[2]:
|
|
| 103 |
content = st.session_state["last_content"]
|
| 104 |
|
| 105 |
# Text file download
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
# PDF file download
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
else:
|
| 113 |
st.write("No content available for download. Generate content first.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 3 |
+
import tempfile
|
| 4 |
|
| 5 |
# Initialize the content generation agent with DuckDuckGo tool
|
| 6 |
content_agent = CodeAgent(
|
|
|
|
| 16 |
st.write("**Agent Output:**")
|
| 17 |
st.code(result, language="text")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Function to save content as a downloadable text file
|
| 20 |
def generate_text_download(content, filename="generated_blog.txt"):
|
| 21 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp_file:
|
| 22 |
+
tmp_file.write(content.encode())
|
| 23 |
+
return tmp_file.name, filename
|
|
|
|
| 24 |
|
| 25 |
# Function to save content as a downloadable PDF
|
| 26 |
def generate_pdf_download(content, filename="generated_blog.pdf"):
|
| 27 |
from fpdf import FPDF
|
| 28 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
| 29 |
+
pdf = FPDF()
|
| 30 |
+
pdf.add_page()
|
| 31 |
+
pdf.set_font("Arial", size=12)
|
| 32 |
+
pdf.multi_cell(0, 10, content)
|
| 33 |
+
pdf.output(tmp_file.name)
|
| 34 |
+
return tmp_file.name, filename
|
|
|
|
| 35 |
|
| 36 |
# Streamlit app title
|
| 37 |
+
st.title("SmolAgents Content Writer")
|
| 38 |
|
| 39 |
# Tabs for the interface
|
| 40 |
tabs = st.tabs(["Generate Content", "Agent Activity", "Download Options"])
|
|
|
|
| 42 |
# Tab 1: Generate Content
|
| 43 |
with tabs[0]:
|
| 44 |
st.header("Generate Content")
|
| 45 |
+
st.write("Generate high-quality blog content enriched with real-time insights using SmolAgents.")
|
| 46 |
|
| 47 |
# Input field for blog topic or prompt
|
| 48 |
+
blog_prompt = st.text_area("Enter your blog topic or prompt:", placeholder="E.g., The Future of AI in Healthcare")
|
| 49 |
|
| 50 |
# Input field for choosing content tone
|
| 51 |
tone = st.selectbox("Choose the tone of the content:", ["Professional", "Casual", "Persuasive", "Informative"])
|
|
|
|
| 59 |
with st.spinner("Generating blog content, please wait..."):
|
| 60 |
try:
|
| 61 |
# Generate blog content using the agent
|
| 62 |
+
agent_prompt = (
|
| 63 |
+
f"Write a well-structured and detailed blog post on the following topic:\n"
|
| 64 |
+
f"{blog_prompt}\n"
|
| 65 |
+
f"Structure it with: Introduction, Key Trends, Applications, Ethical Considerations, and Conclusion."
|
| 66 |
+
f" Use numbered or bulleted points for key takeaways. Tone: {tone}"
|
| 67 |
+
)
|
| 68 |
+
blog_result = content_agent.run(agent_prompt)
|
| 69 |
|
| 70 |
# Display the generated blog content
|
| 71 |
st.subheader("Generated Blog Content")
|
|
|
|
| 73 |
|
| 74 |
# Display a summary if requested
|
| 75 |
if include_summary:
|
| 76 |
+
summary_prompt = (
|
| 77 |
+
f"Provide a concise and well-ordered summary of the following content with key points as bullet points:\n"
|
| 78 |
+
f"{blog_result}"
|
| 79 |
+
)
|
| 80 |
summary_result = content_agent.run(summary_prompt)
|
| 81 |
st.subheader("Content Summary")
|
| 82 |
+
st.markdown(summary_result)
|
| 83 |
|
| 84 |
# Store result for download
|
| 85 |
st.session_state["last_content"] = blog_result
|
|
|
|
| 103 |
content = st.session_state["last_content"]
|
| 104 |
|
| 105 |
# Text file download
|
| 106 |
+
text_file_path, text_filename = generate_text_download(content)
|
| 107 |
+
with open(text_file_path, "rb") as file:
|
| 108 |
+
st.download_button(label="Download as Text File", data=file, file_name=text_filename, mime="text/plain")
|
| 109 |
|
| 110 |
# PDF file download
|
| 111 |
+
pdf_file_path, pdf_filename = generate_pdf_download(content)
|
| 112 |
+
with open(pdf_file_path, "rb") as file:
|
| 113 |
+
st.download_button(label="Download as PDF", data=file, file_name=pdf_filename, mime="application/pdf")
|
| 114 |
else:
|
| 115 |
st.write("No content available for download. Generate content first.")
|