import streamlit as st
import anthropic, openai, base64, cv2, glob, json, math, os, pytz, random, re, requests, time, 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
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
# Available English voices
ENGLISH_VOICES = [
"en-US-AriaNeural", # Female, conversational
"en-US-JennyNeural", # Female, customer service
"en-US-GuyNeural", # Male, newscast
"en-US-RogerNeural", # Male, calm
"en-GB-SoniaNeural", # British female
"en-GB-RyanNeural", # British male
"en-AU-NatashaNeural", # Australian female
"en-AU-WilliamNeural", # Australian male
"en-CA-ClaraNeural", # Canadian female
"en-CA-LiamNeural", # Canadian male
"en-IE-EmilyNeural", # Irish female
"en-IE-ConnorNeural", # Irish male
"en-IN-NeerjaNeural", # Indian female
"en-IN-PrabhatNeural", # Indian male
]
# Core Configuration & Setup
st.set_page_config(
page_title="ARIA Research Assistant",
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': "ARIA: Academic Research Interactive Assistant"
}
)
load_dotenv()
# API Setup
openai_api_key = os.getenv('OPENAI_API_KEY', st.secrets.get('OPENAI_API_KEY', ''))
anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', st.secrets.get('ANTHROPIC_API_KEY', ''))
openai_client = OpenAI(api_key=openai_api_key)
claude_client = anthropic.Anthropic(api_key=anthropic_key)
# Session State Management
if 'transcript_history' not in st.session_state:
st.session_state['transcript_history'] = []
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = []
if 'openai_model' not in st.session_state:
st.session_state['openai_model'] = "gpt-4-vision-preview"
if 'messages' not in st.session_state:
st.session_state['messages'] = []
if 'last_voice_input' not in st.session_state:
st.session_state['last_voice_input'] = ""
if 'current_audio' not in st.session_state:
st.session_state['current_audio'] = None
if 'autoplay_audio' not in st.session_state:
st.session_state['autoplay_audio'] = True
if 'should_rerun' not in st.session_state:
st.session_state['should_rerun'] = False
if 'autorun' not in st.session_state:
st.session_state.autorun = True
if 'run_option' not in st.session_state:
st.session_state.run_option = "Arxiv"
if 'last_processed_text' not in st.session_state:
st.session_state.last_processed_text = ""
# Custom CSS
st.markdown("""
""", unsafe_allow_html=True)
def create_voice_component():
"""Create auto-searching voice recognition component"""
return components.html(
"""
Starting voice recognition...
""",
height=200
)
def get_audio_autoplay_html(audio_path):
"""Create HTML for autoplaying audio with controls and download"""
try:
with open(audio_path, "rb") as audio_file:
audio_bytes = audio_file.read()
audio_b64 = base64.b64encode(audio_bytes).decode()
return f'''