DrishtiSharma commited on
Commit
5fc93c2
·
verified ·
1 Parent(s): 95a6300

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -53
app.py CHANGED
@@ -1,8 +1,9 @@
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
  )
@@ -15,61 +16,99 @@ def display_agent_activity(prompt, result):
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("Smolagnets-based 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
 
 
1
  import streamlit as st
2
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
3
+ from io import BytesIO
4
 
5
+ # Initialize the content generation agent with DuckDuckGo tool
6
+ content_agent = CodeAgent(
7
  tools=[DuckDuckGoSearchTool()],
8
  model=HfApiModel()
9
  )
 
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
+ b = BytesIO()
26
+ b.write(content.encode())
27
+ b.seek(0)
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
+ pdf = FPDF()
34
+ pdf.add_page()
35
+ pdf.set_font("Arial", size=12)
36
+ pdf.multi_cell(0, 10, content)
37
+ b = BytesIO()
38
+ pdf.output(b)
39
+ b.seek(0)
40
+ return b, filename
41
+
42
  # Streamlit app title
43
+ st.title("SmolAgent Content Writer")
44
+
45
+ # Tabs for the interface
46
+ tabs = st.tabs(["Generate Content", "Agent Activity", "Download Options"])
47
+
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 SmolAgent.")
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 Technology in Healthcare")
55
+
56
+ # Input field for choosing content tone
57
+ tone = st.selectbox("Choose the tone of the content:", ["Professional", "Casual", "Persuasive", "Informative"])
58
+
59
+ # Option to include a summary
60
+ include_summary = st.checkbox("Include a summary of the generated content")
61
+
62
+ # Button to trigger blog content generation
63
+ if st.button("Generate Blog Content"):
64
+ if blog_prompt:
65
+ with st.spinner("Generating blog content, please wait..."):
66
+ try:
67
+ # Generate blog content using the agent
68
+ blog_result = content_agent.run(f"{blog_prompt}\nTone: {tone}")
69
+
70
+ # Optionally enhance the content
71
+ blog_result = enhance_content(blog_result)
72
+
73
+ # Display the generated blog content
74
+ st.subheader("Generated Blog Content")
75
+ st.write(blog_result)
76
+
77
+ # Display a summary if requested
78
+ if include_summary:
79
+ summary_prompt = f"Summarize this content:\n{blog_result}"
80
+ summary_result = content_agent.run(summary_prompt)
81
+ st.subheader("Content Summary")
82
+ st.write(summary_result)
83
+
84
+ # Store result for download
85
+ st.session_state["last_content"] = blog_result
86
+ except Exception as e:
87
+ st.error("An error occurred while generating content. Please try again.")
88
+ else:
89
+ st.warning("Please enter a valid blog topic or prompt.")
90
+
91
+ # Tab 2: Agent Activity
92
+ with tabs[1]:
93
+ st.header("Agent Activity")
94
+ if "last_content" in st.session_state:
95
+ display_agent_activity(blog_prompt, st.session_state["last_content"])
96
+ else:
97
+ st.write("No recent activity to display.")
98
+
99
+ # Tab 3: Download Options
100
+ with tabs[2]:
101
+ st.header("Download Options")
102
+ if "last_content" in st.session_state:
103
+ content = st.session_state["last_content"]
104
+
105
+ # Text file download
106
+ text_file, text_filename = generate_text_download(content)
107
+ st.download_button(label="Download as Text File", data=text_file, file_name=text_filename, mime="text/plain")
108
+
109
+ # PDF file download
110
+ pdf_file, pdf_filename = generate_pdf_download(content)
111
+ st.download_button(label="Download as PDF", data=pdf_file, file_name=pdf_filename, mime="application/pdf")
112
  else:
113
+ st.write("No content available for download. Generate content first.")
114