awacke1 commited on
Commit
a85bffe
โ€ข
1 Parent(s): 75d3fb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +195 -0
app.py CHANGED
@@ -184,6 +184,190 @@ def save_to_cosmos_db(container, query, response1, response2):
184
  except Exception as e:
185
  st.error(f"An unexpected error occurred: {str(e)}")
186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  # GitHub functions
188
  def download_github_repo(url, local_path):
189
  if os.path.exists(local_path):
@@ -242,6 +426,17 @@ def main():
242
  if 'cloned_doc' not in st.session_state:
243
  st.session_state.cloned_doc = None
244
 
 
 
 
 
 
 
 
 
 
 
 
245
  # Automatic Login
246
  if Key:
247
  st.session_state.primary_key = Key
 
184
  except Exception as e:
185
  st.error(f"An unexpected error occurred: {str(e)}")
186
 
187
+
188
+
189
+ # Add dropdowns for model and database choices
190
+ def search_glossary(query):
191
+ st.markdown(f"### ๐Ÿ” Search Glossary for: `{query}`")
192
+
193
+ # Dropdown for model selection
194
+ model_options = ['mistralai/Mixtral-8x7B-Instruct-v0.1', 'mistralai/Mistral-7B-Instruct-v0.2', 'google/gemma-7b-it', 'None']
195
+ model_choice = st.selectbox('๐Ÿง  Select LLM Model', options=model_options, index=1)
196
+
197
+ # Dropdown for database selection
198
+ database_options = ['Semantic Search', 'Arxiv Search - Latest - (EXPERIMENTAL)']
199
+ database_choice = st.selectbox('๐Ÿ“š Select Database', options=database_options, index=0)
200
+
201
+
202
+
203
+ # Run Button with Emoji
204
+ #if st.button("๐Ÿš€ Run"):
205
+
206
+ # ๐Ÿ•ต๏ธโ€โ™‚๏ธ Searching the glossary for: query
207
+ all_results = ""
208
+ st.markdown(f"- {query}")
209
+
210
+ # ๐Ÿ” ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM
211
+ #database_choice Literal['Semantic Search', 'Arxiv Search - Latest - (EXPERIMENTAL)'] Default: "Semantic Search"
212
+ #llm_model_picked Literal['mistralai/Mixtral-8x7B-Instruct-v0.1', 'mistralai/Mistral-7B-Instruct-v0.2', 'google/gemma-7b-it', 'None'] Default: "mistralai/Mistral-7B-Instruct-v0.2"
213
+ client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
214
+
215
+
216
+ # ๐Ÿ” ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM - api_name: /ask_llm
217
+ result = client.predict(
218
+ prompt=query,
219
+ llm_model_picked="mistralai/Mixtral-8x7B-Instruct-v0.1",
220
+ stream_outputs=True,
221
+ api_name="/ask_llm"
222
+ )
223
+ st.markdown(result)
224
+ st.code(result, language="python", line_numbers=True)
225
+
226
+ # ๐Ÿ” ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM - api_name: /ask_llm
227
+ result2 = client.predict(
228
+ prompt=query,
229
+ llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
230
+ stream_outputs=True,
231
+ api_name="/ask_llm"
232
+ )
233
+ st.markdown(result2)
234
+ st.code(result2, language="python", line_numbers=True)
235
+
236
+ # ๐Ÿ” ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM - api_name: /ask_llm
237
+ result3 = client.predict(
238
+ prompt=query,
239
+ llm_model_picked="google/gemma-7b-it",
240
+ stream_outputs=True,
241
+ api_name="/ask_llm"
242
+ )
243
+ st.markdown(result3)
244
+ st.code(result3, language="python", line_numbers=True)
245
+
246
+
247
+ # ๐Ÿ” ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM - api_name: /update_with_rag_md
248
+ response2 = client.predict(
249
+ message=query, # str in 'parameter_13' Textbox component
250
+ llm_results_use=10,
251
+ database_choice="Semantic Search",
252
+ llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
253
+ api_name="/update_with_rag_md"
254
+ ) # update_with_rag_md Returns tuple of 2 elements [0] str The output value that appears in the "value_14" Markdown component. [1] str
255
+
256
+ st.markdown(response2[0])
257
+ st.code(response2[0], language="python", line_numbers=True, wrap_lines=True)
258
+
259
+ st.markdown(response2[1])
260
+ st.code(response2[1], language="python", line_numbers=True, wrap_lines=True)
261
+
262
+ # When saving results, pass the container
263
+ try:
264
+ save_to_cosmos_db(st.session_state.cosmos_container, query, result, result)
265
+ save_to_cosmos_db(st.session_state.cosmos_container, query, result2, result2)
266
+ save_to_cosmos_db(st.session_state.cosmos_container, query, result3, result3)
267
+ save_to_cosmos_db(st.session_state.cosmos_container, query, response2[0], response2[0])
268
+ save_to_cosmos_db(st.session_state.cosmos_container, query, response2[1], response2[1])
269
+ except exceptions.CosmosHttpResponseError as e:
270
+ return False, f"HTTP error occurred: {str(e)} ๐Ÿšจ"
271
+ except Exception as e:
272
+ return False, f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ"
273
+
274
+
275
+ try:
276
+ # Aggregate hyperlinks and show with emojis
277
+ hyperlinks = extract_hyperlinks([response1, response2])
278
+ st.markdown("### ๐Ÿ”— Aggregated Hyperlinks")
279
+ for link in hyperlinks:
280
+ st.markdown(f"๐Ÿ”— [{link}]({link})")
281
+
282
+ # Show responses in a code format with line numbers
283
+ st.markdown("### ๐Ÿ“œ Response Outputs with Line Numbers")
284
+ st.code(f"Response 1: \n{format_with_line_numbers(response1)}\n\nResponse 2: \n{format_with_line_numbers(response2)}", language="json")
285
+ except exceptions.CosmosHttpResponseError as e:
286
+ return False, f"HTTP error occurred: {str(e)} ๐Ÿšจ"
287
+ except Exception as e:
288
+ return False, f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ"
289
+
290
+
291
+
292
+ # ๐ŸŽค Function to process text input
293
+ def process_text(text_input):
294
+ # ๐ŸŽค Processing text inputโ€”translating human words into cosmic signals! ๐Ÿ“ก
295
+ if text_input:
296
+ if 'messages' not in st.session_state:
297
+ st.session_state.messages = []
298
+
299
+ st.session_state.messages.append({"role": "user", "content": text_input})
300
+
301
+ with st.chat_message("user"):
302
+ st.markdown(text_input)
303
+
304
+ with st.chat_message("assistant"):
305
+ search_glossary(text_input)
306
+
307
+ # ๐Ÿ“ Function to generate a filename
308
+ def generate_filename(text, file_type):
309
+ # ๐Ÿ“ Generate a filename based on the text input
310
+ safe_text = "".join(c if c.isalnum() or c in (' ', '.', '_') else '_' for c in text)
311
+ safe_text = "_".join(safe_text.strip().split())
312
+ filename = f"{safe_text}.{file_type}"
313
+ return filename
314
+
315
+ # ๐Ÿ•ต๏ธโ€โ™€๏ธ Function to extract markdown title
316
+ def extract_markdown_title(content):
317
+ # ๐Ÿ•ต๏ธโ€โ™€๏ธ Extracting markdown titleโ€”finding the headline in the cosmic news! ๐Ÿ“ฐ
318
+ lines = content.splitlines()
319
+ for line in lines:
320
+ if line.startswith('#'):
321
+ return line.lstrip('#').strip()
322
+ return None
323
+
324
+ # ๐Ÿ’พ Function to create and save a file
325
+ def create_and_save_file(content, file_type="md", prompt=None, is_image=False, should_save=True):
326
+ # ๐Ÿ’พ Creating and saving a fileโ€”capturing cosmic wisdom! ๐Ÿ“
327
+ if not should_save:
328
+ return None
329
+
330
+ # Step 1: Generate filename based on the prompt or content
331
+ filename = generate_filename(prompt if prompt else content, file_type)
332
+
333
+ # Step 2: If it's a markdown file, check if it has a title
334
+ if file_type == "md":
335
+ title_from_content = extract_markdown_title(content)
336
+ if title_from_content:
337
+ filename = generate_filename(title_from_content, file_type)
338
+
339
+ # Step 3: Save the file
340
+ with open(filename, "w", encoding="utf-8") as f:
341
+ if is_image:
342
+ f.write(content)
343
+ else:
344
+ f.write(prompt + "\n\n" + content)
345
+
346
+ return filename
347
+
348
+ # ๐Ÿค– Function to insert an auto-generated record
349
+ def insert_auto_generated_record(container):
350
+ # ๐Ÿค– Automatically generating a record and inserting it into Cosmos DB!
351
+ try:
352
+ # Generate a unique id
353
+ new_id = generate_unique_id()
354
+ # Create a sample JSON document
355
+ new_doc = {
356
+ 'id': new_id,
357
+ 'name': f'Sample Name {new_id[:8]}',
358
+ 'description': 'This is a sample auto-generated description.',
359
+ 'timestamp': datetime.utcnow().isoformat()
360
+ }
361
+ # Insert the document
362
+ container.create_item(body=new_doc)
363
+ return True, f"Record inserted successfully with id: {new_id} ๐ŸŽ‰"
364
+ except exceptions.CosmosHttpResponseError as e:
365
+ return False, f"HTTP error occurred: {str(e)} ๐Ÿšจ"
366
+ except Exception as e:
367
+ return False, f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ"
368
+
369
+
370
+
371
  # GitHub functions
372
  def download_github_repo(url, local_path):
373
  if os.path.exists(local_path):
 
426
  if 'cloned_doc' not in st.session_state:
427
  st.session_state.cloned_doc = None
428
 
429
+ # โš™๏ธ q= Run ArXiv search from query parameters
430
+ try:
431
+ query_params = st.query_params
432
+ query = query_params.get('q') or query_params.get('query') or ''
433
+ if query:
434
+ # ๐Ÿ•ต๏ธโ€โ™‚๏ธ We have a query! Let's process it!
435
+ process_text(query)
436
+ st.stop() # Stop further execution
437
+ except Exception as e:
438
+ st.markdown(' ')
439
+
440
  # Automatic Login
441
  if Key:
442
  st.session_state.primary_key = Key