QuantumLearner commited on
Commit
a2b9a56
·
verified ·
1 Parent(s): f2f2886

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from gpt_researcher import GPTResearcher
4
+ import asyncio
5
+ import nest_asyncio
6
+ from contextlib import redirect_stdout
7
+ import io
8
+ from fpdf import FPDF
9
+
10
+ # Apply nest_asyncio for asyncio support in Streamlit
11
+ nest_asyncio.apply()
12
+
13
+ # Define the asynchronous function to get the report and capture logs
14
+ async def get_report(query: str, report_type: str, sources: list, report_source: str):
15
+ f = io.StringIO()
16
+ logs_container = st.empty()
17
+ with redirect_stdout(f):
18
+ if report_source == 'local':
19
+ # Set the DOC_PATH environment variable
20
+ os.environ['DOC_PATH'] = './uploads'
21
+ researcher = GPTResearcher(query=query, report_type=report_type, report_source=report_source)
22
+ else:
23
+ researcher = GPTResearcher(query=query, report_type=report_type, source_urls=sources)
24
+
25
+ await researcher.conduct_research()
26
+
27
+ while True:
28
+ logs = f.getvalue()
29
+ logs_container.text_area("Agent Logs", logs, height=200)
30
+ await asyncio.sleep(1) # Update every second
31
+ if "Finalized research step" in logs:
32
+ break
33
+
34
+ report = await researcher.write_report()
35
+ return report, f.getvalue()
36
+
37
+ # Function to create PDF using fpdf with UTF-8 encoding
38
+ class PDF(FPDF):
39
+ def header(self):
40
+ self.set_font("Arial", "B", 12)
41
+ self.cell(0, 10, "Research Report", 0, 1, "C")
42
+
43
+ def footer(self):
44
+ self.set_y(-15)
45
+ self.set_font("Arial", "I", 8)
46
+ self.cell(0, 10, f"Page {self.page_no()}", 0, 0, "C")
47
+
48
+ def chapter_title(self, title):
49
+ self.set_font("Arial", "B", 12)
50
+ self.cell(0, 10, title, 0, 1, "L")
51
+ self.ln(5)
52
+
53
+ def chapter_body(self, body):
54
+ self.set_font("Arial", "", 12)
55
+ self.multi_cell(0, 10, body)
56
+ self.ln()
57
+
58
+ def create_pdf(report_text, pdf_path):
59
+ pdf = PDF()
60
+ pdf.add_page()
61
+ pdf.set_auto_page_break(auto=True, margin=15)
62
+ pdf.set_font("Arial", size=12)
63
+
64
+ for line in report_text.split('\n'):
65
+ pdf.multi_cell(0, 10, line.encode('latin-1', 'replace').decode('latin-1'))
66
+
67
+ pdf.output(pdf_path, 'F')
68
+
69
+ # Streamlit interface
70
+ st.set_page_config(layout="wide")
71
+
72
+ st.title("GPT Researcher")
73
+ st.markdown("""
74
+ ### What is GPT Researcher?
75
+ GPT Researcher is an app that uses GPT (Generative Pre-trained Transformer) to conduct research based on user queries. It can pull information from the web or from uploaded documents to create comprehensive research reports.
76
+
77
+ ### How to Use
78
+ 1. **Enter API Keys**: Provide your OpenAI and Tavily API keys in the sidebar.
79
+ 2. **Select Research Type**: Choose between Web Research and Document Research.
80
+ 3. **Enter Research Query**: Type in your research question or topic.
81
+ 4. **Choose Report Type**: Select the format of the report you want (research report, resource list, or article outline).
82
+ 5. **Provide Sources or Upload Files**: For Web Research, you can enter URLs. For Document Research, upload the necessary files.
83
+ 6. **Run Research**: Click the "Run Research" button to start. The logs will update in real-time, and the final report will be displayed and available for download as a PDF.
84
+
85
+ """)
86
+
87
+ with st.sidebar:
88
+ st.markdown("### API Keys")
89
+ openai_api_key = st.text_input("Enter your OpenAI API key:", "sk-proj-vFPqdrr801blzZCRBjztT3BlbkFJJJeQVcc62PA40cQ1S9Zv", type="password")
90
+ tavily_api_key = st.text_input("Enter your Tavily API key:", "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", type="password")
91
+
92
+ st.markdown("### Research Settings")
93
+ research_type = st.selectbox("Select research type:", ["Web Research", "Document Research"])
94
+ query = st.text_input("Enter your research query:", "What is the Latest in Investing using AI?")
95
+ report_type = st.selectbox("Select report type:", ["research_report", "resource_list", "article_outline"])
96
+
97
+ if research_type == "Web Research":
98
+ sources_input = st.text_area("Enter your sources (optional, comma-separated URLs):")
99
+ sources = [url.strip() for url in sources_input.split(',') if url.strip()]
100
+ else:
101
+ uploaded_files = st.file_uploader("Upload files for local research:", accept_multiple_files=True)
102
+ sources = []
103
+ if uploaded_files:
104
+ os.makedirs("uploads", exist_ok=True)
105
+ for uploaded_file in uploaded_files:
106
+ file_path = os.path.join("uploads", uploaded_file.name)
107
+ with open(file_path, "wb") as f:
108
+ f.write(uploaded_file.getbuffer())
109
+
110
+ if st.button("Run Research"):
111
+ if not openai_api_key or not tavily_api_key:
112
+ st.warning("Please enter both API keys.")
113
+ elif not query:
114
+ st.warning("Please enter a research query.")
115
+ else:
116
+ # Set the API keys as environment variables
117
+ os.environ['OPENAI_API_KEY'] = openai_api_key
118
+ os.environ['TAVILY_API_KEY'] = tavily_api_key
119
+
120
+ # Set the retriever environment variable
121
+ os.environ['RETRIEVER'] = 'tavily'
122
+
123
+ report_source = 'local' if research_type == "Document Research" else 'web'
124
+
125
+ with st.spinner("Running research..."):
126
+ # Run the research and get the report and logs
127
+ report, logs = asyncio.run(get_report(query, report_type, sources, report_source))
128
+ st.session_state.report = report
129
+ st.session_state.logs = logs
130
+
131
+ # Display outputs in the main section
132
+ if 'report' in st.session_state:
133
+ st.markdown("### Research Report")
134
+ st.markdown(st.session_state.report)
135
+
136
+ # Create PDF
137
+ pdf_path = "report.pdf"
138
+ create_pdf(st.session_state.report, pdf_path)
139
+
140
+ # Provide download link for the PDF
141
+ with open(pdf_path, "rb") as pdf_file:
142
+ st.download_button(
143
+ label="Download report as PDF",
144
+ data=pdf_file,
145
+ file_name="report.pdf",
146
+ mime="application/pdf"
147
+ )
148
+
149
+ st.markdown("### Agent Logs")
150
+ if 'logs' in st.session_state:
151
+ st.text_area("Logs will appear here during the research process.", st.session_state.logs, height=200)
152
+ else:
153
+ st.text_area("Logs will appear here during the research process.", height=200)