Maheen001 commited on
Commit
52e61df
Β·
verified Β·
1 Parent(s): cff40ea

Create ui/lifeadmin_coach_ui.py

Browse files
Files changed (1) hide show
  1. ui/lifeadmin_coach_ui.py +224 -0
ui/lifeadmin_coach_ui.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ LifeAdmin Coach UI - AI Chatbot Assistant
3
+ Provides conversational interface with RAG context and memory
4
+ """
5
+
6
+ import gradio as gr
7
+ import asyncio
8
+ from datetime import datetime
9
+
10
+
11
+ def create_lifeadmin_coach_ui(agent):
12
+ """Create LifeAdmin Coach chatbot interface"""
13
+
14
+ async def chat_with_coach(message, history):
15
+ """Process chat message with RAG and memory context"""
16
+ if not message or not message.strip():
17
+ return history, ""
18
+
19
+ try:
20
+ # Get relevant context from RAG
21
+ rag_context = ""
22
+ try:
23
+ rag_results = await agent.rag_engine.search(message, k=3)
24
+ if rag_results:
25
+ rag_context = "\n".join([
26
+ f"- {doc.get('text', '')[:200]}..."
27
+ for doc in rag_results
28
+ ])
29
+ except Exception:
30
+ pass
31
+
32
+ # Get relevant memories
33
+ memory_context = ""
34
+ try:
35
+ memory_context = agent.memory.get_relevant_memories(message, k=3)
36
+ except Exception:
37
+ pass
38
+
39
+ # Build enhanced prompt
40
+ system_context = f"""You are LifeAdmin Coach, a helpful AI assistant that helps users manage their life admin tasks.
41
+
42
+ You have access to:
43
+ - User's uploaded documents and files (via RAG)
44
+ - Past conversation history (via memory)
45
+ - Various tools for task automation
46
+
47
+ Your role:
48
+ - Answer questions about uploaded documents
49
+ - Provide advice on life admin tasks (bills, forms, scheduling, etc.)
50
+ - Help organize and plan tasks
51
+ - Be friendly, concise, and actionable
52
+
53
+ """
54
+
55
+ if rag_context:
56
+ system_context += f"\nπŸ“š **Relevant Documents:**\n{rag_context}\n"
57
+
58
+ if memory_context:
59
+ system_context += f"\nπŸ’­ **Past Context:**\n{memory_context}\n"
60
+
61
+ # Build full prompt
62
+ full_prompt = f"""{system_context}
63
+
64
+ **User Question:** {message}
65
+
66
+ Provide a helpful, concise response. If the user asks about their documents, reference the relevant documents context above.
67
+ """
68
+
69
+ # Get LLM response
70
+ from utils.llm_utils import get_llm_response
71
+ response = await get_llm_response(full_prompt, temperature=0.7, max_tokens=1000)
72
+
73
+ # Save to memory
74
+ try:
75
+ agent.memory.add_memory(
76
+ content=f"User: {message}\nCoach: {response}",
77
+ memory_type='conversation',
78
+ metadata={'timestamp': datetime.now().isoformat()},
79
+ importance=5
80
+ )
81
+ except Exception:
82
+ pass
83
+
84
+ # Update history
85
+ history.append({"role": "user", "content": message})
86
+ history.append({"role": "assistant", "content": response})
87
+
88
+ return history, ""
89
+
90
+ except Exception as e:
91
+ error_msg = f"⚠️ Error: {str(e)}"
92
+ history.append({"role": "user", "content": message})
93
+ history.append({"role": "assistant", "content": error_msg})
94
+ return history, ""
95
+
96
+ async def quick_action(action_type, history):
97
+ """Handle quick action buttons"""
98
+ actions = {
99
+ "summarize_docs": "Can you summarize all the documents I've uploaded?",
100
+ "list_tasks": "What tasks or action items can you find in my documents?",
101
+ "deadlines": "Are there any deadlines or important dates I should know about?",
102
+ "help": "What can you help me with?"
103
+ }
104
+
105
+ message = actions.get(action_type, "")
106
+ if message:
107
+ return await chat_with_coach(message, history)
108
+ return history, ""
109
+
110
+ def clear_chat():
111
+ """Clear chat history"""
112
+ return [], ""
113
+
114
+ # Build UI
115
+ with gr.Column():
116
+ gr.Markdown("""
117
+ ## πŸ€– LifeAdmin Coach
118
+
119
+ Your AI assistant for managing life admin tasks. Ask questions about your documents,
120
+ get advice on tasks, or chat about anything related to organizing your life!
121
+
122
+ **I can help you with:**
123
+ - Answering questions about uploaded documents
124
+ - Finding deadlines and important dates
125
+ - Organizing tasks and creating plans
126
+ - Drafting emails and messages
127
+ - General life admin advice
128
+ """)
129
+
130
+ # Chat interface
131
+ chatbot = gr.Chatbot(
132
+ label="πŸ’¬ Chat with LifeAdmin Coach",
133
+ height=500,
134
+ type="messages"
135
+ )
136
+
137
+ # Input area
138
+ with gr.Row():
139
+ msg_input = gr.Textbox(
140
+ label="",
141
+ placeholder="Ask me anything about your documents or life admin tasks...",
142
+ scale=4,
143
+ lines=2
144
+ )
145
+ send_btn = gr.Button("Send", variant="primary", scale=1)
146
+
147
+ # Quick action buttons
148
+ with gr.Row():
149
+ gr.Markdown("**Quick Actions:**")
150
+
151
+ with gr.Row():
152
+ summarize_btn = gr.Button("πŸ“„ Summarize Docs", size="sm")
153
+ tasks_btn = gr.Button("βœ… List Tasks", size="sm")
154
+ deadlines_btn = gr.Button("πŸ“… Find Deadlines", size="sm")
155
+ help_btn = gr.Button("❓ Help", size="sm")
156
+ clear_btn = gr.Button("πŸ—‘οΈ Clear Chat", size="sm", variant="stop")
157
+
158
+ # Examples
159
+ with gr.Accordion("πŸ’‘ Example Questions", open=False):
160
+ gr.Examples(
161
+ examples=[
162
+ "What documents have I uploaded?",
163
+ "Are there any bills I need to pay?",
164
+ "Help me organize my tasks for this week",
165
+ "Draft an email to follow up on my membership application",
166
+ "What are the key points from my PDF?",
167
+ "Find all phone numbers and emails in my documents",
168
+ "When is my next deadline?",
169
+ ],
170
+ inputs=msg_input
171
+ )
172
+
173
+ # Event handlers
174
+ def chat_wrapper(message, history):
175
+ """Sync wrapper for async chat function"""
176
+ return asyncio.run(chat_with_coach(message, history))
177
+
178
+ def quick_action_wrapper(action_type, history):
179
+ """Sync wrapper for quick actions"""
180
+ return asyncio.run(quick_action(action_type, history))
181
+
182
+ # Connect events
183
+ msg_input.submit(
184
+ fn=chat_wrapper,
185
+ inputs=[msg_input, chatbot],
186
+ outputs=[chatbot, msg_input]
187
+ )
188
+
189
+ send_btn.click(
190
+ fn=chat_wrapper,
191
+ inputs=[msg_input, chatbot],
192
+ outputs=[chatbot, msg_input]
193
+ )
194
+
195
+ summarize_btn.click(
196
+ fn=lambda h: quick_action_wrapper("summarize_docs", h),
197
+ inputs=[chatbot],
198
+ outputs=[chatbot, msg_input]
199
+ )
200
+
201
+ tasks_btn.click(
202
+ fn=lambda h: quick_action_wrapper("list_tasks", h),
203
+ inputs=[chatbot],
204
+ outputs=[chatbot, msg_input]
205
+ )
206
+
207
+ deadlines_btn.click(
208
+ fn=lambda h: quick_action_wrapper("deadlines", h),
209
+ inputs=[chatbot],
210
+ outputs=[chatbot, msg_input]
211
+ )
212
+
213
+ help_btn.click(
214
+ fn=lambda h: quick_action_wrapper("help", h),
215
+ inputs=[chatbot],
216
+ outputs=[chatbot, msg_input]
217
+ )
218
+
219
+ clear_btn.click(
220
+ fn=clear_chat,
221
+ outputs=[chatbot, msg_input]
222
+ )
223
+
224
+ return gr.Column()