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
|
| 5 |
+
from llama_index.core import VectorStoreIndex, Document
|
| 6 |
+
from llama_index.embeddings.gemini import GeminiEmbedding
|
| 7 |
+
from llama_index.llms.gemini import Gemini
|
| 8 |
+
from llama_index.core import DocumentSummaryIndex
|
| 9 |
+
import google.generativeai as genai
|
| 10 |
+
import os
|
| 11 |
+
import PyPDF2
|
| 12 |
+
|
| 13 |
+
# Set up Google API key
|
| 14 |
+
|
| 15 |
+
# Configure Google Gemini
|
| 16 |
+
Settings.embed_model = GeminiEmbedding(api_key=os.getenv("GOOGLE_API_KEY"), model_name="models/embedding-001")
|
| 17 |
+
Settings.llm = Gemini(api_key=os.getenv("GOOGLE_API_KEY"), temperature=0.8, model_name="models/gemini-pro")
|
| 18 |
+
llm = Gemini(api_key=os.getenv("GOOGLE_API_KEY"), temperature=0.1, model_name="models/gemini-pro")
|
| 19 |
+
|
| 20 |
+
# Load and index the input data
|
| 21 |
+
def load_data(document_text):
|
| 22 |
+
document = [Document(text=document_text)]
|
| 23 |
+
|
| 24 |
+
#index = VectorStoreIndex.from_documents([document])
|
| 25 |
+
index = DocumentSummaryIndex.from_documents(document)
|
| 26 |
+
return index
|
| 27 |
+
|
| 28 |
+
# Default report format template
|
| 29 |
+
DEFAULT_REPORT_FORMAT = """
|
| 30 |
+
Title Page
|
| 31 |
+
|
| 32 |
+
Includes the report title, author's name, and date.
|
| 33 |
+
|
| 34 |
+
Abstract
|
| 35 |
+
|
| 36 |
+
A concise summary of the report, covering the background, objectives, methodology, key findings, and conclusions.
|
| 37 |
+
|
| 38 |
+
Table of Contents
|
| 39 |
+
|
| 40 |
+
Lists sections and subsections with corresponding page numbers for easy navigation.
|
| 41 |
+
|
| 42 |
+
Introduction
|
| 43 |
+
|
| 44 |
+
Provides background information, defines the scope of the report, and states the objectives.
|
| 45 |
+
|
| 46 |
+
Literature Review
|
| 47 |
+
|
| 48 |
+
Reviews relevant literature and previous research related to the report topic.
|
| 49 |
+
|
| 50 |
+
Methodology/Approach
|
| 51 |
+
|
| 52 |
+
Details the methods used to gather data or conduct experiments, including design and analytical techniques.
|
| 53 |
+
|
| 54 |
+
Results and Discussion
|
| 55 |
+
|
| 56 |
+
Presents findings in a clear format, often using tables, figures, and charts, followed by a discussion interpreting these results.
|
| 57 |
+
|
| 58 |
+
Conclusions
|
| 59 |
+
|
| 60 |
+
Summarizes the main findings and their implications, often linking back to the report's objectives.
|
| 61 |
+
|
| 62 |
+
Recommendations
|
| 63 |
+
|
| 64 |
+
Suggests actions based on the findings, highlighting potential future work or improvements.
|
| 65 |
+
|
| 66 |
+
References
|
| 67 |
+
|
| 68 |
+
Lists all sources cited in the report, adhering to a specific referencing style.
|
| 69 |
+
|
| 70 |
+
Appendices
|
| 71 |
+
|
| 72 |
+
Contains supplementary material that supports the main text, such as raw data, detailed calculations, or additional figures.
|
| 73 |
+
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
# Generate report
|
| 77 |
+
def generate_report(index, report_format, additional_info):
|
| 78 |
+
query_engine = index.as_query_engine()
|
| 79 |
+
|
| 80 |
+
if not report_format.strip():
|
| 81 |
+
report_format = DEFAULT_REPORT_FORMAT
|
| 82 |
+
st.info("Using default report format.")
|
| 83 |
+
|
| 84 |
+
response = query_engine.query(f"""
|
| 85 |
+
You are a professional report writer. Your task is to create a comprehensive report based on the entire document provided.
|
| 86 |
+
|
| 87 |
+
First, thoroughly analyze and summarize the entire document. Then, use the input text to create a well-structured report following the format below:
|
| 88 |
+
|
| 89 |
+
Report Format:
|
| 90 |
+
{report_format}
|
| 91 |
+
|
| 92 |
+
Additional Information:
|
| 93 |
+
{additional_info}
|
| 94 |
+
|
| 95 |
+
Even if the input is shallow, generate a report
|
| 96 |
+
Guidelines:
|
| 97 |
+
1. Ensure you comprehend and summarize the entire document before starting the report.
|
| 98 |
+
2. The report should be comprehensive, covering all major points from the document.
|
| 99 |
+
3. Adapt the provided format as necessary to best fit the content and context of the document.
|
| 100 |
+
4. Incorporate any additional information provided into the relevant sections of the report.
|
| 101 |
+
5. Use clear, professional language throughout the report.
|
| 102 |
+
6. Provide specific examples or data from the document to support your analysis and conclusions.
|
| 103 |
+
7. If the document contains technical information, explain it in a way that's accessible to a general audience.
|
| 104 |
+
|
| 105 |
+
Generate a thorough, well-structured report that captures the essence of the entire document.
|
| 106 |
+
""")
|
| 107 |
+
return response.response
|
| 108 |
+
|
| 109 |
+
# Streamlit app
|
| 110 |
+
def main():
|
| 111 |
+
st.title("AI Report Writer")
|
| 112 |
+
st.write("Upload your document and our AI will generate a comprehensive report based on its contents!")
|
| 113 |
+
|
| 114 |
+
# File uploader
|
| 115 |
+
uploaded_file = st.file_uploader("Choose a file (PDF or TXT)", type=["txt", "pdf"])
|
| 116 |
+
|
| 117 |
+
# Report format input
|
| 118 |
+
report_format = st.text_area("Enter the desired report format (optional)", height=150,
|
| 119 |
+
help="Leave blank to use a default template")
|
| 120 |
+
|
| 121 |
+
# Additional information input
|
| 122 |
+
additional_info = st.text_area("Enter any additional information or context for the report", height=100)
|
| 123 |
+
|
| 124 |
+
if uploaded_file is not None:
|
| 125 |
+
# Read file contents
|
| 126 |
+
if uploaded_file.type == "application/pdf":
|
| 127 |
+
pdf_reader = PyPDF2.PdfReader(uploaded_file)
|
| 128 |
+
document_text = ""
|
| 129 |
+
for page in pdf_reader.pages:
|
| 130 |
+
document_text += page.extract_text()
|
| 131 |
+
else:
|
| 132 |
+
document_text = uploaded_file.getvalue().decode("utf-8")
|
| 133 |
+
|
| 134 |
+
if st.button("Generate Report"):
|
| 135 |
+
st.write("Analyzing document and generating report...")
|
| 136 |
+
|
| 137 |
+
# Load data and generate report
|
| 138 |
+
doc_list = document_text.split(".")
|
| 139 |
+
index = load_data(document_text)
|
| 140 |
+
report = generate_report(index, report_format, additional_info)
|
| 141 |
+
|
| 142 |
+
st.write("## Generated Report")
|
| 143 |
+
st.write(report)
|
| 144 |
+
else:
|
| 145 |
+
st.warning("Please upload a file to generate a report.")
|
| 146 |
+
|
| 147 |
+
if __name__ == "__main__":
|
| 148 |
+
main()
|