File size: 14,170 Bytes
7ddc93d
 
1eb5783
 
7d19cfc
af11e83
1eb5783
6ff89e0
7d19cfc
 
1eb5783
 
7ddc93d
 
 
 
 
 
1eb5783
 
 
 
 
59d3a91
cfb1a62
59d3a91
7d19cfc
 
7ddc93d
 
 
 
 
7d19cfc
af11e83
59d3a91
 
 
af11e83
 
 
59d3a91
669d93a
183168e
7ddc93d
 
 
 
85bbaed
 
7d19cfc
85bbaed
 
 
7d19cfc
85bbaed
7d19cfc
85bbaed
 
 
7d19cfc
85bbaed
 
7ddc93d
 
 
85bbaed
7ddc93d
 
 
7d19cfc
85bbaed
af11e83
 
85bbaed
 
 
 
 
 
 
 
 
7ddc93d
 
 
85bbaed
 
 
 
7ddc93d
 
 
af11e83
 
 
 
 
 
 
7ddc93d
af11e83
 
85bbaed
 
 
7ddc93d
85bbaed
7d19cfc
669d93a
1eb5783
af11e83
669d93a
af11e83
 
 
 
 
7ddc93d
669d93a
af11e83
 
7ddc93d
af11e83
 
 
 
1eb5783
af11e83
669d93a
daee42b
 
7ddc93d
1eb5783
 
52a64e1
 
 
669d93a
af11e83
1eb5783
 
 
 
 
c6040d0
d377a8f
daee42b
 
1eb5783
 
 
af11e83
 
 
 
 
7ddc93d
af11e83
 
 
7ddc93d
af11e83
1eb5783
af11e83
daee42b
1eb5783
 
 
 
 
7ddc93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eb5783
 
daee42b
52a64e1
 
7d19cfc
 
1eb5783
 
 
 
 
 
 
52a64e1
7ddc93d
85bbaed
 
7ddc93d
 
af11e83
85bbaed
7ddc93d
85bbaed
52a64e1
1eb5783
 
 
52a64e1
1eb5783
 
 
 
 
 
52a64e1
1eb5783
 
 
daee42b
1eb5783
 
7ddc93d
1eb5783
 
85bbaed
7ddc93d
 
 
 
 
 
 
 
 
 
85bbaed
 
 
1eb5783
af11e83
 
7ddc93d
 
 
af11e83
 
 
 
 
7ddc93d
 
 
af11e83
 
 
1eb5783
af11e83
1eb5783
7ddc93d
 
 
660cea6
af11e83
1eb5783
 
 
 
 
 
 
 
85bbaed
7ddc93d
85bbaed
 
 
 
 
 
 
 
 
 
 
 
7ddc93d
 
 
 
 
 
 
 
1eb5783
7ddc93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eb5783
 
85bbaed
1eb5783
85bbaed
1eb5783
 
7ddc93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85bbaed
1eb5783
 
 
 
52a64e1
daee42b
1eb5783
d762ede
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# app.py

import json
from typing import List, Tuple
import os
import logging

import gradio as gr
from dotenv import load_dotenv
from slugify import slugify

from rag.rag_pipeline import RAGPipeline
from utils.helpers import (
    generate_follow_up_questions,
    append_to_study_files,
    add_study_files_to_chromadb,
    chromadb_client,
)
from utils.prompts import (
    highlight_prompt,
    evidence_based_prompt,
    sample_questions,
)
import openai

from config import STUDY_FILES, OPENAI_API_KEY
from utils.zotero_manager import ZoteroManager

import csv
import io

import datetime

load_dotenv()
logging.basicConfig(level=logging.INFO)

openai.api_key = OPENAI_API_KEY

# After loop, add all collected data to ChromaDB
add_study_files_to_chromadb("study_files.json", "study_files_collection")

# Cache for RAG pipelines
rag_cache = {}


def process_zotero_library_items(
    zotero_library_id: str, zotero_api_access_key: str
) -> str:
    if not zotero_library_id or not zotero_api_access_key:
        return "Please enter your zotero library Id and API Access Key"

    zotero_library_id = zotero_library_id
    zotero_library_type = "user"  # or "group"
    zotero_api_access_key = zotero_api_access_key

    message = ""

    try:
        zotero_manager = ZoteroManager(
            zotero_library_id, zotero_library_type, zotero_api_access_key
        )

        zotero_collections = zotero_manager.get_collections()
        zotero_collection_lists = zotero_manager.list_zotero_collections(
            zotero_collections
        )
        filtered_zotero_collection_lists = (
            zotero_manager.filter_and_return_collections_with_items(
                zotero_collection_lists
            )
        )

        study_files_data = {}  # Dictionary to collect items for ChromaDB

        for collection in filtered_zotero_collection_lists:
            collection_name = collection.get("name")
            if collection_name not in STUDY_FILES:
                collection_key = collection.get("key")
                collection_items = zotero_manager.get_collection_items(collection_key)
                zotero_collection_items = (
                    zotero_manager.get_collection_zotero_items_by_key(collection_key)
                )
                #### Export zotero collection items to json ####
                zotero_items_json = zotero_manager.zotero_items_to_json(
                    zotero_collection_items
                )
                export_file = f"{slugify(collection_name)}_zotero_items.json"
                zotero_manager.write_zotero_items_to_json_file(
                    zotero_items_json, f"data/{export_file}"
                )
                append_to_study_files(
                    "study_files.json", collection_name, f"data/{export_file}"
                )

                # Collect for ChromaDB
                study_files_data[collection_name] = f"data/{export_file}"

                # Update in-memory STUDY_FILES for reference in current session
                STUDY_FILES.update({collection_name: f"data/{export_file}"})
                logging.info(f"STUDY_FILES: {STUDY_FILES}")

        # After loop, add all collected data to ChromaDB
        add_study_files_to_chromadb("study_files.json", "study_files_collection")
        message = "Successfully processed items in your zotero library"
    except Exception as e:
        message = f"Error process your zotero library: {str(e)}"

    return message


def get_rag_pipeline(study_name: str) -> RAGPipeline:
    """Get or create a RAGPipeline instance for the given study by querying ChromaDB."""
    if study_name not in rag_cache:
        # Query ChromaDB for the study file path by ID
        collection = chromadb_client.get_or_create_collection("study_files_collection")
        result = collection.get(ids=[study_name])  # Retrieve document by ID

        # Check if the result contains the requested document
        if not result or len(result["metadatas"]) == 0:
            raise ValueError(f"Invalid study name: {study_name}")

        # Extract the file path from the document metadata
        study_file = result["metadatas"][0].get("file_path")
        if not study_file:
            raise ValueError(f"File path not found for study name: {study_name}")

        # Create and cache the RAGPipeline instance
        rag_cache[study_name] = RAGPipeline(study_file)

    return rag_cache[study_name]


def chat_function(message: str, study_name: str, prompt_type: str) -> str:
    """Process a chat message and generate a response using the RAG pipeline."""

    if not message.strip():
        return "Please enter a valid query."

    rag = get_rag_pipeline(study_name)
    logging.info(f"rag: ==> {rag}")
    prompt = {
        "Highlight": highlight_prompt,
        "Evidence-based": evidence_based_prompt,
    }.get(prompt_type)

    response = rag.query(message, prompt_template=prompt)
    return response.response


def get_study_info(study_name: str) -> str:
    """Retrieve information about the specified study."""

    collection = chromadb_client.get_or_create_collection("study_files_collection")
    result = collection.get(ids=[study_name])  # Query by study name (as a list)
    logging.info(f"Result: ======> {result}")

    # Check if the document exists in the result
    if not result or len(result["metadatas"]) == 0:
        raise ValueError(f"Invalid study name: {study_name}")

    # Extract the file path from the document metadata
    study_file = result["metadatas"][0].get("file_path")
    logging.info(f"study_file: =======> {study_file}")
    if not study_file:
        raise ValueError(f"File path not found for study name: {study_name}")

    with open(study_file, "r") as f:
        data = json.load(f)
    return f"### Number of documents: {len(data)}"


def markdown_table_to_csv(markdown_text: str) -> str:
    """Convert a markdown table to CSV format."""
    # Split the text into lines and remove empty lines
    lines = [line.strip() for line in markdown_text.split("\n") if line.strip()]

    # Find the table content (lines starting with |)
    table_lines = [line for line in lines if line.startswith("|")]

    if not table_lines:
        return ""

    # Process each line to extract cell values
    csv_data = []
    for line in table_lines:
        # Skip separator lines (containing only dashes)
        if "---" in line:
            continue
        # Split by |, remove empty strings, and strip whitespace
        cells = [cell.strip() for cell in line.split("|") if cell.strip()]
        csv_data.append(cells)

    # Create CSV string
    output = io.StringIO()
    writer = csv.writer(output)
    writer.writerows(csv_data)
    return output.getvalue()


def update_interface(study_name: str) -> Tuple[str, gr.update, gr.update, gr.update]:
    """Update the interface based on the selected study."""

    study_info = get_study_info(study_name)
    questions = sample_questions.get(study_name, [])[:3]
    if not questions:
        questions = sample_questions.get("General", [])[:3]
    visible_questions = [gr.update(visible=True, value=q) for q in questions]
    hidden_questions = [gr.update(visible=False) for _ in range(3 - len(questions))]
    return (study_info, *visible_questions, *hidden_questions)


def set_question(question: str) -> str:
    return question.lstrip("✨ ")


def process_multi_input(text, study_name, prompt_type):
    # Split input based on commas and strip any extra spaces
    variable_list = [word.strip().upper() for word in text.split(",")]
    user_message = f"Extract and present in a tabular format the following variables for each {study_name} study: {', '.join(variable_list)}"
    logging.info(f"User message: ==> {user_message}")
    response = chat_function(user_message, study_name, prompt_type)
    return [response, gr.update(visible=True)]


def create_gr_interface() -> gr.Blocks:
    """
    Create and configure the Gradio interface for the RAG platform.

    This function sets up the entire user interface, including:
    - Chat interface with message input and display
    - Study selection dropdown
    - Sample and follow-up question buttons
    - Prompt type selection
    - Event handlers for user interactions

    Returns:
        gr.Blocks: The configured Gradio interface ready for launching.
    """

    with gr.Blocks() as demo:
        gr.Markdown("# ACRES RAG Platform")

        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("### Zotero Credentials")
                zotero_library_id = gr.Textbox(
                    label="Zotero Library ID",
                    type="password",
                    placeholder="Enter Your Zotero Library ID here...",
                )
                zotero_api_access_key = gr.Textbox(
                    label="Zotero API Access Key",
                    type="password",
                    placeholder="Enter Your Zotero API Access Key...",
                )
                process_zotero_btn = gr.Button("Process your Zotero Library")
                zotero_output = gr.Markdown(label="Zotero")

                gr.Markdown("### Study Information")

                # Query ChromaDB for all document IDs in the "study_files_collection" collection
                collection = chromadb_client.get_or_create_collection(
                    "study_files_collection"
                )
                # Retrieve all documents by querying with an empty string and specifying a high n_results
                all_documents = collection.query(query_texts=[""], n_results=1000)
                logging.info(f"all_documents: =========> {all_documents}")
                # Extract document IDs as study names
                document_ids = all_documents.get("ids")
                study_choices = [
                    doc_id for doc_id in document_ids[0] if document_ids
                ]  # Get list of document IDs
                logging.info(f"study_choices: ======> {study_choices}")

                # Update the Dropdown with choices from ChromaDB
                study_dropdown = gr.Dropdown(
                    choices=study_choices,
                    label="Select Study",
                    value=(
                        study_choices[0] if study_choices else None
                    ),  # Set first choice as default, if available
                )

                study_info = gr.Markdown(label="Study Details")

                gr.Markdown("### Settings")
                prompt_type = gr.Radio(
                    ["Default", "Highlight", "Evidence-based"],
                    label="Prompt Type",
                    value="Default",
                )
                # clear = gr.Button("Clear Chat")

            with gr.Column(scale=3):
                gr.Markdown("### Study Variables")
                with gr.Row():
                    study_variables = gr.Textbox(
                        show_label=False,
                        placeholder="Type your variables separated by commas e.g (Study ID, Study Title, Authors etc)",
                        scale=4,
                        lines=1,
                        autofocus=True,
                    )
                    submit_btn = gr.Button("Submit", scale=1)
                answer_output = gr.Markdown(label="Answer")
                # button to download_csv
                download_btn = gr.DownloadButton(
                    "Download as CSV",
                    variant="primary",
                    size="sm",
                    scale=1,
                    visible=False,
                )

        def download_as_csv(markdown_content):
            """Convert markdown table to CSV and provide for download."""
            if not markdown_content:
                return None

            csv_content = markdown_table_to_csv(markdown_content)
            if not csv_content:
                return None

            # Create temporary file with actual content
            timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
            temp_path = f"study_export_{timestamp}.csv"

            with open(temp_path, "w", newline="", encoding="utf-8") as f:
                f.write(csv_content)

            return temp_path

        def cleanup_temp_files():
            """Clean up old temporary files."""
            try:
                # Delete files older than 5 minutes
                current_time = datetime.datetime.now()
                for file in os.listdir():
                    if file.startswith("study_export_") and file.endswith(".csv"):
                        file_time = datetime.datetime.fromtimestamp(
                            os.path.getmtime(file)
                        )
                        if (current_time - file_time).seconds > 30:  # 5 minutes
                            try:
                                os.remove(file)
                            except Exception as e:
                                logging.warning(
                                    f"Failed to remove temp file {file}: {e}"
                                )
            except Exception as e:
                logging.warning(f"Error during cleanup: {e}")

        study_dropdown.change(
            fn=get_study_info,
            inputs=study_dropdown,
            outputs=[study_info],
        )

        process_zotero_btn.click(
            process_zotero_library_items,
            inputs=[zotero_library_id, zotero_api_access_key],
            outputs=[zotero_output],
            queue=False,
        )
        submit_btn.click(
            process_multi_input,
            inputs=[study_variables, study_dropdown, prompt_type],
            outputs=[answer_output, download_btn],
            queue=False,
        )
        download_btn.click(
            fn=download_as_csv,
            inputs=[answer_output],
            outputs=[download_btn],
        ).then(
            fn=cleanup_temp_files, inputs=None, outputs=None  # Clean up after download
        )

    return demo


demo = create_gr_interface()

if __name__ == "__main__":
    # demo = create_gr_interface()
    demo.launch(share=True, debug=True)