DrishtiSharma
commited on
Update interm.py
Browse files
interm.py
CHANGED
@@ -1,30 +1,17 @@
|
|
1 |
import streamlit as st
|
2 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
|
|
3 |
import tempfile
|
4 |
|
5 |
-
# Initialize
|
6 |
-
content_agent = CodeAgent(
|
7 |
-
|
8 |
-
|
9 |
-
)
|
10 |
-
|
11 |
-
# Function to display agent activity
|
12 |
-
def display_agent_activity(prompt, result):
|
13 |
-
st.write("### Agent Activity:")
|
14 |
-
st.write("**Prompt Sent to Agent:**")
|
15 |
-
st.code(prompt, language="text")
|
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 |
-
|
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()
|
@@ -33,83 +20,89 @@ def generate_pdf_download(content, filename="generated_blog.pdf"):
|
|
33 |
pdf.output(tmp_file.name)
|
34 |
return tmp_file.name, filename
|
35 |
|
36 |
-
|
37 |
-
st.
|
38 |
-
|
39 |
-
# Tabs for the interface
|
40 |
-
tabs = st.tabs(["Generate Content", "Agent Activity", "Download Options"])
|
41 |
|
42 |
-
# Tab 1:
|
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 |
-
|
51 |
-
tone = st.selectbox("Choose the tone
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
# Button to trigger blog content generation
|
57 |
-
if st.button("Generate Blog Content"):
|
58 |
-
if blog_prompt:
|
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
|
64 |
-
f"
|
65 |
-
f"
|
66 |
-
f" Use numbered or bulleted points for key takeaways. Tone: {tone}"
|
67 |
)
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
st.
|
72 |
-
st.write(blog_result)
|
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
|
86 |
except Exception as e:
|
87 |
-
st.error("
|
88 |
else:
|
89 |
-
st.warning("Please enter a valid
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
with tabs[1]:
|
93 |
-
st.header("
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
else:
|
97 |
-
st.write("No
|
98 |
|
99 |
-
# Tab 3:
|
100 |
with tabs[2]:
|
101 |
-
st.header("
|
102 |
-
if "last_content" in st.session_state:
|
103 |
-
content = st.session_state["last_content"]
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
st.download_button(label="Download as PDF", data=file, file_name=pdf_filename, mime="application/pdf")
|
114 |
else:
|
115 |
-
st.write("No
|
|
|
1 |
import streamlit as st
|
2 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
3 |
+
from fpdf import FPDF
|
4 |
import tempfile
|
5 |
|
6 |
+
# Initialize content generation agent
|
7 |
+
content_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
|
8 |
+
|
9 |
+
def save_as_text(content, filename):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp_file:
|
11 |
tmp_file.write(content.encode())
|
12 |
return tmp_file.name, filename
|
13 |
|
14 |
+
def save_as_pdf(content, filename):
|
|
|
|
|
15 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
16 |
pdf = FPDF()
|
17 |
pdf.add_page()
|
|
|
20 |
pdf.output(tmp_file.name)
|
21 |
return tmp_file.name, filename
|
22 |
|
23 |
+
st.title("SmolAgents Content Generator")
|
24 |
+
tabs = st.tabs(["Generated Content", "Generate Summary", "Agent Activity"])
|
|
|
|
|
|
|
25 |
|
26 |
+
# Tab 1: Generated Content
|
27 |
with tabs[0]:
|
28 |
+
st.header("Generate and View Content")
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
prompt = st.text_area("Enter your topic or prompt:")
|
31 |
+
tone = st.selectbox("Choose the tone:", ["Professional", "Casual", "Persuasive", "Informative"])
|
32 |
|
33 |
+
if st.button("Generate Content"):
|
34 |
+
if prompt:
|
35 |
+
with st.spinner("Generating content..."):
|
|
|
|
|
|
|
|
|
36 |
try:
|
|
|
37 |
agent_prompt = (
|
38 |
+
f"Write a detailed blog post on the topic: {prompt}.\n"
|
39 |
+
f"Structure: Introduction, Key Points, Applications, Ethical Considerations, Conclusion.\n"
|
40 |
+
f"Tone: {tone}"
|
|
|
41 |
)
|
42 |
+
result = content_agent.run(agent_prompt)
|
43 |
+
st.session_state["generated_content"] = result
|
44 |
+
st.subheader("Generated Content")
|
45 |
+
st.write(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
except Exception as e:
|
47 |
+
st.error("Error generating content.")
|
48 |
else:
|
49 |
+
st.warning("Please enter a valid prompt.")
|
50 |
+
|
51 |
+
if "generated_content" in st.session_state:
|
52 |
+
content = st.session_state["generated_content"]
|
53 |
+
text_file_path, text_filename = save_as_text(content, "content.txt")
|
54 |
+
pdf_file_path, pdf_filename = save_as_pdf(content, "content.pdf")
|
55 |
|
56 |
+
with open(text_file_path, "rb") as file:
|
57 |
+
st.download_button("Download as TXT", data=file, file_name=text_filename, mime="text/plain")
|
58 |
+
|
59 |
+
with open(pdf_file_path, "rb") as file:
|
60 |
+
st.download_button("Download as PDF", data=file, file_name=pdf_filename, mime="application/pdf")
|
61 |
+
|
62 |
+
# Tab 2: Generate Summary
|
63 |
with tabs[1]:
|
64 |
+
st.header("Generate and View Summary")
|
65 |
+
|
66 |
+
if "generated_content" in st.session_state:
|
67 |
+
content = st.session_state["generated_content"]
|
68 |
+
|
69 |
+
if st.button("Generate Summary"):
|
70 |
+
with st.spinner("Generating summary..."):
|
71 |
+
try:
|
72 |
+
summary_prompt = f"Summarize the following content:\n{content}"
|
73 |
+
summary = content_agent.run(summary_prompt)
|
74 |
+
st.session_state["content_summary"] = summary
|
75 |
+
st.subheader("Summary")
|
76 |
+
st.markdown(summary)
|
77 |
+
except Exception as e:
|
78 |
+
st.error("Error generating summary.")
|
79 |
+
|
80 |
+
if "content_summary" in st.session_state:
|
81 |
+
summary = st.session_state["content_summary"]
|
82 |
+
text_file_path, text_filename = save_as_text(summary, "summary.txt")
|
83 |
+
pdf_file_path, pdf_filename = save_as_pdf(summary, "summary.pdf")
|
84 |
+
|
85 |
+
with open(text_file_path, "rb") as file:
|
86 |
+
st.download_button("Download Summary as TXT", data=file, file_name=text_filename, mime="text/plain")
|
87 |
+
|
88 |
+
with open(pdf_file_path, "rb") as file:
|
89 |
+
st.download_button("Download Summary as PDF", data=file, file_name=pdf_filename, mime="application/pdf")
|
90 |
+
|
91 |
else:
|
92 |
+
st.write("No content generated yet. Go to the first tab to create content.")
|
93 |
|
94 |
+
# Tab 3: Agent Activity
|
95 |
with tabs[2]:
|
96 |
+
st.header("Agent Activity")
|
|
|
|
|
97 |
|
98 |
+
if "generated_content" in st.session_state:
|
99 |
+
st.write("**Prompt:**")
|
100 |
+
st.code(prompt, language="text")
|
101 |
+
st.write("**Generated Content:**")
|
102 |
+
st.code(st.session_state["generated_content"], language="text")
|
103 |
|
104 |
+
if "content_summary" in st.session_state:
|
105 |
+
st.write("**Generated Summary:**")
|
106 |
+
st.code(st.session_state["content_summary"], language="text")
|
|
|
107 |
else:
|
108 |
+
st.write("No activity to display.")
|