awacke1 commited on
Commit
eeeb231
Β·
verified Β·
1 Parent(s): 0fc9456

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -100
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  import anthropic
3
- import openai
4
  import base64
5
  from datetime import datetime
6
  import plotly.graph_objects as go
@@ -87,6 +87,8 @@ if "openai_model" not in st.session_state:
87
  st.session_state["openai_model"] = "gpt-4o-2024-05-13"
88
  if "messages" not in st.session_state:
89
  st.session_state.messages = []
 
 
90
 
91
  # Custom CSS
92
  st.markdown("""
@@ -180,7 +182,12 @@ bike_collections = {
180
  }
181
  }
182
 
183
- # Helper Functions
 
 
 
 
 
184
  def generate_filename(prompt, file_type):
185
  """Generate a safe filename using the prompt and file type."""
186
  central = pytz.timezone('US/Central')
@@ -208,6 +215,39 @@ def get_download_link(file_path):
208
  b64 = base64.b64encode(contents).decode()
209
  return f'<a href="data:file/txt;base64,{b64}" download="{os.path.basename(file_path)}">Download {os.path.basename(file_path)}πŸ“‚</a>'
210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  @st.cache_resource
212
  def SpeechSynthesis(result):
213
  """HTML5 Speech Synthesis."""
@@ -234,6 +274,79 @@ def SpeechSynthesis(result):
234
  '''
235
  components.html(documentHTML5, width=1280, height=300)
236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  # Media Processing Functions
238
  def process_image(image_input, user_prompt):
239
  """Process image with GPT-4o vision."""
@@ -243,6 +356,8 @@ def process_image(image_input, user_prompt):
243
 
244
  base64_image = base64.b64encode(image_input).decode("utf-8")
245
 
 
 
246
  response = openai_client.chat.completions.create(
247
  model=st.session_state["openai_model"],
248
  messages=[
@@ -279,6 +394,17 @@ def process_audio(audio_input, text_input=''):
279
  filename = generate_filename(transcription.text, "wav")
280
  create_and_save_file(audio_input, "wav", transcription.text, True)
281
 
 
 
 
 
 
 
 
 
 
 
 
282
  def process_video(video_path, seconds_per_frame=1):
283
  """Process video files for frame extraction and audio."""
284
  base64Frames = []
@@ -328,107 +454,13 @@ def process_video_with_gpt(video_input, user_prompt):
328
 
329
  return response.choices[0].message.content
330
 
331
- # ArXiv Search Functions
332
- def search_arxiv(query):
333
- """Search ArXiv papers using Hugging Face client."""
334
- client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
335
- response = client.predict(
336
- query,
337
- "mistralai/Mixtral-8x7B-Instruct-v0.1",
338
- True,
339
- api_name="/ask_llm"
340
- )
341
- return response
342
-
343
- # Chat Processing Functions
344
- def process_with_gpt(text_input):
345
- """Process text with GPT-4o."""
346
- if text_input:
347
- st.session_state.messages.append({"role": "user", "content": text_input})
348
-
349
- with st.chat_message("user"):
350
- st.markdown(text_input)
351
-
352
- with st.chat_message("assistant"):
353
- completion = openai_client.chat.completions.create(
354
- model=st.session_state["openai_model"],
355
- messages=[
356
- {"role": m["role"], "content": m["content"]}
357
- for m in st.session_state.messages
358
- ],
359
- stream=False
360
- )
361
- return_text = completion.choices[0].message.content
362
- st.write("GPT-4o: " + return_text)
363
-
364
- filename = generate_filename(text_input, "md")
365
- create_file(filename, text_input, return_text)
366
- st.session_state.messages.append({"role": "assistant", "content": return_text})
367
- return return_text
368
-
369
- def process_with_claude(text_input):
370
- """Process text with Claude."""
371
- if text_input:
372
- response = claude_client.messages.create(
373
- model="claude-3-sonnet-20240229",
374
- max_tokens=1000,
375
- messages=[
376
- {"role": "user", "content": text_input}
377
- ]
378
- )
379
-
380
- response_text = response.content[0].text
381
- st.write("Claude: " + response_text)
382
-
383
- filename = generate_filename(text_input, "md")
384
- create_file(filename, text_input, response_text)
385
-
386
- st.session_state.chat_history.append({
387
- "user": text_input,
388
- "claude": response_text
389
- })
390
- return response_text
391
-
392
- # File Management Functions
393
- def load_file(file_name):
394
- """Load file content."""
395
- with open(file_name, "r", encoding='utf-8') as file:
396
- content = file.read()
397
- return content
398
-
399
- def create_zip_of_files(files):
400
- """Create zip archive of files."""
401
- zip_name = "all_files.zip"
402
- with zipfile.ZipFile(zip_name, 'w') as zipf:
403
- for file in files:
404
- zipf.write(file)
405
- return zip_name
406
-
407
- def get_media_html(media_path, media_type="video", width="100%"):
408
- """Generate HTML for media player."""
409
- media_data = base64.b64encode(open(media_path, 'rb').read()).decode()
410
- if media_type == "video":
411
- return f'''
412
- <video width="{width}" controls autoplay muted loop>
413
- <source src="data:video/mp4;base64,{media_data}" type="video/mp4">
414
- Your browser does not support the video tag.
415
- </video>
416
- '''
417
- else: # audio
418
- return f'''
419
- <audio controls style="width: {width};">
420
- <source src="data:audio/mpeg;base64,{media_data}" type="audio/mpeg">
421
- Your browser does not support the audio element.
422
- </audio>
423
- '''
424
-
425
  def create_media_gallery():
426
  """Create the media gallery interface."""
427
  st.header("🎬 Media Gallery")
428
 
429
  tabs = st.tabs(["πŸ–ΌοΈ Images", "🎡 Audio", "πŸŽ₯ Video", "🎨 Scene Generator"])
430
 
431
- with tabs[0]:
432
  image_files = glob.glob("*.png") + glob.glob("*.jpg")
433
  if image_files:
434
  num_cols = st.slider("Number of columns", 1, 5, 3)
@@ -444,7 +476,7 @@ def create_media_gallery():
444
  st.markdown(analysis)
445
  SpeechSynthesis(analysis)
446
 
447
- with tabs[1]:
448
  audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
449
  for audio_file in audio_files:
450
  with st.expander(f"🎡 {os.path.basename(audio_file)}"):
@@ -455,7 +487,7 @@ def create_media_gallery():
455
  st.write(transcription)
456
  SpeechSynthesis(transcription)
457
 
458
- with tabs[2]:
459
  video_files = glob.glob("*.mp4")
460
  for video_file in video_files:
461
  with st.expander(f"πŸŽ₯ {os.path.basename(video_file)}"):
@@ -466,7 +498,7 @@ def create_media_gallery():
466
  st.markdown(analysis)
467
  SpeechSynthesis(analysis)
468
 
469
- with tabs[3]:
470
  for collection_name, bikes in bike_collections.items():
471
  st.subheader(collection_name)
472
  cols = st.columns(len(bikes))
@@ -486,6 +518,54 @@ def create_media_gallery():
486
  st.write(prompt)
487
  SpeechSynthesis(prompt)
488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
  def display_file_manager():
490
  """Display file management sidebar."""
491
  st.sidebar.title("πŸ“ File Management")
 
1
  import streamlit as st
2
  import anthropic
3
+ import openai
4
  import base64
5
  from datetime import datetime
6
  import plotly.graph_objects as go
 
87
  st.session_state["openai_model"] = "gpt-4o-2024-05-13"
88
  if "messages" not in st.session_state:
89
  st.session_state.messages = []
90
+ if "search_queries" not in st.session_state:
91
+ st.session_state.search_queries = []
92
 
93
  # Custom CSS
94
  st.markdown("""
 
182
  }
183
  }
184
 
185
+ # File Operations Functions
186
+ def create_file(filename, prompt, response, is_image=False):
187
+ """Basic file creation with prompt and response."""
188
+ with open(filename, "w", encoding="utf-8") as f:
189
+ f.write(prompt + "\n\n" + response)
190
+
191
  def generate_filename(prompt, file_type):
192
  """Generate a safe filename using the prompt and file type."""
193
  central = pytz.timezone('US/Central')
 
215
  b64 = base64.b64encode(contents).decode()
216
  return f'<a href="data:file/txt;base64,{b64}" download="{os.path.basename(file_path)}">Download {os.path.basename(file_path)}πŸ“‚</a>'
217
 
218
+ def load_file(file_name):
219
+ """Load file content."""
220
+ with open(file_name, "r", encoding='utf-8') as file:
221
+ content = file.read()
222
+ return content
223
+
224
+ def create_zip_of_files(files):
225
+ """Create zip archive of files."""
226
+ zip_name = "all_files.zip"
227
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
228
+ for file in files:
229
+ zipf.write(file)
230
+ return zip_name
231
+
232
+ def get_media_html(media_path, media_type="video", width="100%"):
233
+ """Generate HTML for media player."""
234
+ media_data = base64.b64encode(open(media_path, 'rb').read()).decode()
235
+ if media_type == "video":
236
+ return f'''
237
+ <video width="{width}" controls autoplay muted loop>
238
+ <source src="data:video/mp4;base64,{media_data}" type="video/mp4">
239
+ Your browser does not support the video tag.
240
+ </video>
241
+ '''
242
+ else: # audio
243
+ return f'''
244
+ <audio controls style="width: {width};">
245
+ <source src="data:audio/mpeg;base64,{media_data}" type="audio/mpeg">
246
+ Your browser does not support the audio element.
247
+ </audio>
248
+ '''
249
+
250
+ # Speech Synthesis
251
  @st.cache_resource
252
  def SpeechSynthesis(result):
253
  """HTML5 Speech Synthesis."""
 
274
  '''
275
  components.html(documentHTML5, width=1280, height=300)
276
 
277
+ # ArXiv Search Functions
278
+ def search_arxiv(query):
279
+ """Search ArXiv papers using Hugging Face client."""
280
+ start_time = time.strftime("%Y-%m-%d %H:%M:%S")
281
+ client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
282
+
283
+ # First query - Get papers
284
+ response1 = client.predict(
285
+ query,
286
+ 10,
287
+ "Semantic Search - up to 10 Mar 2024",
288
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
289
+ api_name="/update_with_rag_md"
290
+ )
291
+
292
+ # Second query - Get summary
293
+ response2 = client.predict(
294
+ query,
295
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
296
+ True,
297
+ api_name="/ask_llm"
298
+ )
299
+
300
+ Question = '### πŸ”Ž ' + query + '\r\n'
301
+ References = response1[0]
302
+ References2 = response1[1]
303
+ ReferenceLinks = extract_urls(References)
304
+
305
+ filename = generate_filename(query, "md")
306
+ create_file(filename, query, References + ReferenceLinks)
307
+
308
+ results = Question + '\r\n' + response2 + '\r\n' + References + '\r\n' + ReferenceLinks
309
+
310
+ end_time = time.strftime("%Y-%m-%d %H:%M:%S")
311
+ start_timestamp = time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S"))
312
+ end_timestamp = time.mktime(time.strptime(end_time, "%Y-%m-%d %H:%M:%S"))
313
+ elapsed_seconds = end_timestamp - start_timestamp
314
+
315
+ st.write(f"Start time: {start_time}")
316
+ st.write(f"Finish time: {end_time}")
317
+ st.write(f"Elapsed time: {elapsed_seconds:.2f} seconds")
318
+
319
+ return results
320
+
321
+ def extract_urls(text):
322
+ """Extract URLs from ArXiv search results."""
323
+ try:
324
+ date_pattern = re.compile(r'### (\d{2} \w{3} \d{4})')
325
+ abs_link_pattern = re.compile(r'\[(.*?)\]\((https://arxiv\.org/abs/\d+\.\d+)\)')
326
+ pdf_link_pattern = re.compile(r'\[⬇️\]\((https://arxiv\.org/pdf/\d+\.\d+)\)')
327
+ title_pattern = re.compile(r'### \d{2} \w{3} \d{4} \| \[(.*?)\]')
328
+
329
+ date_matches = date_pattern.findall(text)
330
+ abs_link_matches = abs_link_pattern.findall(text)
331
+ pdf_link_matches = pdf_link_pattern.findall(text)
332
+ title_matches = title_pattern.findall(text)
333
+
334
+ markdown_text = ""
335
+ for i in range(len(date_matches)):
336
+ date = date_matches[i]
337
+ title = title_matches[i]
338
+ abs_link = abs_link_matches[i][1]
339
+ pdf_link = pdf_link_matches[i]
340
+ markdown_text += f"**Date:** {date}\n\n"
341
+ markdown_text += f"**Title:** {title}\n\n"
342
+ markdown_text += f"**Abstract Link:** [{abs_link}]({abs_link})\n\n"
343
+ markdown_text += f"**PDF Link:** [{pdf_link}]({pdf_link})\n\n"
344
+ markdown_text += "---\n\n"
345
+ return markdown_text
346
+ except:
347
+ st.write('Error extracting URLs')
348
+ return ''
349
+
350
  # Media Processing Functions
351
  def process_image(image_input, user_prompt):
352
  """Process image with GPT-4o vision."""
 
356
 
357
  base64_image = base64.b64encode(image_input).decode("utf-8")
358
 
359
+
360
+
361
  response = openai_client.chat.completions.create(
362
  model=st.session_state["openai_model"],
363
  messages=[
 
394
  filename = generate_filename(transcription.text, "wav")
395
  create_and_save_file(audio_input, "wav", transcription.text, True)
396
 
397
+ def save_and_play_audio(audio_recorder):
398
+ """Save and play recorded audio."""
399
+ audio_bytes = audio_recorder()
400
+ if audio_bytes:
401
+ filename = generate_filename("Recording", "wav")
402
+ with open(filename, 'wb') as f:
403
+ f.write(audio_bytes)
404
+ st.audio(audio_bytes, format="audio/wav")
405
+ return filename
406
+ return None
407
+
408
  def process_video(video_path, seconds_per_frame=1):
409
  """Process video files for frame extraction and audio."""
410
  base64Frames = []
 
454
 
455
  return response.choices[0].message.content
456
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457
  def create_media_gallery():
458
  """Create the media gallery interface."""
459
  st.header("🎬 Media Gallery")
460
 
461
  tabs = st.tabs(["πŸ–ΌοΈ Images", "🎡 Audio", "πŸŽ₯ Video", "🎨 Scene Generator"])
462
 
463
+ with tabs[0]: # Images
464
  image_files = glob.glob("*.png") + glob.glob("*.jpg")
465
  if image_files:
466
  num_cols = st.slider("Number of columns", 1, 5, 3)
 
476
  st.markdown(analysis)
477
  SpeechSynthesis(analysis)
478
 
479
+ with tabs[1]: # Audio
480
  audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
481
  for audio_file in audio_files:
482
  with st.expander(f"🎡 {os.path.basename(audio_file)}"):
 
487
  st.write(transcription)
488
  SpeechSynthesis(transcription)
489
 
490
+ with tabs[2]: # Video
491
  video_files = glob.glob("*.mp4")
492
  for video_file in video_files:
493
  with st.expander(f"πŸŽ₯ {os.path.basename(video_file)}"):
 
498
  st.markdown(analysis)
499
  SpeechSynthesis(analysis)
500
 
501
+ with tabs[3]: # Scene Generator
502
  for collection_name, bikes in bike_collections.items():
503
  st.subheader(collection_name)
504
  cols = st.columns(len(bikes))
 
518
  st.write(prompt)
519
  SpeechSynthesis(prompt)
520
 
521
+ # Chat Processing Functions
522
+ def process_with_gpt(text_input):
523
+ """Process text with GPT-4o."""
524
+ if text_input:
525
+ st.session_state.messages.append({"role": "user", "content": text_input})
526
+
527
+ with st.chat_message("user"):
528
+ st.markdown(text_input)
529
+
530
+ with st.chat_message("assistant"):
531
+ completion = openai_client.chat.completions.create(
532
+ model=st.session_state["openai_model"],
533
+ messages=[
534
+ {"role": m["role"], "content": m["content"]}
535
+ for m in st.session_state.messages
536
+ ],
537
+ stream=False
538
+ )
539
+ return_text = completion.choices[0].message.content
540
+ st.write("GPT-4o: " + return_text)
541
+
542
+ filename = generate_filename(text_input, "md")
543
+ create_file(filename, text_input, return_text)
544
+ st.session_state.messages.append({"role": "assistant", "content": return_text})
545
+ return return_text
546
+
547
+ def process_with_claude(text_input):
548
+ """Process text with Claude."""
549
+ if text_input:
550
+ response = claude_client.messages.create(
551
+ model="claude-3-sonnet-20240229",
552
+ max_tokens=1000,
553
+ messages=[
554
+ {"role": "user", "content": text_input}
555
+ ]
556
+ )
557
+ response_text = response.content[0].text
558
+ st.write("Claude: " + response_text)
559
+
560
+ filename = generate_filename(text_input, "md")
561
+ create_file(filename, text_input, response_text)
562
+
563
+ st.session_state.chat_history.append({
564
+ "user": text_input,
565
+ "claude": response_text
566
+ })
567
+ return response_text
568
+
569
  def display_file_manager():
570
  """Display file management sidebar."""
571
  st.sidebar.title("πŸ“ File Management")