DrishtiSharma commited on
Commit
bb14580
Β·
verified Β·
1 Parent(s): 5f89cb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -5
app.py CHANGED
@@ -5,6 +5,8 @@ from swarm import Swarm, Agent
5
  from bs4 import BeautifulSoup
6
  import requests
7
  import os
 
 
8
 
9
  # Function to fetch OpenAI API key
10
  def fetch_openai_api_key():
@@ -97,6 +99,29 @@ def orchestrate_workflow(client, url):
97
  final_summary = writer_result.messages[-1]["content"]
98
  return final_summary
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  # Streamlit App UI
101
  st.markdown(
102
  """
@@ -110,7 +135,6 @@ st.markdown(
110
  unsafe_allow_html=True,
111
  )
112
 
113
-
114
  # 1. Add the title at the top
115
  st.markdown('<div class="title">Swarm-based Web Content Analyzer 🧐</div>', unsafe_allow_html=True)
116
 
@@ -133,13 +157,12 @@ st.markdown(
133
  # 5. Add one line-spacing after the acknowledgment
134
  st.markdown('<div style="margin-bottom: 20px;"></div>', unsafe_allow_html=True)
135
 
136
-
137
  fetch_openai_api_key()
138
 
139
  if 'OPENAI_API_KEY' in os.environ and os.environ['OPENAI_API_KEY']:
140
  client = initialize_swarm_client()
141
 
142
- # 4. Add interface for URL input
143
  st.subheader("Enter the Website URL πŸ”—")
144
  url = st.text_input("Enter the URL of the website you want to scrape", placeholder="https://huggingface.co/models")
145
 
@@ -154,9 +177,25 @@ if 'OPENAI_API_KEY' in os.environ and os.environ['OPENAI_API_KEY']:
154
  st.success("βœ… Workflow complete!")
155
  st.write("### πŸ“œ Final Report:")
156
  st.write(final_report)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  else:
158
  st.error("❌ Please enter a valid URL.")
159
  else:
160
  st.sidebar.warning("⚠️ OpenAI API Key not set. Please check your Hugging Face secrets configuration.")
161
-
162
-
 
5
  from bs4 import BeautifulSoup
6
  import requests
7
  import os
8
+ import io
9
+ from reportlab.pdfgen import canvas
10
 
11
  # Function to fetch OpenAI API key
12
  def fetch_openai_api_key():
 
99
  final_summary = writer_result.messages[-1]["content"]
100
  return final_summary
101
 
102
+ # Helper functions to create text and PDF files
103
+ def create_text_file(content):
104
+ """Create a downloadable text file."""
105
+ return io.StringIO(content)
106
+
107
+ def create_pdf_file(content):
108
+ """Create a downloadable PDF file."""
109
+ buffer = io.BytesIO()
110
+ c = canvas.Canvas(buffer)
111
+ c.drawString(100, 750, "Generated Report")
112
+ c.drawString(100, 730, "--------------------")
113
+ lines = content.split("\n")
114
+ y = 700
115
+ for line in lines:
116
+ if y < 50: # Create a new page if the content overflows
117
+ c.showPage()
118
+ y = 750
119
+ c.drawString(100, y, line)
120
+ y -= 20
121
+ c.save()
122
+ buffer.seek(0)
123
+ return buffer
124
+
125
  # Streamlit App UI
126
  st.markdown(
127
  """
 
135
  unsafe_allow_html=True,
136
  )
137
 
 
138
  # 1. Add the title at the top
139
  st.markdown('<div class="title">Swarm-based Web Content Analyzer 🧐</div>', unsafe_allow_html=True)
140
 
 
157
  # 5. Add one line-spacing after the acknowledgment
158
  st.markdown('<div style="margin-bottom: 20px;"></div>', unsafe_allow_html=True)
159
 
 
160
  fetch_openai_api_key()
161
 
162
  if 'OPENAI_API_KEY' in os.environ and os.environ['OPENAI_API_KEY']:
163
  client = initialize_swarm_client()
164
 
165
+ # Add interface for URL input
166
  st.subheader("Enter the Website URL πŸ”—")
167
  url = st.text_input("Enter the URL of the website you want to scrape", placeholder="https://huggingface.co/models")
168
 
 
177
  st.success("βœ… Workflow complete!")
178
  st.write("### πŸ“œ Final Report:")
179
  st.write(final_report)
180
+
181
+ # Add download buttons for the report
182
+ text_file = create_text_file(final_report)
183
+ pdf_file = create_pdf_file(final_report)
184
+
185
+ st.download_button(
186
+ label="Download Report as Text",
187
+ data=text_file,
188
+ file_name="report.txt",
189
+ mime="text/plain",
190
+ )
191
+
192
+ st.download_button(
193
+ label="Download Report as PDF",
194
+ data=pdf_file,
195
+ file_name="report.pdf",
196
+ mime="application/pdf",
197
+ )
198
  else:
199
  st.error("❌ Please enter a valid URL.")
200
  else:
201
  st.sidebar.warning("⚠️ OpenAI API Key not set. Please check your Hugging Face secrets configuration.")