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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import transformers
|
3 |
+
import gradio as gr
|
4 |
+
from ragatouille import RAGPretrainedModel
|
5 |
+
from huggingface_hub import InferenceClient
|
6 |
+
import re
|
7 |
+
from datetime import datetime
|
8 |
+
import json
|
9 |
+
import arxiv
|
10 |
+
from utils import get_md_text_abstract, search_cleaner, get_arxiv_live_search
|
11 |
+
import os
|
12 |
+
import glob
|
13 |
+
|
14 |
+
# ποΈ App configuration - tweak these knobs for maximum brain power! π§ πͺ
|
15 |
+
retrieve_results = 20
|
16 |
+
show_examples = True
|
17 |
+
llm_models_to_choose = ['mistralai/Mixtral-8x7B-Instruct-v0.1', 'mistralai/Mistral-7B-Instruct-v0.2', 'google/gemma-7b-it', 'None']
|
18 |
+
|
19 |
+
# π LLM acting instructions - "To be, or not to be... verbose" π€
|
20 |
+
generate_kwargs = dict(
|
21 |
+
temperature = None,
|
22 |
+
max_new_tokens = 512,
|
23 |
+
top_p = None,
|
24 |
+
do_sample = False,
|
25 |
+
)
|
26 |
+
|
27 |
+
# π§ββοΈ Summoning the RAG model - "Accio knowledge!" πβ¨
|
28 |
+
RAG = RAGPretrainedModel.from_index("colbert/indexes/arxiv_colbert")
|
29 |
+
|
30 |
+
try:
|
31 |
+
gr.Info("ποΈ Setting up the knowledge retriever, please wait... π°οΈ")
|
32 |
+
rag_initial_output = RAG.search("What is Generative AI in Healthcare?", k = 1)
|
33 |
+
gr.Info("π Retriever is up and running! Time to flex those brain muscles! πͺπ§ ")
|
34 |
+
except:
|
35 |
+
gr.Warning("π± Oh no! The retriever took a coffee break. Try again later! β")
|
36 |
+
|
37 |
+
# π The grand introduction - roll out the red carpet! π
|
38 |
+
mark_text = '# π©Ίπ Search Results\n'
|
39 |
+
header_text = "## πArxivπPaperπSearch - π΅οΈββοΈ Uncover, π Summarize, and 𧩠Solve π¬ Research π€β Puzzles βοΈ with π Papers and π€ RAG AI π§ \n"
|
40 |
+
|
41 |
+
# π°οΈ Time travel to find when our knowledge was last updated π
|
42 |
+
try:
|
43 |
+
with open("README.md", "r") as f:
|
44 |
+
mdfile = f.read()
|
45 |
+
date_pattern = r'Index Last Updated : \d{4}-\d{2}-\d{2}'
|
46 |
+
match = re.search(date_pattern, mdfile)
|
47 |
+
date = match.group().split(': ')[1]
|
48 |
+
formatted_date = datetime.strptime(date, '%Y-%m-%d').strftime('%d %b %Y')
|
49 |
+
header_text += f'Index Last Updated: {formatted_date}\n'
|
50 |
+
index_info = f"Semantic Search - up to {formatted_date}"
|
51 |
+
except:
|
52 |
+
index_info = "Semantic Search"
|
53 |
+
|
54 |
+
database_choices = [index_info, 'Arxiv Search - Latest - (EXPERIMENTAL)']
|
55 |
+
|
56 |
+
# π¦ Arxiv API - the wise old owl of academic knowledge π
|
57 |
+
arx_client = arxiv.Client()
|
58 |
+
is_arxiv_available = True
|
59 |
+
check_arxiv_result = get_arxiv_live_search("What is Self Rewarding AI and how can it be used in Multi-Agent Systems?", arx_client, retrieve_results)
|
60 |
+
if len(check_arxiv_result) == 0:
|
61 |
+
is_arxiv_available = False
|
62 |
+
print("π΄ Arxiv search is taking a nap, switching to default search ...")
|
63 |
+
database_choices = [index_info]
|
64 |
+
|
65 |
+
# π Show examples - a teaser trailer for your brain! πΏπ§
|
66 |
+
sample_outputs = {
|
67 |
+
'output_placeholder': 'The LLM will provide an answer to your question here...',
|
68 |
+
'search_placeholder': '''
|
69 |
+
1. What is MoE?
|
70 |
+
2. What are Multi Agent Systems?
|
71 |
+
3. What is Self Rewarding AI?
|
72 |
+
4. What is Semantic and Episodic memory?
|
73 |
+
5. What is AutoGen?
|
74 |
+
6. What is ChatDev?
|
75 |
+
7. What is Omniverse?
|
76 |
+
8. What is Lumiere?
|
77 |
+
9. What is SORA?
|
78 |
+
'''
|
79 |
+
}
|
80 |
+
|
81 |
+
output_placeholder = sample_outputs['output_placeholder']
|
82 |
+
md_text_initial = sample_outputs['search_placeholder']
|
83 |
+
|
84 |
+
# π§Ή Clean up the RAG output - nobody likes a messy mind! π§Όπ§
|
85 |
+
def rag_cleaner(inp):
|
86 |
+
rank = inp['rank']
|
87 |
+
title = inp['document_metadata']['title']
|
88 |
+
content = inp['content']
|
89 |
+
date = inp['document_metadata']['_time']
|
90 |
+
return f"{rank}. <b> {title} </b> \n Date : {date} \n Abstract: {content}"
|
91 |
+
|
92 |
+
# π Craft the perfect prompt - it's showtime for the LLM! π¬
|
93 |
+
def get_prompt_text(question, context, formatted = True, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.2'):
|
94 |
+
if formatted:
|
95 |
+
sys_instruction = f"Context:\n {context} \n Given the following scientific paper abstracts, take a deep breath and let's think step by step to answer the question. Cite the titles of your sources when answering, do not cite links or dates."
|
96 |
+
message = f"Question: {question}"
|
97 |
+
|
98 |
+
if 'mistralai' in llm_model_picked:
|
99 |
+
return f"<s>" + f"[INST] {sys_instruction}" + f" {message}[/INST]"
|
100 |
+
elif 'gemma' in llm_model_picked:
|
101 |
+
return f"<bos><start_of_turn>user\n{sys_instruction}" + f" {message}<end_of_turn>\n"
|
102 |
+
|
103 |
+
return f"Context:\n {context} \n Given the following info, take a deep breath and let's think step by step to answer the question: {question}. Cite the titles of your sources when answering.\n\n"
|
104 |
+
|
105 |
+
# π΅οΈββοΈ Get those juicy references - time to go treasure hunting! ππ
|
106 |
+
def get_references(question, retriever, k = retrieve_results):
|
107 |
+
rag_out = retriever.search(query=question, k=k)
|
108 |
+
return rag_out
|
109 |
+
|
110 |
+
def get_rag(message):
|
111 |
+
return get_references(message, RAG)
|
112 |
+
|
113 |
+
# π€ Save the response and read it aloud - it's karaoke time for your brain! π§ πΆ
|
114 |
+
def SaveResponseAndRead(result):
|
115 |
+
documentHTML5='''
|
116 |
+
<!DOCTYPE html>
|
117 |
+
<html>
|
118 |
+
<head>
|
119 |
+
<title>Read It Aloud</title>
|
120 |
+
<script type="text/javascript">
|
121 |
+
function readAloud() {
|
122 |
+
const text = document.getElementById("textArea").value;
|
123 |
+
const speech = new SpeechSynthesisUtterance(text);
|
124 |
+
window.speechSynthesis.speak(speech);
|
125 |
+
}
|
126 |
+
</script>
|
127 |
+
</head>
|
128 |
+
<body>
|
129 |
+
<h1>π Read It Aloud</h1>
|
130 |
+
<textarea id="textArea" rows="10" cols="80">
|
131 |
+
'''
|
132 |
+
documentHTML5 = documentHTML5 + result
|
133 |
+
documentHTML5 = documentHTML5 + '''
|
134 |
+
</textarea>
|
135 |
+
<br>
|
136 |
+
<button onclick="readAloud()">π Read Aloud</button>
|
137 |
+
</body>
|
138 |
+
</html>
|
139 |
+
'''
|
140 |
+
gr.HTML(documentHTML5)
|
141 |
+
|
142 |
+
# π File management functions - because even AI needs a filing system! ποΈπ€
|
143 |
+
|
144 |
+
def save_response_as_markdown(question, response):
|
145 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M")
|
146 |
+
filename = f"{timestamp}_{question[:50]}.md" # Truncate question to 50 chars for filename
|
147 |
+
with open(filename, "w", encoding="utf-8") as f:
|
148 |
+
f.write(response)
|
149 |
+
return filename
|
150 |
+
|
151 |
+
def list_markdown_files():
|
152 |
+
files = glob.glob("*.md")
|
153 |
+
files.sort(key=os.path.getmtime, reverse=True)
|
154 |
+
return [f for f in files if f != "README.md"]
|
155 |
+
|
156 |
+
def delete_file(filename):
|
157 |
+
if filename != "README.md":
|
158 |
+
os.remove(filename)
|
159 |
+
return f"Deleted {filename}"
|
160 |
+
return "Cannot delete README.md"
|
161 |
+
|
162 |
+
def display_markdown_contents():
|
163 |
+
files = list_markdown_files()
|
164 |
+
output = ""
|
165 |
+
for file in files:
|
166 |
+
with open(file, "r", encoding="utf-8") as f:
|
167 |
+
content = f.read()
|
168 |
+
output += f"## {file}\n\n```markdown\n{content}\n```\n\n"
|
169 |
+
return output
|
170 |
+
|
171 |
+
# π¨ Building the UI - it's like LEGO, but for brains! π§ ποΈ
|
172 |
+
with gr.Blocks(theme = gr.themes.Soft()) as demo:
|
173 |
+
header = gr.Markdown(header_text)
|
174 |
+
|
175 |
+
with gr.Group():
|
176 |
+
msg = gr.Textbox(label = 'Search', placeholder = 'What is Generative AI in Healthcare?')
|
177 |
+
|
178 |
+
with gr.Accordion("Advanced Settings", open=False):
|
179 |
+
with gr.Row(equal_height = True):
|
180 |
+
llm_model = gr.Dropdown(choices = llm_models_to_choose, value = 'mistralai/Mistral-7B-Instruct-v0.2', label = 'LLM Model')
|
181 |
+
llm_results = gr.Slider(minimum=4, maximum=10, value=5, step=1, interactive=True, label="Top n results as context")
|
182 |
+
database_src = gr.Dropdown(choices = database_choices, value = index_info, label = 'Search Source')
|
183 |
+
stream_results = gr.Checkbox(value = True, label = "Stream output", visible = False)
|
184 |
+
|
185 |
+
output_text = gr.Textbox(show_label = True, container = True, label = 'LLM Answer', visible = True, placeholder = output_placeholder)
|
186 |
+
input = gr.Textbox(show_label = False, visible = False)
|
187 |
+
gr_md = gr.Markdown(mark_text + md_text_initial)
|
188 |
+
|
189 |
+
with gr.Tab("Saved Responses"):
|
190 |
+
refresh_button = gr.Button("π Refresh File List")
|
191 |
+
file_list = gr.Dropdown(choices=list_markdown_files(), label="Saved Responses")
|
192 |
+
delete_button = gr.Button("ποΈ Delete Selected File")
|
193 |
+
markdown_display = gr.Markdown()
|
194 |
+
|
195 |
+
# π Update the file list - keeping things fresh! πΏ
|
196 |
+
def update_file_list():
|
197 |
+
return gr.Dropdown(choices=list_markdown_files())
|
198 |
+
|
199 |
+
refresh_button.click(update_file_list, outputs=[file_list])
|
200 |
+
delete_button.click(delete_file, inputs=[file_list], outputs=[markdown_display]).then(update_file_list, outputs=[file_list])
|
201 |
+
file_list.change(lambda x: open(x, "r", encoding="utf-8").read() if x else "", inputs=[file_list], outputs=[markdown_display])
|
202 |
+
|
203 |
+
# π The grand finale - where the magic happens! π©β¨
|
204 |
+
def update_with_rag_md(message, llm_results_use = 5, database_choice = index_info, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.2'):
|
205 |
+
prompt_text_from_data = ""
|
206 |
+
database_to_use = database_choice
|
207 |
+
if database_choice == index_info:
|
208 |
+
rag_out = get_rag(message)
|
209 |
+
else:
|
210 |
+
arxiv_search_success = True
|
211 |
+
try:
|
212 |
+
rag_out = get_arxiv_live_search(message, arx_client, retrieve_results)
|
213 |
+
if len(rag_out) == 0:
|
214 |
+
arxiv_search_success = False
|
215 |
+
except:
|
216 |
+
arxiv_search_success = False
|
217 |
+
|
218 |
+
if not arxiv_search_success:
|
219 |
+
gr.Warning("π΄ Arxiv Search is taking a siesta, switching to semantic search ...")
|
220 |
+
rag_out = get_rag(message)
|
221 |
+
database_to_use = index_info
|
222 |
+
|
223 |
+
md_text_updated = mark_text
|
224 |
+
for i in range(retrieve_results):
|
225 |
+
rag_answer = rag_out[i]
|
226 |
+
if i < llm_results_use:
|
227 |
+
md_text_paper, prompt_text = get_md_text_abstract(rag_answer, source = database_to_use, return_prompt_formatting = True)
|
228 |
+
prompt_text_from_data += f"{i+1}. {prompt_text}"
|
229 |
+
else:
|
230 |
+
md_text_paper = get_md_text_abstract(rag_answer, source = database_to_use)
|
231 |
+
md_text_updated += md_text_paper
|
232 |
+
prompt = get_prompt_text(message, prompt_text_from_data, llm_model_picked = llm_model_picked)
|
233 |
+
return md_text_updated, prompt
|
234 |
+
|
235 |
+
# π§ Asking the LLM - it's like a really smart magic 8-ball! π±β¨
|
236 |
+
def ask_llm(prompt, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.2', stream_outputs = False):
|
237 |
+
model_disabled_text = "LLM Model is taking a vacation. Try again later! ποΈ"
|
238 |
+
output = ""
|
239 |
+
|
240 |
+
if llm_model_picked == 'None':
|
241 |
+
if stream_outputs:
|
242 |
+
for out in model_disabled_text:
|
243 |
+
output += out
|
244 |
+
yield output
|
245 |
+
return output
|
246 |
+
else:
|
247 |
+
return model_disabled_text
|
248 |
+
|
249 |
+
client = InferenceClient(llm_model_picked)
|
250 |
+
try:
|
251 |
+
stream = client.text_generation(prompt, **generate_kwargs, stream=stream_outputs, details=False, return_full_text=False)
|
252 |
+
|
253 |
+
except:
|
254 |
+
gr.Warning("π¦ LLM Inference hit a traffic jam! Take a breather and try again later.")
|
255 |
+
return ""
|
256 |
+
|
257 |
+
if stream_outputs:
|
258 |
+
for response in stream:
|
259 |
+
output += response
|
260 |
+
SaveResponseAndRead(response)
|
261 |
+
yield output
|
262 |
+
return output
|
263 |
+
else:
|
264 |
+
return stream
|
265 |
+
|
266 |
+
# π¬ Action! Process the query and save the response
|
267 |
+
def process_and_save(message, llm_results_use, database_choice, llm_model_picked):
|
268 |
+
md_text_updated, prompt = update_with_rag_md(message, llm_results_use, database_choice, llm_model_picked)
|
269 |
+
llm_response = ask_llm(prompt, llm_model_picked, stream_outputs=False)
|
270 |
+
full_response = f"Question: {message}\n\nResponse:\n{llm_response}\n\nReferences:\n{md_text_updated}"
|
271 |
+
filename = save_response_as_markdown(message, full_response)
|
272 |
+
return md_text_updated, prompt, llm_response, filename
|
273 |
+
|
274 |
+
# π¬ Lights, camera, action! Let's get this show on the road! π
|
275 |
+
msg.submit(process_and_save, [msg, llm_results, database_src, llm_model], [gr_md, input, output_text, file_list]).then(update_file_list, outputs=[file_list])
|
276 |
+
|
277 |
+
# π Launch the app - let the knowledge party begin! ππ§
|
278 |
+
demo.queue().launch()
|