Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,8 @@
|
|
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"]
|
@@ -21,14 +18,8 @@ os.environ["DOC_PATH"] = "./local" # Path to the folder with documents
|
|
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,12 +28,22 @@ async def fetch_report(query, report_type):
|
|
37 |
researcher = GPTResearcher(
|
38 |
query=query, report_type=report_type, report_source="local"
|
39 |
)
|
40 |
-
await researcher.conduct_research()
|
41 |
-
|
|
|
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")
|
48 |
|
@@ -55,13 +56,12 @@ query = st.text_area(
|
|
55 |
|
56 |
# Start the report generation process
|
57 |
if st.button("Generate Report"):
|
58 |
-
if not query:
|
59 |
st.warning("Please enter a query to generate a report.")
|
60 |
else:
|
61 |
with st.spinner("Generating report..."):
|
62 |
-
report =
|
63 |
-
|
64 |
-
if report and not report.startswith("Error"):
|
65 |
st.success("Report generated successfully!")
|
66 |
st.write(report) # Display the report in the app
|
67 |
# Create a download button for the report
|
@@ -72,4 +72,4 @@ if st.button("Generate Report"):
|
|
72 |
mime="text/plain",
|
73 |
)
|
74 |
else:
|
75 |
-
st.error(report) # Show the error message if any
|
|
|
1 |
import streamlit as st
|
2 |
from gpt_researcher import GPTResearcher
|
|
|
3 |
import nest_asyncio
|
4 |
+
import asyncio
|
5 |
import os
|
|
|
|
|
|
|
6 |
|
7 |
# Access secrets
|
8 |
openai_api_key = st.secrets["OPENAI_API_KEY"]
|
|
|
18 |
REPORT_TYPE = "research_report"
|
19 |
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# Define the asynchronous function to fetch the report
|
22 |
+
async def fetch_report(query: str, report_type: str) -> str:
|
23 |
"""
|
24 |
Fetch a research report based on the provided query and report type.
|
25 |
Research is conducted on a local document.
|
|
|
28 |
researcher = GPTResearcher(
|
29 |
query=query, report_type=report_type, report_source="local"
|
30 |
)
|
31 |
+
research_result = await researcher.conduct_research()
|
32 |
+
report = await researcher.write_report()
|
33 |
+
return report
|
34 |
except Exception as e:
|
35 |
return f"Error during research: {str(e)}"
|
36 |
|
37 |
|
38 |
+
def run_report_generation(query, report_type):
|
39 |
+
"""
|
40 |
+
Helper function to run async fetch_report function.
|
41 |
+
"""
|
42 |
+
loop = asyncio.get_event_loop()
|
43 |
+
report = loop.run_until_complete(fetch_report(query, report_type))
|
44 |
+
return report
|
45 |
+
|
46 |
+
|
47 |
# Streamlit interface
|
48 |
st.title("Google Leak Reporting Tool")
|
49 |
|
|
|
56 |
|
57 |
# Start the report generation process
|
58 |
if st.button("Generate Report"):
|
59 |
+
if not query.strip():
|
60 |
st.warning("Please enter a query to generate a report.")
|
61 |
else:
|
62 |
with st.spinner("Generating report..."):
|
63 |
+
report = run_report_generation(query, REPORT_TYPE)
|
64 |
+
if "Error during research" not in report:
|
|
|
65 |
st.success("Report generated successfully!")
|
66 |
st.write(report) # Display the report in the app
|
67 |
# Create a download button for the report
|
|
|
72 |
mime="text/plain",
|
73 |
)
|
74 |
else:
|
75 |
+
st.error(report) # Show the error message if any
|