Spaces:
Runtime error
Runtime error
jharrison27
commited on
Commit
•
3701fee
1
Parent(s):
de875e1
initial commit
Browse files- app.py +166 -0
- helper.py +132 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
10 |
+
import arxiv
|
11 |
+
from helper import *
|
12 |
+
|
13 |
+
retrieve_results = 20
|
14 |
+
show_examples = True
|
15 |
+
llm_models_to_choose = ['mistralai/Mixtral-8x7B-Instruct-v0.1','mistralai/Mistral-7B-Instruct-v0.2', 'google/gemma-7b-it', 'None']
|
16 |
+
|
17 |
+
generate_kwargs = dict(
|
18 |
+
temperature = None,
|
19 |
+
max_new_tokens = 512,
|
20 |
+
top_p = None,
|
21 |
+
do_sample = False,
|
22 |
+
)
|
23 |
+
|
24 |
+
## RAG Model
|
25 |
+
RAG = RAGPretrainedModel.from_index("colbert/indexes/arxiv_colbert")
|
26 |
+
|
27 |
+
try:
|
28 |
+
gr.Info("Setting up retriever, please wait...")
|
29 |
+
rag_initial_output = RAG.search("What is Generative AI in Healthcare?", k = 1)
|
30 |
+
gr.Info("Retriever working successfully!")
|
31 |
+
|
32 |
+
except:
|
33 |
+
gr.Warning("Retriever not working!")
|
34 |
+
|
35 |
+
## Header
|
36 |
+
mark_text = '# 🩺🔍 Search Results\n'
|
37 |
+
header_text = "## Arxiv Paper Summary With QA Retrieval Augmented Generation \n"
|
38 |
+
|
39 |
+
try:
|
40 |
+
with open("README.md", "r") as f:
|
41 |
+
mdfile = f.read()
|
42 |
+
date_pattern = r'Index Last Updated : \d{4}-\d{2}-\d{2}'
|
43 |
+
match = re.search(date_pattern, mdfile)
|
44 |
+
date = match.group().split(': ')[1]
|
45 |
+
formatted_date = datetime.strptime(date, '%Y-%m-%d').strftime('%d %b %Y')
|
46 |
+
header_text += f'Index Last Updated: {formatted_date}\n'
|
47 |
+
index_info = f"Semantic Search - up to {formatted_date}"
|
48 |
+
except:
|
49 |
+
index_info = "Semantic Search"
|
50 |
+
|
51 |
+
database_choices = [index_info,'Arxiv Search - Latest - (EXPERIMENTAL)']
|
52 |
+
|
53 |
+
## Arxiv API
|
54 |
+
arx_client = arxiv.Client()
|
55 |
+
is_arxiv_available = True
|
56 |
+
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)
|
57 |
+
if len(check_arxiv_result) == 0:
|
58 |
+
is_arxiv_available = False
|
59 |
+
print("Arxiv search not working, switching to default search ...")
|
60 |
+
database_choices = [index_info]
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
## Show examples
|
65 |
+
sample_outputs = {
|
66 |
+
'output_placeholder': 'The LLM will provide an answer to your question here...',
|
67 |
+
'search_placeholder': '''
|
68 |
+
1. What is MoE?
|
69 |
+
2. What are Multi Agent Systems?
|
70 |
+
3. What is Self Rewarding AI?
|
71 |
+
4. What is Semantic and Episodic memory?
|
72 |
+
5. What is AutoGen?
|
73 |
+
6. What is ChatDev?
|
74 |
+
7. What is Omniverse?
|
75 |
+
8. What is Lumiere?
|
76 |
+
9. What is SORA?
|
77 |
+
'''
|
78 |
+
}
|
79 |
+
|
80 |
+
output_placeholder = sample_outputs['output_placeholder']
|
81 |
+
md_text_initial = sample_outputs['search_placeholder']
|
82 |
+
|
83 |
+
|
84 |
+
with gr.Blocks(theme = gr.themes.Soft()) as demo:
|
85 |
+
header = gr.Markdown(header_text)
|
86 |
+
|
87 |
+
with gr.Group():
|
88 |
+
msg = gr.Textbox(label = 'Search', placeholder = 'What is Generative AI in Healthcare?')
|
89 |
+
|
90 |
+
with gr.Accordion("Advanced Settings", open=False):
|
91 |
+
with gr.Row(equal_height = True):
|
92 |
+
llm_model = gr.Dropdown(choices = llm_models_to_choose, value = 'mistralai/Mistral-7B-Instruct-v0.2', label = 'LLM Model')
|
93 |
+
llm_results = gr.Slider(minimum=4, maximum=10, value=5, step=1, interactive=True, label="Top n results as context")
|
94 |
+
database_src = gr.Dropdown(choices = database_choices, value = index_info, label = 'Search Source')
|
95 |
+
stream_results = gr.Checkbox(value = True, label = "Stream output", visible = False)
|
96 |
+
|
97 |
+
output_text = gr.Textbox(show_label = True, container = True, label = 'LLM Answer', visible = True, placeholder = output_placeholder)
|
98 |
+
input = gr.Textbox(show_label = False, visible = False)
|
99 |
+
gr_md = gr.Markdown(mark_text + md_text_initial)
|
100 |
+
|
101 |
+
def update_with_rag_md(message, llm_results_use = 5, database_choice = index_info, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.2'):
|
102 |
+
prompt_text_from_data = ""
|
103 |
+
database_to_use = database_choice
|
104 |
+
if database_choice == index_info:
|
105 |
+
rag_out = get_rag(message)
|
106 |
+
else:
|
107 |
+
arxiv_search_success = True
|
108 |
+
try:
|
109 |
+
rag_out = get_arxiv_live_search(message, arx_client, retrieve_results)
|
110 |
+
if len(rag_out) == 0:
|
111 |
+
arxiv_search_success = False
|
112 |
+
except:
|
113 |
+
arxiv_search_success = False
|
114 |
+
|
115 |
+
|
116 |
+
if not arxiv_search_success:
|
117 |
+
gr.Warning("Arxiv Search not working, switching to semantic search ...")
|
118 |
+
rag_out = get_rag(message)
|
119 |
+
database_to_use = index_info
|
120 |
+
|
121 |
+
md_text_updated = mark_text
|
122 |
+
for i in range(retrieve_results):
|
123 |
+
rag_answer = rag_out[i]
|
124 |
+
if i < llm_results_use:
|
125 |
+
md_text_paper, prompt_text = get_md_text_abstract(rag_answer, source = database_to_use, return_prompt_formatting = True)
|
126 |
+
prompt_text_from_data += f"{i+1}. {prompt_text}"
|
127 |
+
else:
|
128 |
+
md_text_paper = get_md_text_abstract(rag_answer, source = database_to_use)
|
129 |
+
md_text_updated += md_text_paper
|
130 |
+
prompt = get_prompt_text(message, prompt_text_from_data, llm_model_picked = llm_model_picked)
|
131 |
+
return md_text_updated, prompt
|
132 |
+
|
133 |
+
def ask_llm(prompt, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.2', stream_outputs = False):
|
134 |
+
model_disabled_text = "LLM Model is disabled"
|
135 |
+
output = ""
|
136 |
+
|
137 |
+
if llm_model_picked == 'None':
|
138 |
+
if stream_outputs:
|
139 |
+
for out in model_disabled_text:
|
140 |
+
output += out
|
141 |
+
yield output
|
142 |
+
return output
|
143 |
+
else:
|
144 |
+
return model_disabled_text
|
145 |
+
|
146 |
+
client = InferenceClient(llm_model_picked)
|
147 |
+
try:
|
148 |
+
stream = client.text_generation(prompt, **generate_kwargs, stream=stream_outputs, details=False, return_full_text=False)
|
149 |
+
|
150 |
+
except:
|
151 |
+
gr.Warning("LLM Inference rate limit reached, try again later!")
|
152 |
+
return ""
|
153 |
+
|
154 |
+
if stream_outputs:
|
155 |
+
for response in stream:
|
156 |
+
output += response
|
157 |
+
SaveResponseAndRead(response)
|
158 |
+
yield output
|
159 |
+
return output
|
160 |
+
else:
|
161 |
+
return stream
|
162 |
+
|
163 |
+
|
164 |
+
msg.submit(update_with_rag_md, [msg, llm_results, database_src, llm_model], [gr_md, input]).success(ask_llm, [input, llm_model, stream_results], output_text)
|
165 |
+
|
166 |
+
demo.queue().launch()
|
helper.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datetime
|
2 |
+
import string
|
3 |
+
import nltk
|
4 |
+
nltk.download('stopwords')
|
5 |
+
from nltk.corpus import stopwords
|
6 |
+
stop_words = stopwords.words('english')
|
7 |
+
import arxiv
|
8 |
+
import gradio as gr
|
9 |
+
import re
|
10 |
+
from datetime import datetime
|
11 |
+
import json
|
12 |
+
|
13 |
+
|
14 |
+
def rag_cleaner(inp):
|
15 |
+
rank = inp['rank']
|
16 |
+
title = inp['document_metadata']['title']
|
17 |
+
content = inp['content']
|
18 |
+
date = inp['document_metadata']['_time']
|
19 |
+
return f"{rank}. <b> {title} </b> \n Date : {date} \n Abstract: {content}"
|
20 |
+
|
21 |
+
def get_prompt_text(question, context, formatted = True, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.2'):
|
22 |
+
if formatted:
|
23 |
+
sys_instruction = f"Context:\n {context} \n Given the following scientific paper abstracts, take a deep breath and lets think step by step to answer the question. Cite the titles of your sources when answering, do not cite links or dates."
|
24 |
+
message = f"Question: {question}"
|
25 |
+
|
26 |
+
if 'mistralai' in llm_model_picked:
|
27 |
+
return f"<s>" + f"[INST] {sys_instruction}" + f" {message}[/INST]"
|
28 |
+
|
29 |
+
elif 'gemma' in llm_model_picked:
|
30 |
+
return f"<bos><start_of_turn>user\n{sys_instruction}" + f" {message}<end_of_turn>\n"
|
31 |
+
|
32 |
+
return f"Context:\n {context} \n Given the following info, take a deep breath and lets think step by step to answer the question: {question}. Cite the titles of your sources when answering.\n\n"
|
33 |
+
|
34 |
+
def get_references(question, retriever, k = retrieve_results):
|
35 |
+
rag_out = retriever.search(query=question, k=k)
|
36 |
+
return rag_out
|
37 |
+
|
38 |
+
def get_rag(message):
|
39 |
+
return get_references(message, RAG)
|
40 |
+
|
41 |
+
def SaveResponseAndRead(result):
|
42 |
+
documentHTML5='''
|
43 |
+
<!DOCTYPE html>
|
44 |
+
<html>
|
45 |
+
<head>
|
46 |
+
<title>Read It Aloud</title>
|
47 |
+
<script type="text/javascript">
|
48 |
+
function readAloud() {
|
49 |
+
const text = document.getElementById("textArea").value;
|
50 |
+
const speech = new SpeechSynthesisUtterance(text);
|
51 |
+
window.speechSynthesis.speak(speech);
|
52 |
+
}
|
53 |
+
</script>
|
54 |
+
</head>
|
55 |
+
<body>
|
56 |
+
<h1>🔊 Read It Aloud</h1>
|
57 |
+
<textarea id="textArea" rows="10" cols="80">
|
58 |
+
'''
|
59 |
+
documentHTML5 = documentHTML5 + result
|
60 |
+
documentHTML5 = documentHTML5 + '''
|
61 |
+
</textarea>
|
62 |
+
<br>
|
63 |
+
<button onclick="readAloud()">🔊 Read Aloud</button>
|
64 |
+
</body>
|
65 |
+
</html>
|
66 |
+
'''
|
67 |
+
gr.HTML(documentHTML5)
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
def get_md_text_abstract(rag_answer, source = ['Arxiv Search', 'Semantic Search'][1], return_prompt_formatting = False):
|
73 |
+
if 'Semantic Search' in source:
|
74 |
+
title = rag_answer['document_metadata']['title'].replace('\n','')
|
75 |
+
#score = round(rag_answer['score'], 2)
|
76 |
+
date = rag_answer['document_metadata']['_time']
|
77 |
+
paper_abs = rag_answer['content']
|
78 |
+
authors = rag_answer['document_metadata']['authors'].replace('\n','')
|
79 |
+
doc_id = rag_answer['document_id']
|
80 |
+
paper_link = f'''https://arxiv.org/abs/{doc_id}'''
|
81 |
+
download_link = f'''https://arxiv.org/pdf/{doc_id}'''
|
82 |
+
|
83 |
+
elif 'Arxiv' in source:
|
84 |
+
title = rag_answer.title
|
85 |
+
date = rag_answer.updated.strftime('%d %b %Y')
|
86 |
+
paper_abs = rag_answer.summary.replace('\n',' ') + '\n'
|
87 |
+
authors = ', '.join([author.name for author in rag_answer.authors])
|
88 |
+
paper_link = rag_answer.links[0].href
|
89 |
+
download_link = rag_answer.links[1].href
|
90 |
+
|
91 |
+
else:
|
92 |
+
raise Exception
|
93 |
+
|
94 |
+
paper_title = f'''### {date} | [{title}]({paper_link}) | [⬇️]({download_link})\n'''
|
95 |
+
authors_formatted = f'*{authors}*' + ' \n\n'
|
96 |
+
|
97 |
+
md_text_formatted = paper_title + authors_formatted + paper_abs + '\n---------------\n'+ '\n'
|
98 |
+
if return_prompt_formatting:
|
99 |
+
prompt_formatted = f"<b> {title} </b> \n Abstract: {paper_abs}"
|
100 |
+
return md_text_formatted, prompt_formatted
|
101 |
+
|
102 |
+
return md_text_formatted
|
103 |
+
|
104 |
+
def remove_punctuation(text):
|
105 |
+
punct_str = string.punctuation
|
106 |
+
punct_str = punct_str.replace("'", "")
|
107 |
+
return text.translate(str.maketrans("", "", punct_str))
|
108 |
+
|
109 |
+
def remove_stopwords(text):
|
110 |
+
text = ' '.join(word for word in text.split(' ') if word not in stop_words)
|
111 |
+
return text
|
112 |
+
|
113 |
+
def search_cleaner(text):
|
114 |
+
new_text = text.lower()
|
115 |
+
new_text = remove_stopwords(new_text)
|
116 |
+
new_text = remove_punctuation(new_text)
|
117 |
+
return new_text
|
118 |
+
|
119 |
+
|
120 |
+
q = '(cat:cs.CV OR cat:cs.LG OR cat:cs.CL OR cat:cs.AI OR cat:cs.NE OR cat:cs.RO)'
|
121 |
+
|
122 |
+
|
123 |
+
def get_arxiv_live_search(query, client, max_results = 10):
|
124 |
+
clean_text = search_cleaner(query)
|
125 |
+
search = arxiv.Search(
|
126 |
+
query = clean_text + " AND "+q,
|
127 |
+
max_results = max_results,
|
128 |
+
sort_by = arxiv.SortCriterion.Relevance
|
129 |
+
)
|
130 |
+
results = client.results(search)
|
131 |
+
all_results = list(results)
|
132 |
+
return all_results
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pydantic
|
2 |
+
ragatouille
|
3 |
+
huggingface_hub
|
4 |
+
arxiv
|