Spaces:
Sleeping
Sleeping
Patrick Walukagga
commited on
Commit
·
85bbaed
1
Parent(s):
7d19cfc
Users can provide their zotero credentials and study variables
Browse files
app.py
CHANGED
@@ -26,35 +26,47 @@ openai.api_key = OPENAI_API_KEY
|
|
26 |
# Cache for RAG pipelines
|
27 |
rag_cache = {}
|
28 |
|
29 |
-
zotero_library_id
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
zotero_collection_lists = zotero_manager.list_zotero_collections(zotero_collections)
|
39 |
-
filtered_zotero_collection_lists = (
|
40 |
-
zotero_manager.filter_and_return_collections_with_items(zotero_collection_lists)
|
41 |
-
)
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
collection_key = collection.get("key")
|
47 |
-
collection_items = zotero_manager.get_collection_items(collection_key)
|
48 |
-
zotero_collection_items = (
|
49 |
-
zotero_manager.get_collection_zotero_items_by_key(collection_key)
|
50 |
)
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
)
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
|
60 |
def get_rag_pipeline(study_name: str) -> RAGPipeline:
|
@@ -68,7 +80,7 @@ def get_rag_pipeline(study_name: str) -> RAGPipeline:
|
|
68 |
|
69 |
|
70 |
def chat_function(
|
71 |
-
message: str,
|
72 |
) -> str:
|
73 |
"""Process a chat message and generate a response using the RAG pipeline."""
|
74 |
|
@@ -112,6 +124,13 @@ def update_interface(study_name: str) -> Tuple[str, gr.update, gr.update, gr.upd
|
|
112 |
def set_question(question: str) -> str:
|
113 |
return question.lstrip("✨ ")
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
def create_gr_interface() -> gr.Blocks:
|
117 |
"""
|
@@ -130,29 +149,15 @@ def create_gr_interface() -> gr.Blocks:
|
|
130 |
|
131 |
with gr.Blocks() as demo:
|
132 |
gr.Markdown("# ACRES RAG Platform")
|
133 |
-
|
134 |
with gr.Row():
|
135 |
-
with gr.Column(scale=2):
|
136 |
-
chatbot = gr.Chatbot(
|
137 |
-
elem_id="chatbot",
|
138 |
-
show_label=False,
|
139 |
-
height=600,
|
140 |
-
container=False,
|
141 |
-
show_copy_button=False,
|
142 |
-
layout="bubble",
|
143 |
-
visible=True,
|
144 |
-
)
|
145 |
-
with gr.Row():
|
146 |
-
msg = gr.Textbox(
|
147 |
-
show_label=False,
|
148 |
-
placeholder="Type your message here...",
|
149 |
-
scale=4,
|
150 |
-
lines=1,
|
151 |
-
autofocus=True,
|
152 |
-
)
|
153 |
-
send_btn = gr.Button("Send", scale=1)
|
154 |
-
|
155 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
gr.Markdown("### Study Information")
|
157 |
study_dropdown = gr.Dropdown(
|
158 |
choices=list(STUDY_FILES.keys()),
|
@@ -160,17 +165,6 @@ def create_gr_interface() -> gr.Blocks:
|
|
160 |
value=list(STUDY_FILES.keys())[0],
|
161 |
)
|
162 |
study_info = gr.Markdown(label="Study Details")
|
163 |
-
with gr.Accordion("Sample Questions", open=False):
|
164 |
-
sample_btns = [
|
165 |
-
gr.Button(f"Sample Question {i+1}", visible=False)
|
166 |
-
for i in range(3)
|
167 |
-
]
|
168 |
-
|
169 |
-
gr.Markdown("### ✨ Generated Questions")
|
170 |
-
with gr.Row():
|
171 |
-
follow_up_btns = [
|
172 |
-
gr.Button(f"Follow-up {i+1}", visible=False) for i in range(3)
|
173 |
-
]
|
174 |
|
175 |
gr.Markdown("### Settings")
|
176 |
prompt_type = gr.Radio(
|
@@ -178,7 +172,20 @@ def create_gr_interface() -> gr.Blocks:
|
|
178 |
label="Prompt Type",
|
179 |
value="Default",
|
180 |
)
|
181 |
-
clear = gr.Button("Clear Chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
|
183 |
def user(
|
184 |
user_message: str, history: List[List[str]]
|
@@ -189,7 +196,7 @@ def create_gr_interface() -> gr.Blocks:
|
|
189 |
|
190 |
def bot(
|
191 |
history: List[List[str]], study_name: str, prompt_type: str
|
192 |
-
) ->
|
193 |
"""
|
194 |
Generate bot response and update the interface.
|
195 |
|
@@ -216,42 +223,32 @@ def create_gr_interface() -> gr.Blocks:
|
|
216 |
bot_message = chat_function(user_message, history, study_name, prompt_type)
|
217 |
history[-1][1] = bot_message
|
218 |
|
219 |
-
|
220 |
-
follow_up_questions = generate_follow_up_questions(
|
221 |
-
rag, bot_message, user_message, study_name
|
222 |
-
)
|
223 |
-
|
224 |
-
visible_questions = [
|
225 |
-
gr.update(visible=True, value=q) for q in follow_up_questions
|
226 |
-
]
|
227 |
-
hidden_questions = [
|
228 |
-
gr.update(visible=False) for _ in range(3 - len(follow_up_questions))
|
229 |
-
]
|
230 |
-
|
231 |
-
return (history, *visible_questions, *hidden_questions)
|
232 |
|
233 |
-
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
)
|
238 |
-
send_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
)
|
|
|
|
|
243 |
|
244 |
-
|
245 |
-
btn.click(set_question, inputs=[btn], outputs=[msg])
|
246 |
-
|
247 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
248 |
|
249 |
study_dropdown.change(
|
250 |
-
fn=
|
251 |
inputs=study_dropdown,
|
252 |
-
outputs=[study_info
|
253 |
)
|
254 |
|
|
|
|
|
|
|
255 |
return demo
|
256 |
|
257 |
|
|
|
26 |
# Cache for RAG pipelines
|
27 |
rag_cache = {}
|
28 |
|
29 |
+
def process_zotero_library_items(zotero_library_id: str, zotero_api_access_key: str) -> str:
|
30 |
+
if not zotero_library_id or not zotero_api_access_key:
|
31 |
+
return "Please enter your zotero library Id and API Access Key"
|
32 |
|
33 |
+
zotero_library_id = zotero_library_id
|
34 |
+
zotero_library_type = "user" # or "group"
|
35 |
+
zotero_api_access_key = zotero_api_access_key
|
36 |
|
37 |
+
message = ""
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
try:
|
40 |
+
zotero_manager = ZoteroManager(
|
41 |
+
zotero_library_id, zotero_library_type, zotero_api_access_key
|
|
|
|
|
|
|
|
|
42 |
)
|
43 |
+
|
44 |
+
zotero_collections = zotero_manager.get_collections()
|
45 |
+
zotero_collection_lists = zotero_manager.list_zotero_collections(zotero_collections)
|
46 |
+
filtered_zotero_collection_lists = (
|
47 |
+
zotero_manager.filter_and_return_collections_with_items(zotero_collection_lists)
|
48 |
)
|
49 |
+
|
50 |
+
for collection in filtered_zotero_collection_lists:
|
51 |
+
collection_name = collection.get("name")
|
52 |
+
if collection_name not in STUDY_FILES:
|
53 |
+
collection_key = collection.get("key")
|
54 |
+
collection_items = zotero_manager.get_collection_items(collection_key)
|
55 |
+
zotero_collection_items = (
|
56 |
+
zotero_manager.get_collection_zotero_items_by_key(collection_key)
|
57 |
+
)
|
58 |
+
#### Export zotero collection items to json ####
|
59 |
+
zotero_items_json = zotero_manager.zotero_items_to_json(zotero_collection_items)
|
60 |
+
export_file = f"{slugify(collection_name)}_zotero_items.json"
|
61 |
+
zotero_manager.write_zotero_items_to_json_file(
|
62 |
+
zotero_items_json, f"data/{export_file}"
|
63 |
+
)
|
64 |
+
append_to_study_files("study_files.json", collection_name, f"data/{export_file}")
|
65 |
+
message = "Successfully processed items in your zotero library"
|
66 |
+
except Exception as e:
|
67 |
+
message = f"Error process your zotero library: {str(e)}"
|
68 |
+
|
69 |
+
return message
|
70 |
|
71 |
|
72 |
def get_rag_pipeline(study_name: str) -> RAGPipeline:
|
|
|
80 |
|
81 |
|
82 |
def chat_function(
|
83 |
+
message: str, study_name: str, prompt_type: str
|
84 |
) -> str:
|
85 |
"""Process a chat message and generate a response using the RAG pipeline."""
|
86 |
|
|
|
124 |
def set_question(question: str) -> str:
|
125 |
return question.lstrip("✨ ")
|
126 |
|
127 |
+
def process_multi_input(text, study_name, prompt_type):
|
128 |
+
# Split input based on commas and strip any extra spaces
|
129 |
+
variable_list = [word.strip().upper() for word in text.split(',')]
|
130 |
+
user_message =f"Extract and present in a tabular format the following variables for each {study_name} study: {', '.join(variable_list)}"
|
131 |
+
response = chat_function(user_message, study_name, prompt_type)
|
132 |
+
return response
|
133 |
+
|
134 |
|
135 |
def create_gr_interface() -> gr.Blocks:
|
136 |
"""
|
|
|
149 |
|
150 |
with gr.Blocks() as demo:
|
151 |
gr.Markdown("# ACRES RAG Platform")
|
152 |
+
|
153 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
with gr.Column(scale=1):
|
155 |
+
gr.Markdown("### Zotero Credentials")
|
156 |
+
zotero_library_id = gr.Textbox(label="Zotero Library ID", type="password", placeholder="Enter Your Zotero Library ID here...")
|
157 |
+
zotero_api_access_key = gr.Textbox(label="Zotero API Access Key", type="password", placeholder="Enter Your Zotero API Access Key...")
|
158 |
+
process_zotero_btn = gr.Button("Process your Zotero Library")
|
159 |
+
zotero_output = gr.Markdown(label="Zotero")
|
160 |
+
|
161 |
gr.Markdown("### Study Information")
|
162 |
study_dropdown = gr.Dropdown(
|
163 |
choices=list(STUDY_FILES.keys()),
|
|
|
165 |
value=list(STUDY_FILES.keys())[0],
|
166 |
)
|
167 |
study_info = gr.Markdown(label="Study Details")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
gr.Markdown("### Settings")
|
170 |
prompt_type = gr.Radio(
|
|
|
172 |
label="Prompt Type",
|
173 |
value="Default",
|
174 |
)
|
175 |
+
# clear = gr.Button("Clear Chat")
|
176 |
+
|
177 |
+
with gr.Column(scale=3):
|
178 |
+
gr.Markdown("### Study Variables")
|
179 |
+
with gr.Row():
|
180 |
+
study_variables = gr.Textbox(
|
181 |
+
show_label=False,
|
182 |
+
placeholder="Type your variables separated by commas e.g (Study ID, Study Title, Authors etc)",
|
183 |
+
scale=4,
|
184 |
+
lines=1,
|
185 |
+
autofocus=True,
|
186 |
+
)
|
187 |
+
submit_btn = gr.Button("Submit", scale=1)
|
188 |
+
answer_output = gr.Markdown(label="Answer")
|
189 |
|
190 |
def user(
|
191 |
user_message: str, history: List[List[str]]
|
|
|
196 |
|
197 |
def bot(
|
198 |
history: List[List[str]], study_name: str, prompt_type: str
|
199 |
+
) -> List[List[str]]:
|
200 |
"""
|
201 |
Generate bot response and update the interface.
|
202 |
|
|
|
223 |
bot_message = chat_function(user_message, history, study_name, prompt_type)
|
224 |
history[-1][1] = bot_message
|
225 |
|
226 |
+
return history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
|
228 |
+
# msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
229 |
+
# bot,
|
230 |
+
# [chatbot, study_dropdown, prompt_type],
|
231 |
+
# [chatbot, *follow_up_btns],
|
232 |
+
# )
|
233 |
+
# send_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
234 |
+
# bot,
|
235 |
+
# [chatbot, study_dropdown, prompt_type],
|
236 |
+
# [chatbot, *follow_up_btns],
|
237 |
+
# )
|
238 |
+
# for btn in follow_up_btns + sample_btns:
|
239 |
+
# btn.click(set_question, inputs=[btn], outputs=[msg])
|
240 |
|
241 |
+
# clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
|
|
|
242 |
|
243 |
study_dropdown.change(
|
244 |
+
fn=get_study_info,
|
245 |
inputs=study_dropdown,
|
246 |
+
outputs=[study_info],
|
247 |
)
|
248 |
|
249 |
+
process_zotero_btn.click(process_zotero_library_items, inputs=[zotero_library_id, zotero_api_access_key], outputs=[zotero_output], queue=False)
|
250 |
+
submit_btn.click(process_multi_input, inputs=[study_variables, study_dropdown, prompt_type], outputs=[answer_output], queue=False)
|
251 |
+
|
252 |
return demo
|
253 |
|
254 |
|