Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -2,54 +2,32 @@ import streamlit as st
|
|
2 |
import anthropic, openai, base64, cv2, glob, json, math, os, pytz, random, re, requests, time, zipfile
|
3 |
from datetime import datetime
|
4 |
from audio_recorder_streamlit import audio_recorder
|
5 |
-
from collections import defaultdict
|
6 |
from dotenv import load_dotenv
|
7 |
from gradio_client import Client
|
8 |
from huggingface_hub import InferenceClient
|
9 |
from PIL import Image
|
10 |
-
from
|
11 |
import asyncio
|
12 |
import edge_tts
|
13 |
-
from streamlit_marquee import streamlit_marquee
|
14 |
-
|
15 |
-
st.set_page_config(
|
16 |
-
page_title="🚲TalkingAIResearcher🏆",
|
17 |
-
page_icon="🚲🏆",
|
18 |
-
layout="wide"
|
19 |
-
)
|
20 |
|
21 |
-
|
22 |
-
"en-US-AriaNeural",
|
23 |
-
"en-US-GuyNeural",
|
24 |
-
"en-US-JennyNeural",
|
25 |
-
"en-GB-SoniaNeural"
|
26 |
-
]
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
"mp3": "🎵",
|
31 |
-
"wav": "🔊",
|
32 |
-
"txt": "📄",
|
33 |
-
"pdf": "📑"
|
34 |
-
}
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
st.session_state
|
47 |
-
|
48 |
-
st.session_state['should_rerun'] = False
|
49 |
-
|
50 |
-
# API Setup
|
51 |
-
openai_client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
52 |
-
claude_client = anthropic.Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))
|
53 |
|
54 |
@st.cache_resource
|
55 |
def get_cached_audio_b64(file_path):
|
@@ -60,33 +38,25 @@ def beautify_filename(filename):
|
|
60 |
name = os.path.splitext(filename)[0]
|
61 |
return name.replace('_', ' ').replace('.', ' ')
|
62 |
|
63 |
-
def create_zip_of_files(md_files, mp3_files, wav_files, query=''):
|
64 |
-
all_files = md_files + mp3_files + wav_files
|
65 |
-
if not all_files: return None
|
66 |
-
|
67 |
-
timestamp = datetime.now().strftime("%y%m_%H%M")
|
68 |
-
zip_name = f"{timestamp}_archive.zip"
|
69 |
-
with zipfile.ZipFile(zip_name, 'w') as z:
|
70 |
-
for f in all_files:
|
71 |
-
z.write(f)
|
72 |
-
return zip_name
|
73 |
-
|
74 |
-
def get_download_link(file_path, file_type="zip"):
|
75 |
-
with open(file_path, "rb") as f:
|
76 |
-
b64 = base64.b64encode(f.read()).decode()
|
77 |
-
ext_map = {'zip': '📦', 'mp3': '🎵', 'wav': '🔊', 'md': '📝'}
|
78 |
-
emoji = ext_map.get(file_type, '')
|
79 |
-
return f'<a href="data:application/{file_type};base64,{b64}" download="{os.path.basename(file_path)}">{emoji} Download {os.path.basename(file_path)}</a>'
|
80 |
-
|
81 |
def load_files_for_sidebar():
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
groups = defaultdict(list)
|
84 |
for f in files:
|
85 |
basename = os.path.basename(f)
|
86 |
group_name = basename[:9] if len(basename) >= 9 else 'Other'
|
87 |
groups[group_name].append(f)
|
|
|
88 |
return sorted(groups.items(),
|
89 |
-
key=lambda x: max(os.path.getmtime(f) for f in x[1]),
|
90 |
reverse=True)
|
91 |
|
92 |
def display_marquee_controls():
|
@@ -108,6 +78,23 @@ def display_marquee_controls():
|
|
108 |
"lineHeight": "35px"
|
109 |
}
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
def display_file_manager_sidebar(groups_sorted):
|
112 |
st.sidebar.title("📚 File Manager")
|
113 |
all_files = {'md': [], 'mp3': [], 'wav': []}
|
@@ -124,18 +111,18 @@ def display_file_manager_sidebar(groups_sorted):
|
|
124 |
if st.button(f"🗑️ {ext.upper()}"):
|
125 |
[os.remove(f) for f in files]
|
126 |
st.session_state.should_rerun = True
|
127 |
-
|
128 |
if st.sidebar.button("📦 Zip All"):
|
129 |
-
zip_name = create_zip_of_files(
|
130 |
-
all_files['md'], all_files['mp3'], all_files['wav']
|
131 |
-
)
|
132 |
if zip_name:
|
133 |
st.sidebar.markdown(get_download_link(zip_name), unsafe_allow_html=True)
|
134 |
|
135 |
for group_name, files in groups_sorted:
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
139 |
with st.sidebar.expander(f"📁 {timestamp} ({len(files)})", expanded=True):
|
140 |
c1, c2 = st.columns(2)
|
141 |
with c1:
|
@@ -157,14 +144,12 @@ def display_file_manager_sidebar(groups_sorted):
|
|
157 |
if st.button("🔄", key=f"loop_{f}"):
|
158 |
audio_b64 = get_cached_audio_b64(f)
|
159 |
st.components.v1.html(
|
160 |
-
f'''
|
161 |
-
<audio id="player_{f}" loop>
|
162 |
<source src="data:audio/{ext};base64,{audio_b64}">
|
163 |
-
|
164 |
-
|
165 |
document.getElementById("player_{f}").play();
|
166 |
-
|
167 |
-
''',
|
168 |
height=0
|
169 |
)
|
170 |
|
@@ -220,7 +205,7 @@ def perform_ai_lookup(query):
|
|
220 |
**marquee_settings,
|
221 |
key=f"paper_{paper['id'] or random.randint(1000,9999)}"
|
222 |
)
|
223 |
-
st.write("")
|
224 |
|
225 |
return papers
|
226 |
|
|
|
2 |
import anthropic, openai, base64, cv2, glob, json, math, os, pytz, random, re, requests, time, zipfile
|
3 |
from datetime import datetime
|
4 |
from audio_recorder_streamlit import audio_recorder
|
5 |
+
from collections import defaultdict
|
6 |
from dotenv import load_dotenv
|
7 |
from gradio_client import Client
|
8 |
from huggingface_hub import InferenceClient
|
9 |
from PIL import Image
|
10 |
+
from streamlit_marquee import streamlit_marquee
|
11 |
import asyncio
|
12 |
import edge_tts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
st.set_page_config(page_title="🚲TalkingAIResearcher🏆", page_icon="🚲🏆", layout="wide")
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
EDGE_TTS_VOICES = ["en-US-AriaNeural", "en-US-GuyNeural", "en-US-JennyNeural", "en-GB-SoniaNeural"]
|
17 |
+
FILE_EMOJIS = {"md": "📝", "mp3": "🎵", "wav": "🔊", "txt": "📄", "pdf": "📑"}
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Session state initialization
|
20 |
+
for key, default in {
|
21 |
+
'tts_voice': EDGE_TTS_VOICES[0],
|
22 |
+
'audio_format': 'mp3',
|
23 |
+
'messages': [],
|
24 |
+
'chat_history': [],
|
25 |
+
'transcript_history': [],
|
26 |
+
'viewing_prefix': None,
|
27 |
+
'should_rerun': False
|
28 |
+
}.items():
|
29 |
+
if key not in st.session_state:
|
30 |
+
st.session_state[key] = default
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
@st.cache_resource
|
33 |
def get_cached_audio_b64(file_path):
|
|
|
38 |
name = os.path.splitext(filename)[0]
|
39 |
return name.replace('_', ' ').replace('.', ' ')
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def load_files_for_sidebar():
|
42 |
+
"""Load and filter files for sidebar by timestamp prefix"""
|
43 |
+
files = []
|
44 |
+
for f in glob.glob("*.*"):
|
45 |
+
basename = os.path.basename(f)
|
46 |
+
if f.endswith('.md'):
|
47 |
+
if len(basename) >= 9 and re.match(r'\d{4}_\d{4}', basename[:9]):
|
48 |
+
files.append(f)
|
49 |
+
else:
|
50 |
+
files.append(f)
|
51 |
+
|
52 |
groups = defaultdict(list)
|
53 |
for f in files:
|
54 |
basename = os.path.basename(f)
|
55 |
group_name = basename[:9] if len(basename) >= 9 else 'Other'
|
56 |
groups[group_name].append(f)
|
57 |
+
|
58 |
return sorted(groups.items(),
|
59 |
+
key=lambda x: max(os.path.getmtime(f) for f in x[1]),
|
60 |
reverse=True)
|
61 |
|
62 |
def display_marquee_controls():
|
|
|
78 |
"lineHeight": "35px"
|
79 |
}
|
80 |
|
81 |
+
def get_download_link(file_path, file_type="zip"):
|
82 |
+
with open(file_path, "rb") as f:
|
83 |
+
b64 = base64.b64encode(f.read()).decode()
|
84 |
+
ext_map = {'zip': '📦', 'mp3': '🎵', 'wav': '🔊', 'md': '📝'}
|
85 |
+
emoji = ext_map.get(file_type, '')
|
86 |
+
return f'<a href="data:application/{file_type};base64,{b64}" download="{os.path.basename(file_path)}">{emoji} Download {os.path.basename(file_path)}</a>'
|
87 |
+
|
88 |
+
def create_zip_of_files(md_files, mp3_files, wav_files, query=''):
|
89 |
+
all_files = md_files + mp3_files + wav_files
|
90 |
+
if not all_files: return None
|
91 |
+
timestamp = datetime.now().strftime("%y%m_%H%M")
|
92 |
+
zip_name = f"{timestamp}_archive.zip"
|
93 |
+
with zipfile.ZipFile(zip_name, 'w') as z:
|
94 |
+
for f in all_files:
|
95 |
+
z.write(f)
|
96 |
+
return zip_name
|
97 |
+
|
98 |
def display_file_manager_sidebar(groups_sorted):
|
99 |
st.sidebar.title("📚 File Manager")
|
100 |
all_files = {'md': [], 'mp3': [], 'wav': []}
|
|
|
111 |
if st.button(f"🗑️ {ext.upper()}"):
|
112 |
[os.remove(f) for f in files]
|
113 |
st.session_state.should_rerun = True
|
114 |
+
|
115 |
if st.sidebar.button("📦 Zip All"):
|
116 |
+
zip_name = create_zip_of_files(all_files['md'], all_files['mp3'], all_files['wav'])
|
|
|
|
|
117 |
if zip_name:
|
118 |
st.sidebar.markdown(get_download_link(zip_name), unsafe_allow_html=True)
|
119 |
|
120 |
for group_name, files in groups_sorted:
|
121 |
+
try:
|
122 |
+
timestamp = datetime.strptime(group_name, "%y%m_%H%M").strftime("%Y-%m-%d %H:%M") if len(group_name) == 9 and group_name != 'Other' else group_name
|
123 |
+
except ValueError:
|
124 |
+
timestamp = group_name
|
125 |
+
|
126 |
with st.sidebar.expander(f"📁 {timestamp} ({len(files)})", expanded=True):
|
127 |
c1, c2 = st.columns(2)
|
128 |
with c1:
|
|
|
144 |
if st.button("🔄", key=f"loop_{f}"):
|
145 |
audio_b64 = get_cached_audio_b64(f)
|
146 |
st.components.v1.html(
|
147 |
+
f'''<audio id="player_{f}" loop>
|
|
|
148 |
<source src="data:audio/{ext};base64,{audio_b64}">
|
149 |
+
</audio>
|
150 |
+
<script>
|
151 |
document.getElementById("player_{f}").play();
|
152 |
+
</script>''',
|
|
|
153 |
height=0
|
154 |
)
|
155 |
|
|
|
205 |
**marquee_settings,
|
206 |
key=f"paper_{paper['id'] or random.randint(1000,9999)}"
|
207 |
)
|
208 |
+
st.write("")
|
209 |
|
210 |
return papers
|
211 |
|