Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
3 |
+
|
4 |
+
# Initialize the blog-writing agent with DuckDuckGo tool
|
5 |
+
blog_agent = CodeAgent(
|
6 |
+
tools=[DuckDuckGoSearchTool()],
|
7 |
+
model=HfApiModel()
|
8 |
+
)
|
9 |
+
|
10 |
+
# Function to display agent activity
|
11 |
+
def display_agent_activity(prompt, result):
|
12 |
+
st.write("### Agent Activity:")
|
13 |
+
st.write("**Prompt Sent to Agent:**")
|
14 |
+
st.code(prompt, language="text")
|
15 |
+
st.write("**Agent Output:**")
|
16 |
+
st.code(result, language="text")
|
17 |
+
|
18 |
+
# Function to save generated content to a file
|
19 |
+
def save_to_file(content, filename="generated_blog.txt"):
|
20 |
+
with open(filename, "w") as file:
|
21 |
+
file.write(content)
|
22 |
+
return filename
|
23 |
+
|
24 |
+
# Function to enhance generated content
|
25 |
+
def enhance_content(content):
|
26 |
+
return f"{content}\n\n---\nGenerated with insights and enhanced formatting."
|
27 |
+
|
28 |
+
# Streamlit app title
|
29 |
+
st.title("AI-Powered Blog Writer")
|
30 |
+
|
31 |
+
# App description
|
32 |
+
st.write("Generate high-quality blog content enriched with real-time insights.")
|
33 |
+
|
34 |
+
# Input field for blog topic or prompt
|
35 |
+
blog_prompt = st.text_area("Enter your blog topic or prompt:", placeholder="E.g., The Future of AI in Healthcare")
|
36 |
+
|
37 |
+
# Input field for choosing content tone
|
38 |
+
tone = st.selectbox("Choose the tone of the content:", ["Professional", "Casual", "Persuasive", "Informative"])
|
39 |
+
|
40 |
+
# Option to include a summary
|
41 |
+
include_summary = st.checkbox("Include a summary of the generated content")
|
42 |
+
|
43 |
+
# Button to trigger blog content generation
|
44 |
+
if st.button("Generate Blog Content"):
|
45 |
+
if blog_prompt:
|
46 |
+
with st.spinner("Generating blog content, please wait..."):
|
47 |
+
try:
|
48 |
+
# Generate blog content using the agent
|
49 |
+
blog_result = blog_agent.run(f"{blog_prompt}\nTone: {tone}")
|
50 |
+
|
51 |
+
# Optionally enhance the content
|
52 |
+
blog_result = enhance_content(blog_result)
|
53 |
+
|
54 |
+
# Display the generated blog content
|
55 |
+
st.subheader("Generated Blog Content")
|
56 |
+
st.write(blog_result)
|
57 |
+
|
58 |
+
# Display a summary if requested
|
59 |
+
if include_summary:
|
60 |
+
summary_prompt = f"Summarize this content:\n{blog_result}"
|
61 |
+
summary_result = blog_agent.run(summary_prompt)
|
62 |
+
st.subheader("Content Summary")
|
63 |
+
st.write(summary_result)
|
64 |
+
|
65 |
+
# Log and display agent activity
|
66 |
+
display_agent_activity(blog_prompt, blog_result)
|
67 |
+
|
68 |
+
# Save the content to a file
|
69 |
+
filename = save_to_file(blog_result)
|
70 |
+
st.success(f"Content saved to {filename}")
|
71 |
+
except Exception as e:
|
72 |
+
st.error("An error occurred while generating content. Please try again.")
|
73 |
+
else:
|
74 |
+
st.warning("Please enter a valid blog topic or prompt.")
|
75 |
+
|