-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
56 lines (40 loc) · 1.61 KB
/
Copy pathscript.py
File metadata and controls
56 lines (40 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import argparse
import os
import sys
from datetime import datetime
from app.content_handler import parse_content, save_in_disk
from app.services.open_ai import create_completion, upload_file
RAW_DOCUMENTS_PATH = "./documents"
DESTINATION_DIRECTORY = "output"
if not (len(sys.argv) > 1):
raise ValueError("Please provide a question to be answered.")
parser = argparse.ArgumentParser()
parser.add_argument("--question", help="Specify the question to be answered")
args = parser.parse_args()
QUESTION = args.question
if __name__ == "__main__":
all_items = os.listdir(RAW_DOCUMENTS_PATH)
files = [{"path": f"{RAW_DOCUMENTS_PATH}/{item}", "name": os.path.splitext(item)[0]} for item in all_items]
# Parse the content of each file and save it in a txt file
for file in files:
content = parse_content(file["path"])
file["txt_output_path"] = save_in_disk(DESTINATION_DIRECTORY, file["name"], content)
# Upload the files to OpenAI
context = ""
for file in files:
file["upload_id"] = upload_file(file["txt_output_path"])
context = ""
for file in files:
context += f"- {file['name']} (File ID: {file['upload_id']})\n"
prompt = f"""
Analyze the following documents based on the uploaded files, and than answer the question giving examples:
Documents:
{context}
---
Question:
{QUESTION}
"""
completion = create_completion(prompt)
report_name = f'ai_report_{datetime.now()}'.replace(' ', '_')
save_in_disk(DESTINATION_DIRECTORY, report_name, completion, "md")
print(f"Report saved to {report_name}")