-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (54 loc) · 2.37 KB
/
main.py
File metadata and controls
66 lines (54 loc) · 2.37 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
57
58
59
60
61
62
63
64
65
66
import os
import json
import argparse
from flow import online_flow
from utils.vector_search import load_index
def initialize_system(config):
"""Load necessary resources at system startup."""
print(f"Loading system resources from {config['output_dir']}...")
# Load FAISS index
faiss_index_path = os.path.join(config['output_dir'], "essay_index.faiss")
faiss_index = load_index(faiss_index_path)
print(f"Loaded FAISS index from {faiss_index_path}")
# Load chunk metadata (includes text content)
metadata_path = os.path.join(config['output_dir'], "chunk_metadata.json")
with open(metadata_path, "r") as f:
chunk_metadata = json.load(f)
print(f"Loaded {len(chunk_metadata)} chunks from {metadata_path}")
return {
"faiss_index": faiss_index,
"chunk_metadata": chunk_metadata
}
def main(args):
# Initialize system with resources
system_resources = initialize_system({"output_dir": args.output_dir})
# Process user queries in a loop
while True:
# Get user query
query = input("\nAsk a question about Paul Graham's essays (or type 'exit' to quit): ")
if query.lower() in ['exit', 'quit', 'q']:
break
# Create shared data for this query
shared = {
# System resources
"faiss_index": system_resources["faiss_index"],
"chunk_metadata": system_resources["chunk_metadata"],
# Query
"query": query
}
# Run the online processing flow
online_flow.run(shared)
# Display results
print("\n" + "-" * 50)
if not shared.get("is_valid_query", True):
print(f"Query was determined to be off-topic. Reason: {shared.get('rejection_reason', 'Unknown')}")
print(f"Paul Graham's response: \n\n{shared['final_response']}")
print("-" * 50)
print(f"Audio response available with hash: {shared['audio_file_hash']}")
print(f"(Check the audio_cache directory for the MP3 file)")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="AI Paul Graham - Ask questions about Paul Graham's essays")
parser.add_argument("--output-dir", type=str, default="output",
help="Directory where processed files are stored")
args = parser.parse_args()
main(args)