awacke1 commited on
Commit
d4ade34
·
verified ·
1 Parent(s): 5886fb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -41
app.py CHANGED
@@ -6,13 +6,15 @@ from huggingface_hub import InferenceClient
6
  import re
7
  from datetime import datetime
8
  import json
9
-
10
  import arxiv
11
  from utils import get_md_text_abstract, search_cleaner, get_arxiv_live_search
12
 
13
  retrieve_results = 10
14
  show_examples = False
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,
@@ -33,8 +35,8 @@ 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:
@@ -53,7 +55,7 @@ database_choices = [index_info,'Arxiv Search - Latest - (EXPERIMENTAL)']
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 ...")
@@ -80,7 +82,7 @@ def rag_cleaner(inp):
80
  date = inp['document_metadata']['_time']
81
  return f"{rank}. <b> {title} </b> \n Date : {date} \n Abstract: {content}"
82
 
83
- def get_prompt_text(question, context, formatted = True, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.2'):
84
  if formatted:
85
  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."
86
  message = f"Question: {question}"
@@ -100,35 +102,6 @@ def get_references(question, retriever, k = retrieve_results):
100
  def get_rag(message):
101
  return get_references(message, RAG)
102
 
103
- def SaveResponseAndRead(result):
104
- documentHTML5='''
105
- <!DOCTYPE html>
106
- <html>
107
- <head>
108
- <title>Read It Aloud</title>
109
- <script type="text/javascript">
110
- function readAloud() {
111
- const text = document.getElementById("textArea").value;
112
- const speech = new SpeechSynthesisUtterance(text);
113
- window.speechSynthesis.speak(speech);
114
- }
115
- </script>
116
- </head>
117
- <body>
118
- <h1>🔊 Read It Aloud</h1>
119
- <textarea id="textArea" rows="10" cols="80">
120
- '''
121
- documentHTML5 = documentHTML5 + result
122
- documentHTML5 = documentHTML5 + '''
123
- </textarea>
124
- <br>
125
- <button onclick="readAloud()">🔊 Read Aloud</button>
126
- </body>
127
- </html>
128
- '''
129
- gr.HTML(documentHTML5)
130
-
131
-
132
  with gr.Blocks(theme = gr.themes.Soft()) as demo:
133
  header = gr.Markdown(header_text)
134
 
@@ -137,7 +110,7 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
137
 
138
  with gr.Accordion("Advanced Settings", open=False):
139
  with gr.Row(equal_height = True):
140
- llm_model = gr.Dropdown(choices = llm_models_to_choose, value = 'mistralai/Mistral-7B-Instruct-v0.2', label = 'LLM Model')
141
  llm_results = gr.Slider(minimum=4, maximum=10, value=5, step=1, interactive=True, label="Top n results as context")
142
  database_src = gr.Dropdown(choices = database_choices, value = index_info, label = 'Search Source')
143
  stream_results = gr.Checkbox(value = True, label = "Stream output", visible = False)
@@ -146,7 +119,7 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
146
  input = gr.Textbox(show_label = False, visible = False)
147
  gr_md = gr.Markdown(mark_text + md_text_initial)
148
 
149
- def update_with_rag_md(message, llm_results_use = 5, database_choice = index_info, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.2'):
150
  prompt_text_from_data = ""
151
  database_to_use = database_choice
152
  if database_choice == index_info:
@@ -178,7 +151,7 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
178
  prompt = get_prompt_text(message, prompt_text_from_data, llm_model_picked = llm_model_picked)
179
  return md_text_updated, prompt
180
 
181
- def ask_llm(prompt, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.2', stream_outputs = False):
182
  model_disabled_text = "LLM Model is disabled"
183
  output = ""
184
 
@@ -191,7 +164,7 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
191
  else:
192
  return model_disabled_text
193
 
194
- client = InferenceClient(llm_model_picked)
195
  try:
196
  stream = client.text_generation(prompt, **generate_kwargs, stream=stream_outputs, details=False, return_full_text=False)
197
 
@@ -202,7 +175,6 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
202
  if stream_outputs:
203
  for response in stream:
204
  output += response
205
- SaveResponseAndRead(response)
206
  yield output
207
  return output
208
  else:
@@ -211,4 +183,4 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
211
 
212
  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)
213
 
214
- demo.queue().launch()
 
6
  import re
7
  from datetime import datetime
8
  import json
9
+ import os
10
  import arxiv
11
  from utils import get_md_text_abstract, search_cleaner, get_arxiv_live_search
12
 
13
  retrieve_results = 10
14
  show_examples = False
15
+ llm_models_to_choose = ['mistralai/Mixtral-8x7B-Instruct-v0.1','mistralai/Mistral-7B-Instruct-v0.3', 'google/gemma-2-2b-it', 'None']
16
+
17
+ token = os.getenv("HF_TOKEN")
18
 
19
  generate_kwargs = dict(
20
  temperature = None,
 
35
  gr.Warning("Retriever not working!")
36
 
37
  ## Header
38
+ mark_text = '# 🔍 Search Results\n'
39
+ header_text = "# ArXiv CS RAG \n"
40
 
41
  try:
42
  with open("README.md", "r") as f:
 
55
  ## Arxiv API
56
  arx_client = arxiv.Client()
57
  is_arxiv_available = True
58
+ check_arxiv_result = get_arxiv_live_search("What is Mistral?", arx_client, retrieve_results)
59
  if len(check_arxiv_result) == 0:
60
  is_arxiv_available = False
61
  print("Arxiv search not working, switching to default search ...")
 
82
  date = inp['document_metadata']['_time']
83
  return f"{rank}. <b> {title} </b> \n Date : {date} \n Abstract: {content}"
84
 
85
+ def get_prompt_text(question, context, formatted = True, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.3'):
86
  if formatted:
87
  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."
88
  message = f"Question: {question}"
 
102
  def get_rag(message):
103
  return get_references(message, RAG)
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  with gr.Blocks(theme = gr.themes.Soft()) as demo:
106
  header = gr.Markdown(header_text)
107
 
 
110
 
111
  with gr.Accordion("Advanced Settings", open=False):
112
  with gr.Row(equal_height = True):
113
+ llm_model = gr.Dropdown(choices = llm_models_to_choose, value = 'mistralai/Mistral-7B-Instruct-v0.3', label = 'LLM Model')
114
  llm_results = gr.Slider(minimum=4, maximum=10, value=5, step=1, interactive=True, label="Top n results as context")
115
  database_src = gr.Dropdown(choices = database_choices, value = index_info, label = 'Search Source')
116
  stream_results = gr.Checkbox(value = True, label = "Stream output", visible = False)
 
119
  input = gr.Textbox(show_label = False, visible = False)
120
  gr_md = gr.Markdown(mark_text + md_text_initial)
121
 
122
+ def update_with_rag_md(message, llm_results_use = 5, database_choice = index_info, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.3'):
123
  prompt_text_from_data = ""
124
  database_to_use = database_choice
125
  if database_choice == index_info:
 
151
  prompt = get_prompt_text(message, prompt_text_from_data, llm_model_picked = llm_model_picked)
152
  return md_text_updated, prompt
153
 
154
+ def ask_llm(prompt, llm_model_picked = 'mistralai/Mistral-7B-Instruct-v0.3', stream_outputs = False):
155
  model_disabled_text = "LLM Model is disabled"
156
  output = ""
157
 
 
164
  else:
165
  return model_disabled_text
166
 
167
+ client = InferenceClient(llm_model_picked, token = token)
168
  try:
169
  stream = client.text_generation(prompt, **generate_kwargs, stream=stream_outputs, details=False, return_full_text=False)
170
 
 
175
  if stream_outputs:
176
  for response in stream:
177
  output += response
 
178
  yield output
179
  return output
180
  else:
 
183
 
184
  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)
185
 
186
+ demo.queue().launch()