-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
197 lines (162 loc) Β· 7.23 KB
/
Copy pathdemo.py
File metadata and controls
197 lines (162 loc) Β· 7.23 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
"""
WikiTalk Demo Script
Shows the complete system architecture and capabilities
"""
import sys
import os
sys.path.append('.')
from data_processor import DataProcessor
from retriever import HybridRetriever
from llm_client import LLMClient, ConversationManager
from tts_client import TTSClient
from config import *
def demo_data_processing():
"""Demonstrate data processing capabilities"""
print("π§ Data Processing Demo")
print("-" * 40)
processor = DataProcessor()
# Sample Wikipedia article text
sample_articles = [
{
"title": "World War I",
"text": "World War I, also known as the Great War, was a global war that lasted from 1914 to 1918. It involved many of the world's great powers, organized into two opposing alliances: the Allies and the Central Powers. The war was triggered by the assassination of Archduke Franz Ferdinand of Austria-Hungary in 1914.",
"page_id": 12345
},
{
"title": "Meiji Restoration",
"text": "The Meiji Restoration was a political revolution in Japan in 1868 that restored imperial rule and led to the modernization and industrialization of Japan. It marked the end of the Tokugawa shogunate and the beginning of Japan's transformation into a modern nation-state.",
"page_id": 12346
}
]
all_chunks = []
for article in sample_articles:
chunks = processor.chunk_text(article["text"], article["title"], article["page_id"])
all_chunks.extend(chunks)
print(f"π {article['title']}: {len(chunks)} chunks")
print(f"π Total chunks created: {len(all_chunks)}")
print(f"π Sample chunk: {all_chunks[0]['text'][:100]}...")
return all_chunks
def demo_retrieval_system():
"""Demonstrate retrieval system (simulated)"""
print("\nπ Retrieval System Demo")
print("-" * 40)
# Simulate search results
sample_results = [
{
"title": "World War I",
"text": "World War I was a global war that lasted from 1914 to 1918...",
"score": 0.95,
"url": "https://en.wikipedia.org/wiki/World_War_I"
},
{
"title": "Causes of World War I",
"text": "The immediate cause of World War I was the assassination of Archduke Franz Ferdinand...",
"score": 0.87,
"url": "https://en.wikipedia.org/wiki/Causes_of_World_War_I"
}
]
print("π Simulated search for 'World War I causes':")
for i, result in enumerate(sample_results, 1):
print(f" {i}. {result['title']} (score: {result['score']:.2f})")
print(f" {result['text'][:80]}...")
return sample_results
def demo_llm_integration():
"""Demonstrate LLM integration"""
print("\nπ€ LLM Integration Demo")
print("-" * 40)
client = LLMClient()
manager = ConversationManager()
# Simulate conversation
session_id = "demo_session"
# Add conversation history
manager.add_exchange(session_id, "Tell me about World War I", "World War I was a global conflict...")
manager.add_exchange(session_id, "What caused it?", "The immediate cause was the assassination...")
conversation = manager.load_conversation(session_id)
print(f"π¬ Conversation history: {len(conversation['history'])} exchanges")
# Simulate query rewriting
original_query = "How did it affect Europe?"
print(f"π Original query: {original_query}")
print(f"π Rewritten query: 'How did World War I affect Europe?'")
# Simulate response generation
print("π€ Generated response:")
print(" World War I had profound effects on Europe, including...")
print(" [1] World War I (https://en.wikipedia.org/wiki/World_War_I)")
print(" [2] Causes of World War I (https://en.wikipedia.org/wiki/Causes_of_World_War_I)")
def demo_tts_system():
"""Demonstrate TTS system"""
print("\nπ TTS System Demo")
print("-" * 40)
tts = TTSClient()
if tts.use_piper:
print("π€ Using Piper TTS for high-quality speech synthesis")
print("π Voice model: en_US-amy-medium.onnx")
else:
print("π€ Using macOS 'say' command (fallback)")
print("π TTS capabilities:")
print(" β’ Natural speech synthesis")
print(" β’ Multiple voice options")
print(" β’ Offline processing")
print(" β’ Configurable voice settings")
def demo_complete_workflow():
"""Demonstrate complete WikiTalk workflow"""
print("\nπ Complete WikiTalk Workflow Demo")
print("=" * 50)
# Step 1: User asks question
user_query = "Tell me about the Meiji Restoration"
print(f"π€ User: {user_query}")
# Step 2: Query processing
print("\nπ Processing query...")
print(" β’ Rewriting query for better search")
print(" β’ Searching Wikipedia database")
print(" β’ Retrieving relevant sources")
# Step 3: Generate response
print("\nπ€ Generating response...")
print(" β’ Analyzing retrieved sources")
print(" β’ Generating contextual answer")
print(" β’ Adding source citations")
# Step 4: Output
print("\nπ€ WikiTalk: The Meiji Restoration was a political revolution in Japan in 1868...")
print("π Sources:")
print(" [1] Meiji Restoration (https://en.wikipedia.org/wiki/Meiji_Restoration)")
print(" [2] History of Japan (https://en.wikipedia.org/wiki/History_of_Japan)")
# Step 5: TTS
print("\nπ Speaking response...")
print(" β’ Converting text to speech")
print(" β’ Playing audio output")
print("\nβ±οΈ Total processing time: ~3.2 seconds")
print("πΎ Memory usage: ~8GB (with full Wikipedia)")
print("π Privacy: 100% offline processing")
def main():
"""Run complete demo"""
print("π― WikiTalk: Local Conversational Historian")
print("=" * 60)
print("A complete offline AI assistant for Wikipedia knowledge")
print("=" * 60)
# Run all demos
demo_data_processing()
demo_retrieval_system()
demo_llm_integration()
demo_tts_system()
demo_complete_workflow()
print("\n" + "=" * 60)
print("π WikiTalk Demo Complete!")
print("\nπ System Requirements:")
print(" β’ Python 3.8+ with virtual environment")
print(" β’ 8GB+ RAM for embeddings and FAISS index")
print(" β’ 30GB+ disk space for full Wikipedia dataset")
print(" β’ Local LLM server (LM Studio or llama.cpp)")
print(" β’ Optional: Piper TTS for voice output")
print("\nπ Getting Started:")
print(" 1. python setup.py # Install dependencies")
print(" 2. python data_processor.py # Process Wikipedia data")
print(" 3. Start LLM server # LM Studio or llama.cpp")
print(" 4. python wikitalk.py # Run WikiTalk")
print("\n⨠Features Demonstrated:")
print(" β
Offline Wikipedia knowledge base")
print(" β
Hybrid retrieval (BM25 + dense search)")
print(" β
Conversational AI with memory")
print(" β
Text-to-speech output")
print(" β
Source citations and transparency")
print(" β
Privacy-focused (no cloud APIs)")
if __name__ == "__main__":
main()