Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,34 @@
|
|
1 |
import streamlit as st
|
2 |
from gpt_researcher import GPTResearcher
|
|
|
|
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
5 |
# Access secrets
|
6 |
openai_api_key = st.secrets["OPENAI_API_KEY"]
|
7 |
tavily_api_key = st.secrets["TAVILY_API_KEY"]
|
8 |
|
|
|
|
|
|
|
9 |
# Set the document path environment variable
|
10 |
os.environ["DOC_PATH"] = "./local" # Path to the folder with documents
|
11 |
|
12 |
# Constants
|
13 |
REPORT_TYPE = "research_report"
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
"""
|
18 |
Fetch a research report based on the provided query and report type.
|
19 |
Research is conducted on a local document.
|
@@ -22,15 +37,11 @@ def fetch_report(query, report_type):
|
|
22 |
researcher = GPTResearcher(
|
23 |
query=query, report_type=report_type, report_source="local"
|
24 |
)
|
25 |
-
researcher.conduct_research()
|
26 |
-
return researcher.write_report()
|
27 |
except Exception as e:
|
28 |
return f"Error during research: {str(e)}"
|
29 |
|
30 |
-
# Cache the report generation function to avoid redundant computations
|
31 |
-
@st.cache(suppress_st_warning=True, show_spinner=False)
|
32 |
-
def cached_fetch_report(query, report_type):
|
33 |
-
return fetch_report(query, report_type)
|
34 |
|
35 |
# Streamlit interface
|
36 |
st.title("Google Leak Reporting Tool")
|
@@ -48,7 +59,7 @@ if st.button("Generate Report"):
|
|
48 |
st.warning("Please enter a query to generate a report.")
|
49 |
else:
|
50 |
with st.spinner("Generating report..."):
|
51 |
-
report =
|
52 |
# Display the report or error message
|
53 |
if report and not report.startswith("Error"):
|
54 |
st.success("Report generated successfully!")
|
|
|
1 |
import streamlit as st
|
2 |
from gpt_researcher import GPTResearcher
|
3 |
+
import asyncio
|
4 |
+
import nest_asyncio
|
5 |
import os
|
6 |
+
from contextlib import contextmanager
|
7 |
+
from io import StringIO
|
8 |
+
import sys
|
9 |
|
10 |
# Access secrets
|
11 |
openai_api_key = st.secrets["OPENAI_API_KEY"]
|
12 |
tavily_api_key = st.secrets["TAVILY_API_KEY"]
|
13 |
|
14 |
+
# Apply the asyncio patch from nest_asyncio if required
|
15 |
+
nest_asyncio.apply()
|
16 |
+
|
17 |
# Set the document path environment variable
|
18 |
os.environ["DOC_PATH"] = "./local" # Path to the folder with documents
|
19 |
|
20 |
# Constants
|
21 |
REPORT_TYPE = "research_report"
|
22 |
|
23 |
+
|
24 |
+
# Function to handle asynchronous calls
|
25 |
+
def run_async(coroutine):
|
26 |
+
loop = asyncio.get_event_loop()
|
27 |
+
return loop.run_until_complete(coroutine)
|
28 |
+
|
29 |
+
|
30 |
+
# Define the asynchronous function to fetch the report
|
31 |
+
async def fetch_report(query, report_type):
|
32 |
"""
|
33 |
Fetch a research report based on the provided query and report type.
|
34 |
Research is conducted on a local document.
|
|
|
37 |
researcher = GPTResearcher(
|
38 |
query=query, report_type=report_type, report_source="local"
|
39 |
)
|
40 |
+
await researcher.conduct_research()
|
41 |
+
return await researcher.write_report()
|
42 |
except Exception as e:
|
43 |
return f"Error during research: {str(e)}"
|
44 |
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Streamlit interface
|
47 |
st.title("Google Leak Reporting Tool")
|
|
|
59 |
st.warning("Please enter a query to generate a report.")
|
60 |
else:
|
61 |
with st.spinner("Generating report..."):
|
62 |
+
report = run_async(fetch_report(query, REPORT_TYPE))
|
63 |
# Display the report or error message
|
64 |
if report and not report.startswith("Error"):
|
65 |
st.success("Report generated successfully!")
|