Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 41,343 Bytes
d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 766d18c f0f1a57 766d18c f0f1a57 766d18c f0f1a57 766d18c f0f1a57 766d18c f0f1a57 766d18c f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 d12bf77 f0f1a57 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 |
import streamlit as st
import anthropic
import openai
import base64
import cv2
import glob
import json
import math
import os
import pytz
import random
import re
import requests
import textract
import time
import zipfile
import plotly.graph_objects as go
import streamlit.components.v1 as components
from datetime import datetime
from audio_recorder_streamlit import audio_recorder
from bs4 import BeautifulSoup
from collections import defaultdict, deque, Counter
from dotenv import load_dotenv
from gradio_client import Client
from huggingface_hub import InferenceClient
from io import BytesIO
from PIL import Image
from PyPDF2 import PdfReader
from urllib.parse import quote
from xml.etree import ElementTree as ET
from openai import OpenAI
import extra_streamlit_components as stx
from streamlit.runtime.scriptrunner import get_script_run_ctx
import asyncio
import edge_tts
from streamlit_marquee import streamlit_marquee
from concurrent.futures import ThreadPoolExecutor
from functools import partial
from typing import Dict, List, Optional, Tuple, Union
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 1. CORE CONFIGURATION & SETUP
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.set_page_config(
page_title="π²TalkingAIResearcherπ",
page_icon="π²π",
layout="wide",
initial_sidebar_state="auto",
menu_items={
'Get Help': 'https://huggingface.co/awacke1',
'Report a bug': 'https://huggingface.co/spaces/awacke1',
'About': "π²TalkingAIResearcherπ"
}
)
load_dotenv()
# Available English voices for Edge TTS
EDGE_TTS_VOICES = [
"en-US-AriaNeural",
"en-US-GuyNeural",
"en-US-JennyNeural",
"en-GB-SoniaNeural",
"en-GB-RyanNeural",
"en-AU-NatashaNeural",
"en-AU-WilliamNeural",
"en-CA-ClaraNeural",
"en-CA-LiamNeural"
]
# Session state initialization with default values
DEFAULT_SESSION_STATE = {
'marquee_settings': {
"background": "#1E1E1E",
"color": "#FFFFFF",
"font-size": "14px",
"animationDuration": "20s",
"width": "100%",
"lineHeight": "35px"
},
'tts_voice': EDGE_TTS_VOICES[0],
'audio_format': 'mp3',
'transcript_history': [],
'chat_history': [],
'openai_model': "gpt-4o-2024-05-13",
'messages': [],
'last_voice_input': "",
'editing_file': None,
'edit_new_name': "",
'edit_new_content': "",
'viewing_prefix': None,
'should_rerun': False,
'old_val': None,
'last_query': "",
'marquee_content': "π Welcome to TalkingAIResearcher | π€ Your Research Assistant",
'enable_audio': False,
'enable_download': False,
'enable_claude': True,
'audio_cache': {},
'paper_cache': {},
'download_link_cache': {},
'performance_metrics': defaultdict(list),
'operation_timings': defaultdict(float)
}
# Initialize session state
for key, value in DEFAULT_SESSION_STATE.items():
if key not in st.session_state:
st.session_state[key] = value
# API Keys and Configuration
openai_api_key = os.getenv('OPENAI_API_KEY', "")
anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', "")
xai_key = os.getenv('xai', "")
if 'OPENAI_API_KEY' in st.secrets:
openai_api_key = st.secrets['OPENAI_API_KEY']
if 'ANTHROPIC_API_KEY' in st.secrets:
anthropic_key = st.secrets["ANTHROPIC_API_KEY"]
openai.api_key = openai_api_key
openai_client = OpenAI(api_key=openai.api_key, organization=os.getenv('OPENAI_ORG_ID'))
HF_KEY = os.getenv('HF_KEY')
API_URL = os.getenv('API_URL')
# File type emojis for display
FILE_EMOJIS = {
"md": "π",
"mp3": "π΅",
"wav": "π",
"pdf": "π",
"txt": "π",
"json": "π",
"csv": "π"
}
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2. PERFORMANCE MONITORING & TIMING
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class PerformanceTimer:
"""Context manager for timing operations with automatic logging."""
def __init__(self, operation_name: str):
self.operation_name = operation_name
self.start_time = None
def __enter__(self):
self.start_time = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if not exc_type: # Only log if no exception occurred
duration = time.time() - self.start_time
st.session_state['operation_timings'][self.operation_name] = duration
st.session_state['performance_metrics'][self.operation_name].append(duration)
def log_performance_metrics():
"""Display performance metrics in the sidebar."""
st.sidebar.markdown("### β±οΈ Performance Metrics")
metrics = st.session_state['operation_timings']
if metrics:
total_time = sum(metrics.values())
st.sidebar.write(f"**Total Processing Time:** {total_time:.2f}s")
# Create timing breakdown
for operation, duration in metrics.items():
percentage = (duration / total_time) * 100
st.sidebar.write(f"**{operation}:** {duration:.2f}s ({percentage:.1f}%)")
# Show timing history chart
if st.session_state['performance_metrics']:
history_data = []
for op, times in st.session_state['performance_metrics'].items():
if times: # Only show if we have timing data
avg_time = sum(times) / len(times)
history_data.append({"Operation": op, "Avg Time (s)": avg_time})
if history_data: # Create chart if we have data
st.sidebar.markdown("### π Timing History")
chart_data = pd.DataFrame(history_data)
st.sidebar.bar_chart(chart_data.set_index("Operation"))
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 3. OPTIMIZED AUDIO GENERATION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def clean_for_speech(text: str) -> str:
"""Clean up text for TTS output with enhanced cleaning."""
with PerformanceTimer("text_cleaning"):
# Remove markdown formatting
text = re.sub(r'#+ ', '', text) # Remove headers
text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text) # Clean links
text = re.sub(r'[*_~`]', '', text) # Remove emphasis markers
# Remove code blocks
text = re.sub(r'```[\s\S]*?```', '', text)
text = re.sub(r'`[^`]*`', '', text)
# Clean up whitespace
text = re.sub(r'\s+', ' ', text)
text = text.replace("\n", " ")
text = text.replace("</s>", " ")
# Remove URLs
text = re.sub(r'https?://\S+', '', text)
text = re.sub(r'\(https?://[^\)]+\)', '', text)
# Final cleanup
text = text.strip()
return text
async def async_edge_tts_generate(
text: str,
voice: str,
rate: int = 0,
pitch: int = 0,
file_format: str = "mp3"
) -> Tuple[Optional[str], float]:
"""Asynchronous TTS generation with performance tracking and caching."""
with PerformanceTimer("tts_generation") as timer:
# Clean and validate text
text = clean_for_speech(text)
if not text.strip():
return None, 0
# Check cache
cache_key = f"{text[:100]}_{voice}_{rate}_{pitch}_{file_format}"
if cache_key in st.session_state['audio_cache']:
return st.session_state['audio_cache'][cache_key], 0
try:
# Generate audio
rate_str = f"{rate:+d}%"
pitch_str = f"{pitch:+d}Hz"
communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
# Generate unique filename
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"audio_{timestamp}_{random.randint(1000, 9999)}.{file_format}"
# Save audio file
await communicate.save(filename)
# Cache result
st.session_state['audio_cache'][cache_key] = filename
return filename, time.time() - timer.start_time
except Exception as e:
st.error(f"Error generating audio: {str(e)}")
return None, 0
async def async_save_qa_with_audio(
question: str,
answer: str,
voice: Optional[str] = None
) -> Tuple[str, Optional[str], float, float]:
"""Asynchronously save Q&A to markdown and generate audio with timing."""
voice = voice or st.session_state['tts_voice']
with PerformanceTimer("qa_save") as timer:
# Save markdown
md_start = time.time()
combined_text = f"# Question\n{question}\n\n# Answer\n{answer}"
md_file = create_file(question, answer, "md")
md_time = time.time() - md_start
# Generate audio if enabled
audio_file = None
audio_time = 0
if st.session_state['enable_audio']:
audio_text = f"{question}\n\nAnswer: {answer}"
audio_file, audio_time = await async_edge_tts_generate(
audio_text,
voice=voice,
file_format=st.session_state['audio_format']
)
return md_file, audio_file, md_time, audio_time
def create_download_link_with_cache(
file_path: str,
file_type: str = "mp3"
) -> str:
"""Create download link with caching and error handling."""
with PerformanceTimer("download_link_generation"):
# Check cache first
cache_key = f"dl_{file_path}"
if cache_key in st.session_state['download_link_cache']:
return st.session_state['download_link_cache'][cache_key]
try:
with open(file_path, "rb") as f:
b64 = base64.b64encode(f.read()).decode()
# Generate appropriate link based on file type
filename = os.path.basename(file_path)
if file_type == "mp3":
link = f'<a href="data:audio/mpeg;base64,{b64}" download="{filename}">π΅ Download {filename}</a>'
elif file_type == "wav":
link = f'<a href="data:audio/wav;base64,{b64}" download="{filename}">π Download {filename}</a>'
elif file_type == "md":
link = f'<a href="data:text/markdown;base64,{b64}" download="{filename}">π Download {filename}</a>'
else:
link = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">β¬οΈ Download {filename}</a>'
# Cache and return
st.session_state['download_link_cache'][cache_key] = link
return link
except Exception as e:
st.error(f"Error creating download link: {str(e)}")
return ""
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 4. PAPER PROCESSING & DISPLAY
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def parse_arxiv_refs(ref_text: str) -> List[Dict[str, str]]:
"""Parse arxiv references with improved error handling."""
if not ref_text:
return []
with PerformanceTimer("parse_refs"):
results = []
current_paper = {}
lines = ref_text.split('\n')
for i, line in enumerate(lines):
try:
if line.count('|') == 2:
# Found a new paper line
if current_paper:
results.append(current_paper)
if len(results) >= 20: # Limit to 20 papers
break
# Parse header parts
header_parts = line.strip('* ').split('|')
date = header_parts[0].strip()
title = header_parts[1].strip()
url_match = re.search(r'(https://arxiv.org/\S+)', line)
url = url_match.group(1) if url_match else f"paper_{len(results)}"
current_paper = {
'date': date,
'title': title,
'url': url,
'authors': '',
'summary': '',
'full_audio': None,
'download_base64': '',
}
elif current_paper:
# Add content to current paper
line = line.strip('* ')
if not current_paper['authors']:
current_paper['authors'] = line
else:
if current_paper['summary']:
current_paper['summary'] += ' ' + line
else:
current_paper['summary'] = line
except Exception as e:
st.warning(f"Error parsing line {i}: {str(e)}")
continue
# Add final paper if exists
if current_paper:
results.append(current_paper)
return results[:20] # Ensure we don't exceed 20 papers
async def create_paper_audio_files(papers: List[Dict], input_question: str):
"""Generate audio files for papers asynchronously with progress tracking."""
with PerformanceTimer("paper_audio_generation"):
tasks = []
for paper in papers:
try:
# Prepare text for audio generation
audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
audio_text = clean_for_speech(audio_text)
# Create task for audio generation
task = async_edge_tts_generate(
audio_text,
voice=st.session_state['tts_voice'],
file_format=st.session_state['audio_format']
)
tasks.append((paper, task))
except Exception as e:
st.warning(f"Error preparing audio for paper {paper['title']}: {str(e)}")
continue
# Process all audio generation tasks concurrently
for paper, task in tasks:
try:
audio_file, gen_time = await task
if audio_file:
paper['full_audio'] = audio_file
if st.session_state['enable_download']:
paper['download_base64'] = create_download_link_with_cache(
audio_file,
st.session_state['audio_format']
)
except Exception as e:
st.warning(f"Error generating audio for paper {paper['title']}: {str(e)}")
paper['full_audio'] = None
paper['download_base64'] = ''
def display_papers(papers: List[Dict], marquee_settings: Dict):
"""Display paper information with enhanced visualization."""
with PerformanceTimer("paper_display"):
st.write("## π Research Papers")
# Create tabs for different views
tab1, tab2 = st.tabs(["π List View", "π Grid View"])
with tab1:
for i, paper in enumerate(papers, start=1):
# Create marquee for paper title
marquee_text = f"π {paper['title']} | π€ {paper['authors'][:120]}"
display_marquee(marquee_text, marquee_settings, key_suffix=f"paper_{i}")
# Paper details expander
with st.expander(f"{i}. π {paper['title']}", expanded=True):
# Create PDF link
pdf_url = paper['url'].replace('/abs/', '/pdf/')
# Display paper information
st.markdown(f"""
**Date:** {paper['date']}
**Title:** {paper['title']}
**Links:** π [Abstract]({paper['url']}) | π [PDF]({pdf_url})
""")
st.markdown(f"**Authors:** {paper['authors']}")
st.markdown(f"**Summary:** {paper['summary']}")
# Audio player and download if available
if paper.get('full_audio'):
st.write("π§ Paper Audio Summary")
st.audio(paper['full_audio'])
if paper['download_base64']:
st.markdown(paper['download_base64'], unsafe_allow_html=True)
with tab2:
# Create a grid layout of papers
cols = st.columns(3)
for i, paper in enumerate(papers):
with cols[i % 3]:
st.markdown(f"""
### π {paper['title'][:50]}...
**Date:** {paper['date']}
[Abstract]({paper['url']}) | [PDF]({paper['url'].replace('/abs/', '/pdf/')})
""")
if paper.get('full_audio'):
st.audio(paper['full_audio'])
def display_papers_in_sidebar(papers: List[Dict]):
"""Display paper listing in sidebar with lazy loading."""
with PerformanceTimer("sidebar_display"):
st.sidebar.title("π Papers Overview")
# Add filter options
filter_date = st.sidebar.date_input("Filter by date:", None)
search_term = st.sidebar.text_input("Search papers:", "")
# Filter papers based on criteria
filtered_papers = papers
if filter_date:
filtered_papers = [p for p in filtered_papers
if filter_date.strftime("%Y-%m-%d") in p['date']]
if search_term:
search_lower = search_term.lower()
filtered_papers = [p for p in filtered_papers
if search_lower in p['title'].lower()
or search_lower in p['authors'].lower()]
# Display filtered papers
for i, paper in enumerate(filtered_papers, start=1):
paper_key = f"paper_{paper['url']}"
if paper_key not in st.session_state:
st.session_state[paper_key] = False
with st.sidebar.expander(f"{i}. {paper['title'][:50]}...", expanded=False):
# Paper metadata
st.markdown(f"**Date:** {paper['date']}")
# Links
pdf_url = paper['url'].replace('/abs/', '/pdf/')
st.markdown(f"π [Abstract]({paper['url']}) | π [PDF]({pdf_url})")
# Preview of authors and summary
st.markdown(f"**Authors:** {paper['authors'][:100]}...")
if paper['summary']:
st.markdown(f"**Summary:** {paper['summary'][:200]}...")
# Audio controls
if paper['full_audio']:
if st.button("π΅ Load Audio", key=f"btn_{paper_key}"):
st.session_state[paper_key] = True
if st.session_state[paper_key]:
st.audio(paper['full_audio'])
if paper['download_base64']:
st.markdown(paper['download_base64'], unsafe_allow_html=True)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 5. FILE MANAGEMENT & HISTORY
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def create_file(prompt: str, response: str, file_type: str = "md") -> str:
"""Create a file with proper naming and error handling."""
with PerformanceTimer("file_creation"):
try:
# Generate filename
filename = generate_filename(prompt.strip(), response.strip(), file_type)
# Ensure directory exists
os.makedirs("generated_files", exist_ok=True)
filepath = os.path.join("generated_files", filename)
# Write content
with open(filepath, 'w', encoding='utf-8') as f:
if file_type == "md":
f.write(f"# Query\n{prompt}\n\n# Response\n{response}")
else:
f.write(f"{prompt}\n\n{response}")
return filepath
except Exception as e:
st.error(f"Error creating file: {str(e)}")
return ""
def get_high_info_terms(text: str, top_n: int = 10) -> List[str]:
"""Extract most informative terms from text."""
# Common English stop words to filter out
stop_words = set([
'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to',
'for', 'of', 'with', 'by', 'from', 'up', 'about', 'into', 'over',
'after', 'the', 'this', 'that', 'these', 'those', 'what', 'which'
])
# Extract words and bi-grams
words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
bi_grams = [' '.join(pair) for pair in zip(words, words[1:])]
# Combine and filter terms
combined = words + bi_grams
filtered = [term for term in combined
if term not in stop_words
and len(term.split()) <= 2
and len(term) > 3]
# Count and return top terms
counter = Counter(filtered)
return [term for term, freq in counter.most_common(top_n)]
def clean_text_for_filename(text: str) -> str:
"""Clean text for use in filenames."""
# Remove special characters
text = text.lower()
text = re.sub(r'[^\w\s-]', '', text)
# Remove common unhelpful words
stop_words = set([
'the', 'and', 'for', 'with', 'this', 'that', 'what', 'which',
'where', 'when', 'why', 'how', 'who', 'whom', 'whose', 'ai',
'library', 'function', 'method', 'class', 'object', 'variable'
])
words = text.split()
filtered = [w for w in words if len(w) > 3 and w not in stop_words]
return '_'.join(filtered)[:200]
def generate_filename(prompt: str, response: str, file_type: str = "md",
max_length: int = 200) -> str:
"""Generate descriptive filename from content."""
# Get timestamp prefix
prefix = format_timestamp_prefix() + "_"
# Extract informative terms
combined_text = (prompt + " " + response)[:500]
info_terms = get_high_info_terms(combined_text, top_n=5)
# Get content snippet
snippet = (prompt[:40] + " " + response[:40]).strip()
snippet_cleaned = clean_text_for_filename(snippet)
# Combine and deduplicate parts
name_parts = info_terms + [snippet_cleaned]
seen = set()
unique_parts = []
for part in name_parts:
if part not in seen:
seen.add(part)
unique_parts.append(part)
# Create final filename
full_name = '_'.join(unique_parts).strip('_')
leftover_chars = max_length - len(prefix) - len(file_type) - 1
if len(full_name) > leftover_chars:
full_name = full_name[:leftover_chars]
return f"{prefix}{full_name}.{file_type}"
def create_zip_of_files(md_files: List[str], mp3_files: List[str],
wav_files: List[str], input_question: str) -> Optional[str]:
"""Create zip archive of files with optimization."""
with PerformanceTimer("zip_creation"):
# Filter out readme and empty files
md_files = [f for f in md_files
if os.path.basename(f).lower() != 'readme.md'
and os.path.getsize(f) > 0]
all_files = md_files + mp3_files + wav_files
if not all_files:
return None
try:
# Generate zip name
all_content = []
for f in all_files:
if f.endswith('.md'):
with open(f, 'r', encoding='utf-8') as file:
all_content.append(file.read())
elif f.endswith(('.mp3', '.wav')):
basename = os.path.splitext(os.path.basename(f))[0]
all_content.append(basename.replace('_', ' '))
all_content.append(input_question)
combined_content = " ".join(all_content)
info_terms = get_high_info_terms(combined_content, top_n=10)
timestamp = format_timestamp_prefix()
name_text = '-'.join(term for term in info_terms[:5])
zip_name = f"archive_{timestamp}_{name_text[:50]}.zip"
# Create zip file
with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as z:
for f in all_files:
z.write(f, os.path.basename(f))
return zip_name
except Exception as e:
st.error(f"Error creating zip archive: {str(e)}")
return None
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 6. OPTIMIZED AI LOOKUP & PROCESSING
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def perform_ai_lookup(q: str, vocal_summary: bool = True,
extended_refs: bool = False,
titles_summary: bool = True,
full_audio: bool = False) -> Tuple[str, Dict[str, float]]:
"""Main AI lookup routine with performance optimization."""
with PerformanceTimer("total_lookup") as total_timer:
timings = {}
# Add operation controls if not present
if 'operation_controls' not in st.session_state:
st.sidebar.markdown("### π§ Operation Controls")
st.session_state['enable_claude'] = st.sidebar.checkbox(
"Enable Claude Search",
value=st.session_state['enable_claude']
)
st.session_state['enable_audio'] = st.sidebar.checkbox(
"Generate Audio",
value=st.session_state['enable_audio']
)
st.session_state['enable_download'] = st.sidebar.checkbox(
"Create Download Links",
value=st.session_state['enable_download']
)
st.session_state['operation_controls'] = True
result = ""
# 1. Claude API (if enabled)
if st.session_state['enable_claude']:
with PerformanceTimer("claude_api") as claude_timer:
try:
client = anthropic.Anthropic(api_key=anthropic_key)
response = client.messages.create(
model="claude-3-sonnet-20240229",
max_tokens=1000,
messages=[{"role": "user", "content": q}]
)
st.write("Claude's reply π§ :")
st.markdown(response.content[0].text)
result = response.content[0].text
timings['claude_api'] = time.time() - claude_timer.start_time
except Exception as e:
st.error(f"Error with Claude API: {str(e)}")
result = "Error occurred during Claude API call"
timings['claude_api'] = 0
# 2. Async save and audio generation
async def process_results():
with PerformanceTimer("results_processing") as proc_timer:
md_file, audio_file, md_time, audio_time = await async_save_qa_with_audio(
q, result
)
timings['markdown_save'] = md_time
timings['audio_generation'] = audio_time
if audio_file and st.session_state['enable_audio']:
st.subheader("π Main Response Audio")
st.audio(audio_file)
if st.session_state['enable_download']:
st.markdown(
create_download_link_with_cache(
audio_file,
st.session_state['audio_format']
),
unsafe_allow_html=True
)
# Run async operations
asyncio.run(process_results())
# 3. Arxiv RAG with performance tracking
if st.session_state['enable_claude']:
with PerformanceTimer("arxiv_rag") as rag_timer:
try:
st.write('Running Arxiv RAG with Claude inputs.')
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
refs = client.predict(
q,
10,
"Semantic Search",
"mistralai/Mixtral-8x7B-Instruct-v0.1",
api_name="/update_with_rag_md"
)[0]
timings['arxiv_rag'] = time.time() - rag_timer.start_time
# Process papers asynchronously
papers = parse_arxiv_refs(refs)
if papers:
with PerformanceTimer("paper_processing") as paper_timer:
async def process_papers():
# Create minimal links page
paper_links = create_paper_links_md(papers)
links_file = create_file(q, paper_links, "md")
st.markdown(paper_links)
# Generate audio and display papers
await create_paper_audio_files(papers, q)
display_papers(papers, get_marquee_settings())
display_papers_in_sidebar(papers)
asyncio.run(process_papers())
timings['paper_processing'] = time.time() - paper_timer.start_time
else:
st.warning("No papers found in the response.")
except Exception as e:
st.error(f"Error during Arxiv RAG: {str(e)}")
timings['arxiv_rag'] = 0
return result, timings
def process_voice_input(text: str):
"""Process voice input with enhanced error handling and feedback."""
if not text:
st.warning("Please provide some input text.")
return
with PerformanceTimer("voice_processing"):
try:
st.subheader("π Search Results")
result, timings = perform_ai_lookup(
text,
vocal_summary=True,
extended_refs=False,
titles_summary=True,
full_audio=True
)
# Save results
md_file, audio_file = save_qa_with_audio(text, result)
# Display results
st.subheader("π Generated Files")
col1, col2 = st.columns(2)
with col1:
st.write(f"π Markdown: {os.path.basename(md_file)}")
st.markdown(get_download_link(md_file, "md"), unsafe_allow_html=True)
with col2:
if audio_file:
st.write(f"π΅ Audio: {os.path.basename(audio_file)}")
play_and_download_audio(
audio_file,
st.session_state['audio_format']
)
except Exception as e:
st.error(f"Error processing voice input: {str(e)}")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 7. SIDEBAR AND FILE HISTORY
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def display_file_history_in_sidebar():
"""Display file history with enhanced organization and filtering."""
with PerformanceTimer("file_history"):
st.sidebar.markdown("---")
st.sidebar.markdown("### π File History")
# Gather all files
md_files = glob.glob("*.md")
mp3_files = glob.glob("*.mp3")
wav_files = glob.glob("*.wav")
all_files = md_files + mp3_files + wav_files
if not all_files:
st.sidebar.write("No files found.")
return
# Add file management controls
col1, col2 = st.sidebar.columns(2)
with col1:
if st.button("π Delete All"):
try:
for f in all_files:
os.remove(f)
st.session_state.should_rerun = True
st.success("All files deleted successfully.")
except Exception as e:
st.error(f"Error deleting files: {str(e)}")
with col2:
if st.button("β¬οΈ Zip All"):
zip_name = create_zip_of_files(
md_files,
mp3_files,
wav_files,
st.session_state.get('last_query', '')
)
if zip_name:
st.sidebar.markdown(
get_download_link(zip_name, "zip"),
unsafe_allow_html=True
)
# Add file filtering options
st.sidebar.markdown("### π Filter Files")
file_search = st.sidebar.text_input("Search files:", "")
file_type_filter = st.sidebar.multiselect(
"File types:",
["Markdown", "Audio"],
default=["Markdown", "Audio"]
)
# Sort files by modification time
all_files.sort(key=os.path.getmtime, reverse=True)
# Filter files based on search and type
filtered_files = []
for f in all_files:
if file_search.lower() in f.lower():
ext = os.path.splitext(f)[1].lower()
if (("Markdown" in file_type_filter and ext == ".md") or
("Audio" in file_type_filter and ext in [".mp3", ".wav"])):
filtered_files.append(f)
# Display filtered files
for f in filtered_files:
fname = os.path.basename(f)
ext = os.path.splitext(fname)[1].lower().strip('.')
emoji = FILE_EMOJIS.get(ext, 'π¦')
# Get file metadata
mod_time = datetime.fromtimestamp(os.path.getmtime(f))
time_str = mod_time.strftime("%Y-%m-%d %H:%M:%S")
file_size = os.path.getsize(f) / 1024 # Size in KB
with st.sidebar.expander(f"{emoji} {fname}"):
st.write(f"**Modified:** {time_str}")
st.write(f"**Size:** {file_size:.1f} KB")
if ext == "md":
try:
with open(f, "r", encoding="utf-8") as file_in:
snippet = file_in.read(200).replace("\n", " ")
if len(snippet) == 200:
snippet += "..."
st.write(snippet)
st.markdown(
get_download_link(f, file_type="md"),
unsafe_allow_html=True
)
except Exception as e:
st.error(f"Error reading markdown file: {str(e)}")
elif ext in ["mp3", "wav"]:
st.audio(f)
st.markdown(
get_download_link(f, file_type=ext),
unsafe_allow_html=True
)
else:
st.markdown(get_download_link(f), unsafe_allow_html=True)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 8. MAIN APPLICATION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def main():
"""Main application entry point with enhanced UI and error handling."""
try:
# 1. Setup marquee UI in sidebar
update_marquee_settings_ui()
marquee_settings = get_marquee_settings()
# 2. Display welcome marquee
display_marquee(
st.session_state['marquee_content'],
{**marquee_settings, "font-size": "28px", "lineHeight": "50px"},
key_suffix="welcome"
)
# 3. Main action tabs
tab_main = st.radio(
"Action:",
["π€ Voice", "πΈ Media", "π ArXiv", "π Editor"],
horizontal=True
)
# Custom component usage
mycomponent = components.declare_component(
"mycomponent",
path="mycomponent"
)
val = mycomponent(my_input_value="Hello")
if val:
# Process input value
val_stripped = val.replace('\\n', ' ')
edited_input = st.text_area(
"βοΈ Edit Input:",
value=val_stripped,
height=100
)
# Model selection and options
run_option = st.selectbox("Model:", ["Arxiv"])
col1, col2 = st.columns(2)
with col1:
autorun = st.checkbox("β AutoRun", value=True)
with col2:
full_audio = st.checkbox("π FullAudio", value=False)
# Check for input changes
input_changed = (val != st.session_state.old_val)
if autorun and input_changed:
st.session_state.old_val = val
st.session_state.last_query = edited_input
result, timings = perform_ai_lookup(
edited_input,
vocal_summary=True,
extended_refs=False,
titles_summary=True,
full_audio=full_audio
)
# Display performance metrics
display_performance_metrics(timings)
else:
if st.button("βΆ Run"):
st.session_state.old_val = val
st.session_state.last_query = edited_input
result, timings = perform_ai_lookup(
edited_input,
vocal_summary=True,
extended_refs=False,
titles_summary=True,
full_audio=full_audio
)
# Display performance metrics
display_performance_metrics(timings)
# Tab-specific content
if tab_main == "π ArXiv":
display_arxiv_tab()
elif tab_main == "π€ Voice":
display_voice_tab()
elif tab_main == "πΈ Media":
display_media_tab()
elif tab_main == "π Editor":
display_editor_tab()
# Display file history
display_file_history_in_sidebar()
# Apply styling
apply_custom_styling()
# Check for rerun
if st.session_state.should_rerun:
st.session_state.should_rerun = False
st.rerun()
except Exception as e:
st.error(f"An error occurred in the main application: {str(e)}")
st.info("Please try refreshing the page or contact support if the issue persists.")
if __name__ == "__main__":
main() |