-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
Β·276 lines (223 loc) Β· 9.54 KB
/
cli.py
File metadata and controls
executable file
Β·276 lines (223 loc) Β· 9.54 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
"""
Command Line Interface for Turkish News RAG Assistant.
Provides easy access to common operations without the web interface.
"""
import argparse
import sys
from datetime import datetime
import json
def main():
parser = argparse.ArgumentParser(
description='Turkish News RAG Assistant CLI',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s test # Run system tests
%(prog)s collect --test # Collect news (test mode)
%(prog)s collect --categories Teknoloji Bilim
%(prog)s ask "BugΓΌn dolar ne kadar?" # Ask a question
%(prog)s summary --hours 24 # Generate daily summary
%(prog)s search "yapay zeka" # Search news
%(prog)s stats # Show database statistics
%(prog)s ui # Launch web interface
"""
)
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Test command
test_parser = subparsers.add_parser('test', help='Run system tests')
# Collect command
collect_parser = subparsers.add_parser('collect', help='Collect news articles')
collect_parser.add_argument('--categories', nargs='+', help='Specific categories to collect')
collect_parser.add_argument('--test', action='store_true', help='Run in test mode')
collect_parser.add_argument('--no-scraping', action='store_true', help='Skip content scraping')
collect_parser.add_argument('--no-newsapi', action='store_true', help='Skip NewsAPI')
collect_parser.add_argument('--max-articles', type=int, help='Max articles per category')
# Ask command
ask_parser = subparsers.add_parser('ask', help='Ask a question')
ask_parser.add_argument('question', help='Question to ask')
ask_parser.add_argument('--category', help='Filter by category')
ask_parser.add_argument('--source', help='Filter by source')
ask_parser.add_argument('--max-sources', type=int, default=5, help='Max sources to use')
# Summary command
summary_parser = subparsers.add_parser('summary', help='Generate news summary')
summary_parser.add_argument('--hours', type=int, default=24, help='Time range in hours')
summary_parser.add_argument('--output', help='Output file (default: stdout)')
# Search command
search_parser = subparsers.add_parser('search', help='Search news articles')
search_parser.add_argument('query', help='Search query')
search_parser.add_argument('--category', help='Filter by category')
search_parser.add_argument('--limit', type=int, default=10, help='Number of results')
search_parser.add_argument('--json', action='store_true', help='Output as JSON')
# Stats command
stats_parser = subparsers.add_parser('stats', help='Show database statistics')
stats_parser.add_argument('--json', action='store_true', help='Output as JSON')
# UI command
ui_parser = subparsers.add_parser('ui', help='Launch web interface')
ui_parser.add_argument('--port', type=int, default=8501, help='Port number')
args = parser.parse_args()
if not args.command:
parser.print_help()
return
# Execute commands
if args.command == 'test':
run_tests()
elif args.command == 'collect':
run_collection(args)
elif args.command == 'ask':
run_question(args)
elif args.command == 'summary':
run_summary(args)
elif args.command == 'search':
run_search(args)
elif args.command == 'stats':
run_stats(args)
elif args.command == 'ui':
run_ui(args)
def run_tests():
"""Run system tests."""
print("π§ͺ Running system tests...")
try:
from test_system import run_all_tests
success = run_all_tests()
sys.exit(0 if success else 1)
except ImportError:
print("β Test system not found. Make sure test_system.py exists.")
sys.exit(1)
def run_collection(args):
"""Run news collection pipeline."""
print("π Starting news collection...")
try:
from main import NewsPipeline
pipeline = NewsPipeline()
# Configure arguments
kwargs = {
'categories': args.categories,
'scrape_content': not args.no_scraping,
'use_newsapi': not args.no_newsapi,
'max_articles_per_category': args.max_articles
}
# Test mode
if args.test:
kwargs['categories'] = ['Bilim', 'Teknoloji']
kwargs['max_articles_per_category'] = 5
print("π§ͺ Running in test mode (limited data)")
# Run pipeline
report = pipeline.run_full_pipeline(**kwargs)
# Print summary
print("\nπ Collection Summary:")
print(f" Articles fetched: {report['articles']['fetched']}")
print(f" Articles processed: {report['articles']['processed']}")
print(f" Chunks created: {report['chunks']['created']}")
print(f" Chunks indexed: {report['chunks']['indexed']}")
print(f" Duration: {report['execution_time']['duration']}")
if report['errors']:
print(f" Errors: {len(report['errors'])}")
except Exception as e:
print(f"β Collection failed: {e}")
sys.exit(1)
def run_question(args):
"""Answer a question using RAG."""
print(f"π€ Answering: {args.question}")
try:
from rag_engine import RAGEngine
rag = RAGEngine()
result = rag.answer_question(
question=args.question,
category=args.category,
source=args.source,
max_chunks=args.max_sources
)
print(f"\n㪠Answer:")
print(result['answer'])
print(f"\nπ Confidence: {result['confidence']:.2%}")
print(f"π Sources used: {result['context_used']}")
if result.get('sources'):
print(f"\nπ° Top Sources:")
for i, source in enumerate(result['sources'][:3]):
print(f" {i+1}. {source.get('source', 'Unknown')} - {source.get('title', 'No title')[:50]}...")
except Exception as e:
print(f"β Question answering failed: {e}")
sys.exit(1)
def run_summary(args):
"""Generate news summary."""
print(f"π Generating summary for last {args.hours} hours...")
try:
from rag_engine import RAGEngine
rag = RAGEngine()
summary = rag.generate_daily_summary(hours=args.hours)
if args.output:
with open(args.output, 'w', encoding='utf-8') as f:
f.write(summary)
print(f"β
Summary saved to {args.output}")
else:
print("\n" + "="*60)
print("π° NEWS SUMMARY")
print("="*60)
print(summary)
except Exception as e:
print(f"β Summary generation failed: {e}")
sys.exit(1)
def run_search(args):
"""Search news articles."""
print(f"π Searching for: {args.query}")
try:
from rag_engine import RAGEngine
rag = RAGEngine()
articles = rag.search_news(
query=args.query,
limit=args.limit,
category=args.category
)
if args.json:
print(json.dumps(articles, indent=2, ensure_ascii=False, default=str))
else:
print(f"\nπ Found {len(articles)} articles:")
for i, article in enumerate(articles):
print(f"\n{i+1}. {article.get('title', 'No title')}")
print(f" Source: {article.get('source', 'Unknown')}")
print(f" Category: {article.get('category', 'Unknown')}")
print(f" Relevance: {article.get('relevance_score', 0):.2%}")
if article.get('url'):
print(f" URL: {article['url']}")
except Exception as e:
print(f"β Search failed: {e}")
sys.exit(1)
def run_stats(args):
"""Show database statistics."""
print("π Database Statistics:")
try:
from db import NewsDatabase
db = NewsDatabase()
stats = db.get_statistics()
if args.json:
print(json.dumps(stats, indent=2, ensure_ascii=False))
else:
print(f"\nTotal chunks: {stats.get('total_chunks', 0)}")
if stats.get('categories'):
print(f"\nCategories:")
for category, count in sorted(stats['categories'].items(), key=lambda x: x[1], reverse=True):
print(f" {category}: {count}")
if stats.get('sources'):
print(f"\nTop 10 Sources:")
sorted_sources = sorted(stats['sources'].items(), key=lambda x: x[1], reverse=True)[:10]
for source, count in sorted_sources:
print(f" {source}: {count}")
except Exception as e:
print(f"β Statistics failed: {e}")
sys.exit(1)
def run_ui(args):
"""Launch web interface."""
print(f"π Launching web interface on port {args.port}...")
try:
import subprocess
import sys
cmd = [sys.executable, '-m', 'streamlit', 'run', 'ui.py', '--server.port', str(args.port)]
subprocess.run(cmd)
except KeyboardInterrupt:
print("\nπ Web interface stopped.")
except Exception as e:
print(f"β Failed to launch web interface: {e}")
sys.exit(1)
if __name__ == "__main__":
main()