File size: 11,626 Bytes
d660683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
HousingMind Web Interface

Gradio-based web interface for the HousingMind RAG system with
built-in feedback collection for continuous improvement.
"""

import os
import sys
import uuid
import time
from pathlib import Path
from typing import Dict, List, Optional, Tuple

import gradio as gr
from dotenv import load_dotenv

# Add scripts directory to path
SCRIPT_DIR = Path(__file__).parent
sys.path.insert(0, str(SCRIPT_DIR))

from rag_engine import HousingMindRAG, DEFAULT_MODEL
from setup_vector_db import HousingMindVectorDB, DEFAULT_EMBEDDING_MODEL
from feedback_db import FeedbackDB, setup_feedback_db

# Load environment variables
load_dotenv()

# Default paths
DEFAULT_VECTOR_DB = SCRIPT_DIR.parent / "vector_db"
DEFAULT_FEEDBACK_DB = SCRIPT_DIR.parent / "data" / "housingmind_feedback.db"

# Global instances
rag_engine: Optional[HousingMindRAG] = None
feedback_db: Optional[FeedbackDB] = None


def initialize_system():
    """Initialize RAG engine and feedback database."""
    global rag_engine, feedback_db

    db_path = os.environ.get("VECTOR_DB_PATH", str(DEFAULT_VECTOR_DB))

    # Initialize RAG engine
    try:
        rag_engine = HousingMindRAG(db_path=db_path)
        print(f"RAG engine initialized")
        print(f"  Model: {rag_engine.model}")
    except Exception as e:
        print(f"Warning: Could not initialize RAG engine: {e}")
        print("Make sure Ollama is running and the vector database exists.")

    # Initialize feedback database
    feedback_db = setup_feedback_db(str(DEFAULT_FEEDBACK_DB))


def answer_query(query: str, session_state: Dict) -> Tuple[str, Dict, int]:
    """
    Process a query and return the response with sources.

    Args:
        query: User's question
        session_state: Session state dictionary

    Returns:
        Tuple of (response text, updated session state, feedback_id)
    """
    if rag_engine is None:
        return (
            "**Error:** RAG system not initialized. Please make sure:\n"
            "1. Ollama is running (`ollama serve`)\n"
            "2. Vector database exists (`python main.py --setup`)",
            session_state,
            None
        )

    if not query.strip():
        return "Please enter a question.", session_state, None

    # Initialize session if needed
    if session_state is None:
        session_state = {
            'session_id': str(uuid.uuid4()),
            'interactions': [],
            'last_feedback_id': None
        }

    # Generate response
    start_time = time.time()
    try:
        result = rag_engine.query(query)
        response_time_ms = int((time.time() - start_time) * 1000)
    except Exception as e:
        return f"**Error generating response:** {str(e)}", session_state, None

    # Format response
    response_text = result.answer

    # Add citations if found
    if result.citations_found:
        response_text += "\n\n**Citations:**\n"
        for citation in result.citations_found:
            response_text += f"- {citation}\n"

    # Add sources
    response_text += "\n\n**Sources:**\n"
    for i, source in enumerate(result.sources, 1):
        source_desc = source['source']
        if source.get('notice_number'):
            source_desc += f" ({source['notice_number']})"
        elif source.get('document_type') and source['document_type'] != 'Unknown':
            source_desc += f" ({source['document_type']})"
        response_text += f"{i}. {source_desc}\n"

    # Save interaction (without rating yet)
    retrieved_docs = [
        {
            'source': s['source'],
            'document_type': s.get('document_type', 'Unknown'),
            'topic': s.get('topic', ''),
            'notice_number': s.get('notice_number', '')
        }
        for s in result.sources
    ]

    feedback_id = feedback_db.save_feedback(
        query=query,
        response=result.answer,
        retrieved_docs=retrieved_docs,
        session_id=session_state['session_id'],
        response_time_ms=response_time_ms,
        model_used=rag_engine.model
    )

    # Update session state
    session_state['interactions'].append({
        'query': query,
        'response': result.answer,
        'feedback_id': feedback_id,
        'retrieved_docs': retrieved_docs
    })
    session_state['last_feedback_id'] = feedback_id

    return response_text, session_state, feedback_id


def submit_rating(rating: int, comment: str, session_state: Dict) -> str:
    """
    Submit a rating for the last response.

    Args:
        rating: 1 (helpful) or -1 (not helpful)
        comment: Optional feedback comment
        session_state: Session state dictionary

    Returns:
        Status message
    """
    if session_state is None or not session_state.get('last_feedback_id'):
        return "No response to rate yet. Ask a question first."

    feedback_id = session_state['last_feedback_id']

    try:
        feedback_db.update_rating(feedback_id, rating, comment if comment else None)
        emoji = "πŸ‘" if rating > 0 else "πŸ‘Ž"
        return f"{emoji} Feedback saved! Thank you for helping improve HousingMind."
    except Exception as e:
        return f"Error saving feedback: {str(e)}"


def get_system_stats() -> str:
    """Get system statistics for display."""
    stats_text = "### System Status\n\n"

    # RAG engine status
    if rag_engine:
        db_stats = rag_engine.vector_db.get_collection_stats()
        stats_text += f"**Documents:** {db_stats['total_documents']:,} chunks\n"
        stats_text += f"**Model:** {rag_engine.model}\n"
        stats_text += f"**Embeddings:** {db_stats['embedding_model']}\n\n"
    else:
        stats_text += "**Status:** Not initialized\n\n"

    # Feedback stats
    if feedback_db:
        fb_stats = feedback_db.get_feedback_stats()
        stats_text += "### Feedback Statistics\n\n"
        stats_text += f"**Total Queries:** {fb_stats['total']}\n"
        stats_text += f"**Rated:** {fb_stats['rated']}\n"
        if fb_stats['rated'] > 0:
            stats_text += f"**Helpful:** {fb_stats['helpful']} ({fb_stats['helpful_pct']}%)\n"
        if fb_stats['avg_response_time_ms']:
            stats_text += f"**Avg Response Time:** {fb_stats['avg_response_time_ms']}ms\n"

    return stats_text


def create_interface() -> gr.Blocks:
    """Create the Gradio interface."""

    # Example queries for users to try
    example_queries = [
        "What are tenant rights during a RAD conversion?",
        "How do PHAs calculate annual income for HCV?",
        "What are the portability requirements for voucher holders?",
        "When can a PHA deny an applicant based on criminal history?",
        "What is the difference between HQS and NSPIRE inspections?",
    ]

    with gr.Blocks(
        title="HousingMind",
        theme=gr.themes.Soft(),
        css="""
        .feedback-btn { min-width: 100px; }
        .main-container { max-width: 1200px; margin: auto; }
        """
    ) as demo:
        # Session state
        session_state = gr.State(None)
        current_feedback_id = gr.State(None)

        gr.Markdown("""
        # HousingMind
        ### AI-Powered Housing Policy Assistant

        Ask questions about HUD regulations, PIH notices, Housing Choice Vouchers,
        Public Housing, LIHTC, and more. Responses are based on official HUD documentation.
        """)

        with gr.Row():
            with gr.Column(scale=3):
                # Query input
                query_input = gr.Textbox(
                    label="Ask a housing policy question",
                    placeholder="Example: What are the income limits for the HCV program?",
                    lines=3,
                    max_lines=5
                )

                with gr.Row():
                    submit_btn = gr.Button("Ask HousingMind", variant="primary", scale=2)
                    clear_btn = gr.Button("Clear", scale=1)

                # Example queries
                gr.Markdown("**Try these examples:**")
                for example in example_queries:
                    gr.Button(
                        example[:50] + "..." if len(example) > 50 else example,
                        size="sm"
                    ).click(
                        fn=lambda x=example: x,
                        outputs=query_input
                    )

            with gr.Column(scale=1):
                stats_display = gr.Markdown(get_system_stats())
                refresh_stats_btn = gr.Button("Refresh Stats", size="sm")

        # Response area
        gr.Markdown("---")

        response_output = gr.Markdown(
            label="Response",
            value="*Ask a question to get started.*"
        )

        # Feedback section
        gr.Markdown("---")
        gr.Markdown("### Was this response helpful?")

        with gr.Row():
            thumbs_up = gr.Button("πŸ‘ Helpful", variant="primary", elem_classes="feedback-btn")
            thumbs_down = gr.Button("πŸ‘Ž Not Helpful", variant="secondary", elem_classes="feedback-btn")

        feedback_comment = gr.Textbox(
            label="Additional feedback (optional)",
            placeholder="What was good/bad about this response? What was missing?",
            lines=2,
            max_lines=4
        )

        feedback_status = gr.Markdown("")

        # Event handlers
        def on_submit(query, state):
            response, new_state, fb_id = answer_query(query, state)
            return response, new_state, fb_id, ""  # Clear feedback status

        submit_btn.click(
            fn=on_submit,
            inputs=[query_input, session_state],
            outputs=[response_output, session_state, current_feedback_id, feedback_status]
        )

        query_input.submit(
            fn=on_submit,
            inputs=[query_input, session_state],
            outputs=[response_output, session_state, current_feedback_id, feedback_status]
        )

        clear_btn.click(
            fn=lambda: ("", "*Ask a question to get started.*", ""),
            outputs=[query_input, response_output, feedback_status]
        )

        thumbs_up.click(
            fn=lambda c, s: submit_rating(1, c, s),
            inputs=[feedback_comment, session_state],
            outputs=feedback_status
        )

        thumbs_down.click(
            fn=lambda c, s: submit_rating(-1, c, s),
            inputs=[feedback_comment, session_state],
            outputs=feedback_status
        )

        refresh_stats_btn.click(
            fn=get_system_stats,
            outputs=stats_display
        )

        # Footer
        gr.Markdown("""
        ---
        *HousingMind uses Llama 3.1 8B and searches through 2,500+ HUD policy documents.
        Your feedback helps improve the system.*
        """)

    return demo


def main():
    """Launch the web interface."""
    import argparse

    parser = argparse.ArgumentParser(description="HousingMind Web Interface")
    parser.add_argument("--host", default="127.0.0.1", help="Host to bind to")
    parser.add_argument("--port", type=int, default=7860, help="Port to listen on")
    parser.add_argument("--share", action="store_true", help="Create public link")

    args = parser.parse_args()

    # Initialize system
    print("Initializing HousingMind...")
    initialize_system()

    # Create and launch interface
    demo = create_interface()

    print(f"\nLaunching HousingMind at http://{args.host}:{args.port}")
    if args.share:
        print("Creating public share link...")

    demo.launch(
        server_name=args.host,
        server_port=args.port,
        share=args.share
    )


if __name__ == "__main__":
    main()