Spaces:
Running
Running
import streamlit as st | |
from gpt_researcher import GPTResearcher | |
import nest_asyncio | |
import asyncio | |
import os | |
# Access secrets | |
openai_api_key = st.secrets["OPENAI_API_KEY"] | |
tavily_api_key = st.secrets["TAVILY_API_KEY"] | |
# Apply the asyncio patch from nest_asyncio if required | |
nest_asyncio.apply() | |
# Set the document path environment variable | |
os.environ["DOC_PATH"] = "./local" # Path to the folder with documents | |
# Constants | |
REPORT_TYPE = "research_report" | |
# Define the asynchronous function to fetch the report | |
async def fetch_report(query: str, report_type: str) -> str: | |
""" | |
Fetch a research report based on the provided query and report type. | |
Research is conducted on a local document. | |
""" | |
try: | |
researcher = GPTResearcher( | |
query=query, report_type=report_type, report_source="local" | |
) | |
research_result = await researcher.conduct_research() | |
report = await researcher.write_report() | |
return report | |
except Exception as e: | |
return f"Error during research: {str(e)}" | |
def run_report_generation(query, report_type): | |
""" | |
Helper function to run async fetch_report function. | |
""" | |
loop = asyncio.get_event_loop() | |
report = loop.run_until_complete(fetch_report(query, report_type)) | |
return report | |
# Streamlit interface | |
st.title("Google Leak Reporting Tool") | |
# User input for the query using a text area | |
query = st.text_area( | |
"Enter your research query:", | |
"Extract all the information about how the ranking for internal links works.", | |
height=150, # Adjustable height | |
) | |
# Start the report generation process | |
if st.button("Generate Report"): | |
if not query.strip(): | |
st.warning("Please enter a query to generate a report.") | |
else: | |
with st.spinner("Generating report..."): | |
report = run_report_generation(query, REPORT_TYPE) | |
if "Error during research" not in report: | |
st.success("Report generated successfully!") | |
st.write(report) # Display the report in the app | |
# Create a download button for the report | |
st.download_button( | |
label="Download Report as Text File", | |
data=report, | |
file_name="research_report.txt", | |
mime="text/plain", | |
) | |
else: | |
st.error(report) # Show the error message if any |