DrishtiSharma commited on
Commit
7becee7
·
verified ·
1 Parent(s): d096b0c

Create interm.py

Browse files
Files changed (1) hide show
  1. interm.py +113 -0
interm.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )
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 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.")