DrishtiSharma commited on
Commit
3b69057
·
verified ·
1 Parent(s): c7a49f0

Update interm.py

Browse files
Files changed (1) hide show
  1. interm.py +75 -82
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 the content generation agent with DuckDuckGo tool
6
- content_agent = CodeAgent(
7
- tools=[DuckDuckGoSearchTool()],
8
- model=HfApiModel()
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
- # 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()
@@ -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
- # 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"])
41
 
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"])
52
 
53
- # Option to include a summary
54
- include_summary = st.checkbox("Include a summary of the generated content")
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 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")
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("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_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.")
 
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.")