File size: 10,114 Bytes
5fffd14
 
 
 
8207117
 
 
 
 
 
5fffd14
e5495b5
8207117
 
 
 
 
 
5fffd14
8207117
 
 
 
 
5fffd14
8207117
 
 
5fffd14
e5495b5
 
 
8207117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fffd14
 
 
8207117
 
 
e5495b5
 
8207117
 
 
5fffd14
 
 
 
 
 
 
 
 
 
8207117
 
5fffd14
8207117
 
5fffd14
8207117
 
 
 
 
 
 
5fffd14
 
 
8207117
 
 
 
5fffd14
8207117
 
 
 
 
 
 
 
5fffd14
8207117
 
 
 
 
 
 
 
 
 
 
 
 
 
5fffd14
 
8207117
 
 
5fffd14
 
 
 
 
8207117
 
 
 
 
 
 
5fffd14
 
 
 
 
8207117
 
 
 
 
 
 
 
 
5fffd14
 
 
 
 
 
 
 
 
 
 
 
 
 
8207117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fffd14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8207117
5fffd14
 
 
 
 
 
 
 
 
 
 
 
 
8207117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import gradio as gr
import tempfile
import pandas as pd
import sqlite3
from langchain_core.prompts import ChatPromptTemplate
#test
# Add parent directory to path to import backend modules
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from backend.main import process_query, upload_document, process_voice, DocumentAssistant
from backend.db import SQLiteDB
from backend.vector_db import ChromaVectorDB
from backend.query_engine import QueryEngine
from backend.voice_assist import VoiceAssistant
from backend.document_parser import DocumentParser
from backend.agents import DocumentAgents

# Initialize components
sqlite_db = SQLiteDB()
vector_db = ChromaVectorDB(os.getenv("CHROMA_DB_PATH", "./data/chroma_db"))
query_engine = QueryEngine()
voice_assistant = VoiceAssistant()

# Initialize the document parser and agents
document_parser = DocumentParser()
document_agents = DocumentAgents()

# Initialize DocumentAssistant
document_assistant = DocumentAssistant()

# Define the prompt with examples
query_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", """
            You are an SQL and data analysis expert. Generate an appropriate SQL query using SQLite syntax for the question provided, without any explanations or code comments.
            Follow SQLite-specific conventions, as shown in the examples below:
            
            Example 1:
            Question: "What is the average fare for trips over 10 miles?"
            SQL Query: SELECT AVG(fare_amount) FROM taxi_data WHERE trip_distance > 10;

            Example 2:
            Question: "How many trips were taken in each month?"
            SQL Query: SELECT strftime('%m', pickup_datetime) AS month, COUNT(*) AS trip_count FROM taxi_data GROUP BY month;

            Example 3:
            Question: "What is the total fare amount for each driver (medallion) per day?"
            SQL Query: SELECT DATE(pickup_datetime) AS date, medallion, SUM(fare_amount) AS total_fare FROM taxi_data GROUP BY date, medallion;
            
            SQLite-Specific Conventions:
            
            1. Date and Time Extraction:
               - Instead of `EXTRACT(YEAR FROM column)`, use `strftime('%Y', column)` to extract the year.
               - Example: `SELECT strftime('%Y', pickup_datetime) FROM taxi_data;`

            2. String Length:
               - Instead of `CHAR_LENGTH(column)`, use `LENGTH(column)`.
               - Example: `SELECT LENGTH(passenger_name) FROM taxi_data;`

            3. Regular Expressions:
               - SQLite does not support `REGEXP`. Use `LIKE` for simple patterns or avoid regular expressions.
               - Example: `SELECT * FROM taxi_data WHERE passenger_name LIKE 'A%';`

            4. Window Functions:
               - For row numbering, use `ROW_NUMBER()` if supported, or simulate with joins.
               - Example: `SELECT id, ROW_NUMBER() OVER (ORDER BY pickup_datetime) AS row_num FROM taxi_data;`

            5. Data Type Casting:
               - Use `CAST(column AS TYPE)`, but note that SQLite supports limited types.
               - Example: `SELECT CAST(fare_amount AS INTEGER) FROM taxi_data;`

            6. Full Outer Join Workaround:
               - SQLite doesn't support `FULL OUTER JOIN`. Combine `LEFT JOIN` and `UNION` for a similar effect.
               - Example:
                 ```
                 SELECT a.*, b.*
                 FROM table_a a
                 LEFT JOIN table_b b ON a.id = b.id
                 UNION
                 SELECT a.*, b.*
                 FROM table_a a
                 RIGHT JOIN table_b b ON a.id = b.id;
                 ```

            Use these examples and guidelines to generate an SQL query compatible with SQLite syntax for the question provided.
        """),
        ("human", "{question}"),
    ]
)

def process_text_query(query, history):
    """Process a text query and update chat history"""
    # Log query to database
    sqlite_db.log_query(query)
    
    # Use DocumentAssistant to process the query
    response = document_assistant.process_query(query)
    
    # Update database with response
    sqlite_db.log_query(query, response)
    
    # Update history
    history.append((query, response))
    return "", history

def process_file_upload(files):
    """Process uploaded files and index them"""
    file_info = []
    for file in files:
        file_path = file.name
        file_name = os.path.basename(file_path)
        file_type = os.path.splitext(file_name)[1].lower()
        
        # Parse document
        text_chunks = document_parser.parse_document(file_path)
        
        # Add to SQLite DB
        doc_id = sqlite_db.add_document(file_name, file_path, file_type)
        
        # Add to vector DB
        vector_db.add_document(file_path, text_chunks, {"doc_id": doc_id})
        
        file_info.append(f"Indexed: {file_name} ({len(text_chunks)} chunks)")
    
    return "\n".join(file_info)

def process_voice_input(audio_path):
    """Process voice input and return transcribed text"""
    if audio_path is None:
        return "No audio recorded"
    
    # Transcribe audio
    text = voice_assistant.speech_to_text(audio_path)
    return text

def text_to_speech_output(text):
    """Convert text to speech"""
    if not text:
        return None
    
    audio_path = voice_assistant.text_to_speech(text)
    return audio_path

def load_csv_to_sqlite(file_path, conn):
    # Read the CSV in chunks
    chunksize = 1000  # Adjust based on your memory constraints
    for chunk in pd.read_csv(file_path, chunksize=chunksize):
        # Perform any necessary data cleaning on the chunk
        if 'pickup_datetime' in chunk.columns:
            chunk['pickup_datetime'] = pd.to_datetime(chunk['pickup_datetime'], errors='coerce')
            chunk = chunk.dropna(subset=['pickup_datetime'])
        
        # Load the chunk into the SQLite database
        chunk.to_sql('data_tab', conn, if_exists='append', index=False, method='multi')

# Create Gradio interface
with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
    gr.Markdown("# πŸ€– AI Document Analysis & Voice Assistant")
    gr.Markdown("Upload documents, ask questions, and get voice responses!")
    
    with gr.Tab("Chat"):
        chatbot = gr.Chatbot(height=400)
        
        with gr.Row():
            with gr.Column(scale=8):
                msg = gr.Textbox(
                    placeholder="Ask a question about your documents...",
                    show_label=False
                )
            with gr.Column(scale=1):
                voice_btn = gr.Button("🎀")
        
        with gr.Row():
            submit_btn = gr.Button("Submit")
            clear_btn = gr.Button("Clear")
        
        audio_output = gr.Audio(label="Voice Response", type="filepath")
        
        # Voice input
        voice_input = gr.Audio(
            label="Voice Input", 
            type="filepath",
            visible=False
        )
        
        # Event handlers
        submit_btn.click(
            process_text_query, 
            inputs=[msg, chatbot], 
            outputs=[msg, chatbot]
        )
        
        msg.submit(
            process_text_query, 
            inputs=[msg, chatbot], 
            outputs=[msg, chatbot]
        )
        
        clear_btn.click(lambda: None, None, chatbot, queue=False)
        
        voice_btn.click(
            lambda: gr.update(visible=True),
            None,
            voice_input
        )
        
        voice_input.change(
            process_voice_input,
            inputs=[voice_input],
            outputs=[msg]
        )
        
        # Add TTS functionality
        tts_btn = gr.Button("πŸ”Š Speak Response")
        tts_btn.click(
            text_to_speech_output,
            inputs=[chatbot],
            outputs=[audio_output]
        )
    
    with gr.Tab("Document Upload"):
        file_upload = gr.File(
            label="Upload Documents",
            file_types=[".pdf", ".txt", ".docx", ".csv", ".xlsx"],
            file_count="multiple"
        )
        upload_button = gr.Button("Process & Index Documents")
        upload_output = gr.Textbox(label="Upload Status")
        
        upload_button.click(
            process_file_upload,
            inputs=[file_upload],
            outputs=[upload_output]
        )
    
    with gr.Tab("Settings"):
        gr.Markdown("## System Settings")
        api_key = gr.Textbox(
            label="Groq API Key",
            placeholder="Enter your Groq API key",
            type="password"
        )
        save_btn = gr.Button("Save Settings")
        
        def save_settings(key):
            os.environ["GROQ_API_KEY"] = key
            return "Settings saved!"
        
        save_btn.click(
            save_settings,
            inputs=[api_key],
            outputs=[gr.Textbox(label="Status")]
        )

    with gr.Tab("Advanced Query"):
        gr.Markdown("# 🧠 Complex Query Processing")
        gr.Markdown("Use AI agents to process complex queries about your documents")
        
        complex_chatbot = gr.Chatbot(height=400)
        
        with gr.Row():
            complex_msg = gr.Textbox(
                placeholder="Ask a complex question requiring analysis...",
                show_label=False
            )
        
        with gr.Row():
            complex_submit_btn = gr.Button("Process with Agents")
            complex_clear_btn = gr.Button("Clear")
        
        # Event handlers
        complex_submit_btn.click(
            process_complex_query, 
            inputs=[complex_msg, complex_chatbot], 
            outputs=[complex_msg, complex_chatbot]
        )
        
        complex_msg.submit(
            process_complex_query, 
            inputs=[complex_msg, complex_chatbot], 
            outputs=[complex_msg, complex_chatbot]
        )
        
        complex_clear_btn.click(lambda: None, None, complex_chatbot, queue=False)

# Launch the app
if __name__ == "__main__":
    demo.launch()