QuantumLearner commited on
Commit
15545df
·
verified ·
1 Parent(s): 6d546db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -7,6 +7,7 @@ from contextlib import redirect_stdout
7
  import io
8
  from fpdf import FPDF
9
  from datetime import datetime
 
10
 
11
  # Apply nest_asyncio for asyncio support in Streamlit
12
  nest_asyncio.apply()
@@ -22,7 +23,8 @@ if not openai_api_key or not tavily_api_key:
22
  # Define the asynchronous function to get the report and capture logs
23
  async def get_report(query: str, report_type: str, sources: list, report_source: str):
24
  f = io.StringIO()
25
- logs_container = st.empty()
 
26
  with redirect_stdout(f):
27
  if report_source == 'local':
28
  # Set the DOC_PATH environment variable
@@ -33,15 +35,21 @@ async def get_report(query: str, report_type: str, sources: list, report_source:
33
 
34
  await researcher.conduct_research()
35
 
36
- while True:
 
 
37
  logs = f.getvalue()
38
- logs_container.text_area("Agent Logs", logs, height=200, key="realtime_logs_display_unique")
39
- await asyncio.sleep(1) # Update every second
40
  if "Finalized research step" in logs:
41
  break
 
 
 
42
 
43
  report = await researcher.write_report()
44
- return report, f.getvalue()
 
45
 
46
  # Function to create PDF using fpdf with UTF-8 encoding
47
  class PDF(FPDF):
@@ -54,16 +62,6 @@ class PDF(FPDF):
54
  self.set_font("Arial", "I", 8)
55
  self.cell(0, 10, f"Page {self.page_no()}", 0, 0, "C")
56
 
57
- def chapter_title(self, title):
58
- self.set_font("Arial", "B", 12)
59
- self.cell(0, 10, title, 0, 1, "L")
60
- self.ln(5)
61
-
62
- def chapter_body(self, body):
63
- self.set_font("Arial", "", 12)
64
- self.multi_cell(0, 10, body)
65
- self.ln()
66
-
67
  def create_pdf(report_text, pdf_path):
68
  pdf = PDF()
69
  pdf.add_page()
@@ -187,14 +185,14 @@ if 'report' in st.session_state:
187
 
188
  st.markdown("### Agent Logs")
189
  if 'logs' in st.session_state:
190
- st.text_area("Logs will appear here during the research process.",
191
- st.session_state.logs,
192
  height=200,
193
- key="final_logs_display_unique")
194
  else:
195
- st.text_area("Logs will appear here during the research process.",
196
  height=200,
197
- key="final_logs_display_alternative")
198
 
199
  # Hide Streamlit's default footer and menu
200
  hide_streamlit_style = """
 
7
  import io
8
  from fpdf import FPDF
9
  from datetime import datetime
10
+ import uuid
11
 
12
  # Apply nest_asyncio for asyncio support in Streamlit
13
  nest_asyncio.apply()
 
23
  # Define the asynchronous function to get the report and capture logs
24
  async def get_report(query: str, report_type: str, sources: list, report_source: str):
25
  f = io.StringIO()
26
+ unique_key = str(uuid.uuid4()) # Generate a unique key for this run
27
+
28
  with redirect_stdout(f):
29
  if report_source == 'local':
30
  # Set the DOC_PATH environment variable
 
35
 
36
  await researcher.conduct_research()
37
 
38
+ max_attempts = 30 # Prevent infinite loop
39
+ attempts = 0
40
+ while attempts < max_attempts:
41
  logs = f.getvalue()
42
+
43
+ # Break condition
44
  if "Finalized research step" in logs:
45
  break
46
+
47
+ await asyncio.sleep(1) # Update every second
48
+ attempts += 1
49
 
50
  report = await researcher.write_report()
51
+
52
+ return report, logs
53
 
54
  # Function to create PDF using fpdf with UTF-8 encoding
55
  class PDF(FPDF):
 
62
  self.set_font("Arial", "I", 8)
63
  self.cell(0, 10, f"Page {self.page_no()}", 0, 0, "C")
64
 
 
 
 
 
 
 
 
 
 
 
65
  def create_pdf(report_text, pdf_path):
66
  pdf = PDF()
67
  pdf.add_page()
 
185
 
186
  st.markdown("### Agent Logs")
187
  if 'logs' in st.session_state:
188
+ st.text_area("Logs will appear here during the research process:",
189
+ value=st.session_state.logs,
190
  height=200,
191
+ key=f"logs_{uuid.uuid4()}")
192
  else:
193
+ st.text_area("Logs will appear here during the research process",
194
  height=200,
195
+ key=f"logs_{uuid.uuid4()}")
196
 
197
  # Hide Streamlit's default footer and menu
198
  hide_streamlit_style = """