-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
76 lines (59 loc) · 2.61 KB
/
Copy pathcli.py
File metadata and controls
76 lines (59 loc) · 2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
"""
Command-line interface for PDF Analyzer
"""
import argparse
import os
from pdf_analyzer import PDFAnalyzer
def main():
parser = argparse.ArgumentParser(description='PDF Analyzer with Local Ollama')
parser.add_argument('pdf_path', help='Path to the PDF file')
parser.add_argument('--model', default='llama3.2', help='Ollama model name')
parser.add_argument('--chunk-size', type=int, default=500, help='Text chunk size')
parser.add_argument('--overlap', type=int, default=100, help='Chunk overlap')
parser.add_argument('--top-k', type=int, default=5, help='Number of chunks to retrieve')
args = parser.parse_args()
# Check if PDF file exists
if not os.path.exists(args.pdf_path):
print(f"Error: PDF file '{args.pdf_path}' not found.")
return
# Initialize analyzer
print("Initializing PDF Analyzer...")
analyzer = PDFAnalyzer()
# Process PDF
print(f"Processing PDF: {args.pdf_path}")
success = analyzer.process_pdf(args.pdf_path, args.chunk_size, args.overlap)
if not success:
print("Failed to process PDF. Exiting.")
return
print(f"\nPDF processed successfully! Created {len(analyzer.chunks)} chunks.")
print("\nYou can now ask questions about the PDF content.")
print("Type 'quit' or 'exit' to stop.\n")
# Interactive Q&A loop
while True:
try:
question = input("Question: ").strip()
if question.lower() in ['quit', 'exit', 'q']:
print("Goodbye!")
break
if not question:
continue
print("Searching and generating answer...")
result = analyzer.answer_question(question, args.top_k, args.model)
print(f"\nAnswer: {result['answer']}\n")
# Optionally show sources
show_sources = input("Show sources? (y/n): ").strip().lower()
if show_sources in ['y', 'yes']:
print("\nSources:")
for i, source in enumerate(result['sources']):
print(f"\n--- Source {i+1} (Page {source['page']}) ---")
print(f"Similarity: {source['similarity_score']:.3f}")
print(source['text'][:300] + "..." if len(source['text']) > 300 else source['text'])
print("\n" + "="*50 + "\n")
except KeyboardInterrupt:
print("\nGoodbye!")
break
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
main()