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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -1,6 +1,6 @@
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(
@@ -22,22 +22,20 @@ def enhance_content(content):
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")
@@ -103,12 +101,13 @@ with tabs[2]:
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
-
 
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(
 
22
 
23
  # Function to save content as a downloadable text file
24
  def generate_text_download(content, filename="generated_blog.txt"):
25
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp_file:
26
+ tmp_file.write(content.encode())
27
+ return tmp_file.name, filename
 
28
 
29
  # Function to save content as a downloadable PDF
30
  def generate_pdf_download(content, filename="generated_blog.pdf"):
31
  from fpdf import FPDF
32
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
33
+ pdf = FPDF()
34
+ pdf.add_page()
35
+ pdf.set_font("Arial", size=12)
36
+ pdf.multi_cell(0, 10, content)
37
+ pdf.output(tmp_file.name)
38
+ return tmp_file.name, filename
 
39
 
40
  # Streamlit app title
41
  st.title("SmolAgent Content Writer")
 
101
  content = st.session_state["last_content"]
102
 
103
  # Text file download
104
+ text_file_path, text_filename = generate_text_download(content)
105
+ with open(text_file_path, "rb") as file:
106
+ st.download_button(label="Download as Text File", data=file, file_name=text_filename, mime="text/plain")
107
 
108
  # PDF file download
109
+ pdf_file_path, pdf_filename = generate_pdf_download(content)
110
+ with open(pdf_file_path, "rb") as file:
111
+ st.download_button(label="Download as PDF", data=file, file_name=pdf_filename, mime="application/pdf")
112
  else:
113
+ st.write("No content available for download. Generate content first.")