Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,7 +34,75 @@ st.set_page_config(
|
|
| 34 |
)
|
| 35 |
load_dotenv()
|
| 36 |
|
| 37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
openai_api_key = os.getenv('OPENAI_API_KEY', "")
|
| 39 |
anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', "")
|
| 40 |
xai_key = os.getenv('xai',"")
|
|
@@ -49,13 +117,13 @@ openai_client = OpenAI(api_key=openai.api_key, organization=os.getenv('OPENAI_OR
|
|
| 49 |
HF_KEY = os.getenv('HF_KEY')
|
| 50 |
API_URL = os.getenv('API_URL')
|
| 51 |
|
| 52 |
-
# 📝
|
| 53 |
if 'transcript_history' not in st.session_state:
|
| 54 |
st.session_state['transcript_history'] = []
|
| 55 |
if 'chat_history' not in st.session_state:
|
| 56 |
st.session_state['chat_history'] = []
|
| 57 |
if 'openai_model' not in st.session_state:
|
| 58 |
-
st.session_state['openai_model'] = "gpt-
|
| 59 |
if 'messages' not in st.session_state:
|
| 60 |
st.session_state['messages'] = []
|
| 61 |
if 'last_voice_input' not in st.session_state:
|
|
@@ -66,21 +134,19 @@ if 'edit_new_name' not in st.session_state:
|
|
| 66 |
st.session_state['edit_new_name'] = ""
|
| 67 |
if 'edit_new_content' not in st.session_state:
|
| 68 |
st.session_state['edit_new_content'] = ""
|
| 69 |
-
if 'viewing_prefix' not in st.session_state:
|
| 70 |
st.session_state['viewing_prefix'] = None
|
| 71 |
if 'should_rerun' not in st.session_state:
|
| 72 |
st.session_state['should_rerun'] = False
|
| 73 |
if 'old_val' not in st.session_state:
|
| 74 |
st.session_state['old_val'] = None
|
| 75 |
|
| 76 |
-
# 🎨
|
| 77 |
st.markdown("""
|
| 78 |
<style>
|
| 79 |
.main { background: linear-gradient(to right, #1a1a1a, #2d2d2d); color: #fff; }
|
| 80 |
.stMarkdown { font-family: 'Helvetica Neue', sans-serif; }
|
| 81 |
-
.stButton>button {
|
| 82 |
-
margin-right: 0.5rem;
|
| 83 |
-
}
|
| 84 |
</style>
|
| 85 |
""", unsafe_allow_html=True)
|
| 86 |
|
|
@@ -89,87 +155,37 @@ FILE_EMOJIS = {
|
|
| 89 |
"mp3": "🎵",
|
| 90 |
}
|
| 91 |
|
| 92 |
-
# 🧠
|
| 93 |
def get_high_info_terms(text: str) -> list:
|
| 94 |
"""Extract high-information terms from text, including key phrases."""
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
'should', 'could', 'might', 'must', 'shall', 'can', 'may', 'this', 'that', 'these',
|
| 100 |
-
'those', 'i', 'you', 'he', 'she', 'it', 'we', 'they', 'what', 'which', 'who',
|
| 101 |
-
'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most',
|
| 102 |
-
'other', 'some', 'such', 'than', 'too', 'very', 'just', 'there'
|
| 103 |
-
])
|
| 104 |
-
|
| 105 |
-
key_phrases = [
|
| 106 |
-
'artificial intelligence', 'machine learning', 'deep learning', 'neural network',
|
| 107 |
-
'personal assistant', 'natural language', 'computer vision', 'data science',
|
| 108 |
-
'reinforcement learning', 'knowledge graph', 'semantic search', 'time series',
|
| 109 |
-
'large language model', 'transformer model', 'attention mechanism',
|
| 110 |
-
'autonomous system', 'edge computing', 'quantum computing', 'blockchain technology',
|
| 111 |
-
'cognitive science', 'human computer', 'decision making', 'arxiv search',
|
| 112 |
-
'research paper', 'scientific study', 'empirical analysis'
|
| 113 |
-
]
|
| 114 |
-
|
| 115 |
-
# Identify key phrases
|
| 116 |
-
preserved_phrases = []
|
| 117 |
-
lower_text = text.lower()
|
| 118 |
-
for phrase in key_phrases:
|
| 119 |
-
if phrase in lower_text:
|
| 120 |
-
preserved_phrases.append(phrase)
|
| 121 |
-
text = text.replace(phrase, '')
|
| 122 |
-
|
| 123 |
-
# Extract individual words
|
| 124 |
-
words = re.findall(r'\b\w+(?:-\w+)*\b', text)
|
| 125 |
-
high_info_words = [
|
| 126 |
-
word.lower() for word in words
|
| 127 |
-
if len(word) > 3
|
| 128 |
-
and word.lower() not in stop_words
|
| 129 |
-
and not word.isdigit()
|
| 130 |
-
and any(c.isalpha() for c in word)
|
| 131 |
-
]
|
| 132 |
-
|
| 133 |
-
all_terms = preserved_phrases + high_info_words
|
| 134 |
-
seen = set()
|
| 135 |
-
unique_terms = []
|
| 136 |
-
for term in all_terms:
|
| 137 |
-
if term not in seen:
|
| 138 |
-
seen.add(term)
|
| 139 |
-
unique_terms.append(term)
|
| 140 |
-
|
| 141 |
-
max_terms = 5
|
| 142 |
-
return unique_terms[:max_terms]
|
| 143 |
|
| 144 |
def clean_text_for_filename(text: str) -> str:
|
| 145 |
"""Remove punctuation and short filler words, return a compact string."""
|
| 146 |
-
text =
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
# 📁 6. File Operations
|
| 154 |
def generate_filename(prompt, response, file_type="md"):
|
| 155 |
-
"""
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
prefix = datetime.now().strftime("%y%m_%H%M") + "_"
|
| 160 |
-
combined = (
|
| 161 |
info_terms = get_high_info_terms(combined)
|
| 162 |
|
| 163 |
-
|
| 164 |
-
snippet = (prompt[:100] + " " + response[:100]).strip()
|
| 165 |
snippet_cleaned = clean_text_for_filename(snippet)
|
| 166 |
|
| 167 |
-
# Combine info terms and snippet
|
| 168 |
-
# Prioritize info terms in front
|
| 169 |
name_parts = info_terms + [snippet_cleaned]
|
| 170 |
full_name = '_'.join(name_parts)
|
| 171 |
|
| 172 |
-
# Trim to ~150 chars
|
| 173 |
if len(full_name) > 150:
|
| 174 |
full_name = full_name[:150]
|
| 175 |
|
|
@@ -179,8 +195,12 @@ def generate_filename(prompt, response, file_type="md"):
|
|
| 179 |
def create_file(prompt, response, file_type="md"):
|
| 180 |
"""Create file with intelligent naming"""
|
| 181 |
filename = generate_filename(prompt.strip(), response.strip(), file_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
with open(filename, 'w', encoding='utf-8') as f:
|
| 183 |
-
f.write(
|
| 184 |
return filename
|
| 185 |
|
| 186 |
def get_download_link(file):
|
|
@@ -189,23 +209,21 @@ def get_download_link(file):
|
|
| 189 |
b64 = base64.b64encode(f.read()).decode()
|
| 190 |
return f'<a href="data:file/zip;base64,{b64}" download="{os.path.basename(file)}">📂 Download {os.path.basename(file)}</a>'
|
| 191 |
|
| 192 |
-
# 🔊
|
| 193 |
def clean_for_speech(text: str) -> str:
|
| 194 |
"""Clean text for speech synthesis"""
|
| 195 |
-
text =
|
| 196 |
-
text = text.replace("</s>", " ")
|
| 197 |
-
text = text.replace("#", "")
|
| 198 |
text = re.sub(r"\(https?:\/\/[^\)]+\)", "", text)
|
| 199 |
-
text = re.sub(r"\s+", " ", text).strip()
|
| 200 |
return text
|
| 201 |
|
| 202 |
@st.cache_resource
|
| 203 |
def speech_synthesis_html(result):
|
| 204 |
"""Create HTML for speech synthesis"""
|
|
|
|
| 205 |
html_code = f"""
|
| 206 |
<html><body>
|
| 207 |
<script>
|
| 208 |
-
var msg = new SpeechSynthesisUtterance("{
|
| 209 |
window.speechSynthesis.speak(msg);
|
| 210 |
</script>
|
| 211 |
</body></html>
|
|
@@ -235,95 +253,152 @@ def play_and_download_audio(file_path):
|
|
| 235 |
dl_link = f'<a href="data:audio/mpeg;base64,{base64.b64encode(open(file_path,"rb").read()).decode()}" download="{os.path.basename(file_path)}">Download {os.path.basename(file_path)}</a>'
|
| 236 |
st.markdown(dl_link, unsafe_allow_html=True)
|
| 237 |
|
| 238 |
-
# 🎬
|
| 239 |
def process_image(image_path, user_prompt):
|
| 240 |
"""Process image with GPT-4V"""
|
| 241 |
with open(image_path, "rb") as imgf:
|
| 242 |
image_data = imgf.read()
|
| 243 |
b64img = base64.b64encode(image_data).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
| 244 |
resp = openai_client.chat.completions.create(
|
| 245 |
model=st.session_state["openai_model"],
|
| 246 |
messages=[
|
| 247 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 248 |
{"role": "user", "content": [
|
| 249 |
-
{"type": "text", "text":
|
| 250 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64img}"}}
|
| 251 |
]}
|
| 252 |
],
|
| 253 |
temperature=0.0,
|
| 254 |
)
|
| 255 |
-
return resp.choices[0].message.content
|
| 256 |
|
| 257 |
def process_audio(audio_path):
|
| 258 |
"""Process audio with Whisper"""
|
| 259 |
with open(audio_path, "rb") as f:
|
| 260 |
transcription = openai_client.audio.transcriptions.create(model="whisper-1", file=f)
|
| 261 |
-
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
def process_video(video_path, seconds_per_frame=1):
|
| 265 |
"""Extract frames from video"""
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
fps = vid.get(cv2.CAP_PROP_FPS)
|
| 269 |
-
skip = int(fps*seconds_per_frame)
|
| 270 |
-
frames_b64 = []
|
| 271 |
-
for i in range(0, total, skip):
|
| 272 |
-
vid.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 273 |
-
ret, frame = vid.read()
|
| 274 |
-
if not ret: break
|
| 275 |
-
_, buf = cv2.imencode(".jpg", frame)
|
| 276 |
-
frames_b64.append(base64.b64encode(buf).decode("utf-8"))
|
| 277 |
-
vid.release()
|
| 278 |
-
return frames_b64
|
| 279 |
|
| 280 |
def process_video_with_gpt(video_path, prompt):
|
| 281 |
"""Analyze video frames with GPT-4V"""
|
| 282 |
frames = process_video(video_path)
|
|
|
|
|
|
|
| 283 |
resp = openai_client.chat.completions.create(
|
| 284 |
model=st.session_state["openai_model"],
|
| 285 |
messages=[
|
| 286 |
{"role":"system","content":"Analyze video frames."},
|
| 287 |
{"role":"user","content":[
|
| 288 |
-
{"type":"text","text":
|
| 289 |
-
*[{"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{fr}"}}
|
|
|
|
| 290 |
]}
|
| 291 |
]
|
| 292 |
)
|
| 293 |
-
return resp.choices[0].message.content
|
| 294 |
|
| 295 |
-
# 🤖
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
-
def
|
| 298 |
-
"""
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary=True, full_audio=False):
|
| 302 |
"""Perform Arxiv search and generate audio summaries"""
|
|
|
|
| 303 |
start = time.time()
|
|
|
|
| 304 |
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
| 305 |
-
refs = client.predict(
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
st.markdown(result)
|
| 311 |
-
|
| 312 |
-
# Generate full audio version if requested
|
| 313 |
if full_audio:
|
| 314 |
-
complete_text = f"Complete response for query: {
|
| 315 |
audio_file_full = speak_with_edge_tts(complete_text)
|
| 316 |
st.write("### 📚 Full Audio")
|
| 317 |
play_and_download_audio(audio_file_full)
|
| 318 |
|
| 319 |
if vocal_summary:
|
| 320 |
-
main_text = clean_for_speech(
|
| 321 |
audio_file_main = speak_with_edge_tts(main_text)
|
| 322 |
st.write("### 🎙 Short Audio")
|
| 323 |
play_and_download_audio(audio_file_main)
|
| 324 |
|
| 325 |
if extended_refs:
|
| 326 |
-
summaries_text = "Extended references: " +
|
| 327 |
summaries_text = clean_for_speech(summaries_text)
|
| 328 |
audio_file_refs = speak_with_edge_tts(summaries_text)
|
| 329 |
st.write("### 📜 Long Refs")
|
|
@@ -331,7 +406,7 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary
|
|
| 331 |
|
| 332 |
if titles_summary:
|
| 333 |
titles = []
|
| 334 |
-
for line in
|
| 335 |
m = re.search(r"\[([^\]]+)\]", line)
|
| 336 |
if m:
|
| 337 |
titles.append(m.group(1))
|
|
@@ -342,50 +417,19 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary
|
|
| 342 |
st.write("### 🔖 Titles")
|
| 343 |
play_and_download_audio(audio_file_titles)
|
| 344 |
|
| 345 |
-
elapsed = time.time()-start
|
| 346 |
st.write(f"**Total Elapsed:** {elapsed:.2f} s")
|
| 347 |
|
| 348 |
-
|
| 349 |
-
create_file(q, result, "md")
|
| 350 |
-
|
| 351 |
return result
|
| 352 |
|
| 353 |
-
def
|
| 354 |
-
"""
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
st.markdown(text)
|
| 359 |
-
with st.chat_message("assistant"):
|
| 360 |
-
c = openai_client.chat.completions.create(
|
| 361 |
-
model=st.session_state["openai_model"],
|
| 362 |
-
messages=st.session_state.messages,
|
| 363 |
-
stream=False
|
| 364 |
-
)
|
| 365 |
-
ans = c.choices[0].message.content
|
| 366 |
-
st.write("GPT-4o: " + ans)
|
| 367 |
-
create_file(text, ans, "md")
|
| 368 |
-
st.session_state.messages.append({"role":"assistant","content":ans})
|
| 369 |
-
return ans
|
| 370 |
-
|
| 371 |
-
def process_with_claude(text):
|
| 372 |
-
"""Process text with Claude"""
|
| 373 |
-
if not text: return
|
| 374 |
-
with st.chat_message("user"):
|
| 375 |
-
st.markdown(text)
|
| 376 |
-
with st.chat_message("assistant"):
|
| 377 |
-
r = claude_client.messages.create(
|
| 378 |
-
model="claude-3-sonnet-20240229",
|
| 379 |
-
max_tokens=1000,
|
| 380 |
-
messages=[{"role":"user","content":text}]
|
| 381 |
-
)
|
| 382 |
-
ans = r.content[0].text
|
| 383 |
-
st.write("Claude-3.5: " + ans)
|
| 384 |
-
create_file(text, ans, "md")
|
| 385 |
-
st.session_state.chat_history.append({"user":text,"claude":ans})
|
| 386 |
-
return ans
|
| 387 |
|
| 388 |
-
#
|
| 389 |
def create_zip_of_files(md_files, mp3_files):
|
| 390 |
"""Create zip with intelligent naming"""
|
| 391 |
md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
|
|
@@ -393,12 +437,13 @@ def create_zip_of_files(md_files, mp3_files):
|
|
| 393 |
if not all_files:
|
| 394 |
return None
|
| 395 |
|
| 396 |
-
# Collect content for high-info term extraction
|
| 397 |
all_content = []
|
| 398 |
for f in all_files:
|
| 399 |
if f.endswith('.md'):
|
| 400 |
with open(f, 'r', encoding='utf-8') as file:
|
| 401 |
-
|
|
|
|
|
|
|
| 402 |
elif f.endswith('.mp3'):
|
| 403 |
all_content.append(os.path.basename(f))
|
| 404 |
|
|
@@ -409,7 +454,7 @@ def create_zip_of_files(md_files, mp3_files):
|
|
| 409 |
name_text = '_'.join(term.replace(' ', '-') for term in info_terms[:3])
|
| 410 |
zip_name = f"{timestamp}_{name_text}.zip"
|
| 411 |
|
| 412 |
-
with zipfile.ZipFile(zip_name,'w') as z:
|
| 413 |
for f in all_files:
|
| 414 |
z.write(f)
|
| 415 |
|
|
@@ -442,8 +487,10 @@ def extract_keywords_from_md(files):
|
|
| 442 |
text = ""
|
| 443 |
for f in files:
|
| 444 |
if f.endswith(".md"):
|
| 445 |
-
|
| 446 |
-
|
|
|
|
|
|
|
| 447 |
return get_high_info_terms(text)
|
| 448 |
|
| 449 |
def display_file_manager_sidebar(groups, sorted_prefixes):
|
|
@@ -474,14 +521,14 @@ def display_file_manager_sidebar(groups, sorted_prefixes):
|
|
| 474 |
if st.button("⬇️ ZipAll"):
|
| 475 |
z = create_zip_of_files(all_md, all_mp3)
|
| 476 |
if z:
|
| 477 |
-
st.sidebar.markdown(get_download_link(z),unsafe_allow_html=True)
|
| 478 |
|
| 479 |
for prefix in sorted_prefixes:
|
| 480 |
files = groups[prefix]
|
| 481 |
kw = extract_keywords_from_md(files)
|
| 482 |
keywords_str = " ".join(kw) if kw else "No Keywords"
|
| 483 |
with st.sidebar.expander(f"{prefix} Files ({len(files)}) - KW: {keywords_str}", expanded=True):
|
| 484 |
-
c1,c2 = st.columns(2)
|
| 485 |
with c1:
|
| 486 |
if st.button("👀ViewGrp", key="view_group_"+prefix):
|
| 487 |
st.session_state.viewing_prefix = prefix
|
|
@@ -497,25 +544,25 @@ def display_file_manager_sidebar(groups, sorted_prefixes):
|
|
| 497 |
ctime = datetime.fromtimestamp(os.path.getmtime(f)).strftime("%Y-%m-%d %H:%M:%S")
|
| 498 |
st.write(f"**{fname}** - {ctime}")
|
| 499 |
|
| 500 |
-
# 🎯
|
| 501 |
def main():
|
| 502 |
st.sidebar.markdown("### 🚲BikeAI🏆 Multi-Agent Research")
|
| 503 |
-
tab_main = st.radio("Action:",["🎤 Voice","📸 Media","🔍 ArXiv","📝 Editor"],horizontal=True)
|
| 504 |
|
| 505 |
mycomponent = components.declare_component("mycomponent", path="mycomponent")
|
| 506 |
val = mycomponent(my_input_value="Hello")
|
| 507 |
|
| 508 |
# Show input in a text box for editing if detected
|
| 509 |
if val:
|
| 510 |
-
|
| 511 |
-
edited_input = st.text_area("✏️ Edit Input:", value=
|
| 512 |
run_option = st.selectbox("Model:", ["Arxiv", "GPT-4o", "Claude-3.5"])
|
| 513 |
col1, col2 = st.columns(2)
|
| 514 |
with col1:
|
| 515 |
autorun = st.checkbox("⚙ AutoRun", value=True)
|
| 516 |
with col2:
|
| 517 |
full_audio = st.checkbox("📚FullAudio", value=False,
|
| 518 |
-
|
| 519 |
|
| 520 |
input_changed = (val != st.session_state.old_val)
|
| 521 |
|
|
@@ -523,7 +570,7 @@ def main():
|
|
| 523 |
st.session_state.old_val = val
|
| 524 |
if run_option == "Arxiv":
|
| 525 |
perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
|
| 526 |
-
|
| 527 |
else:
|
| 528 |
if run_option == "GPT-4o":
|
| 529 |
process_with_gpt(edited_input)
|
|
@@ -534,7 +581,7 @@ def main():
|
|
| 534 |
st.session_state.old_val = val
|
| 535 |
if run_option == "Arxiv":
|
| 536 |
perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
|
| 537 |
-
|
| 538 |
else:
|
| 539 |
if run_option == "GPT-4o":
|
| 540 |
process_with_gpt(edited_input)
|
|
@@ -544,62 +591,70 @@ def main():
|
|
| 544 |
if tab_main == "🔍 ArXiv":
|
| 545 |
st.subheader("🔍 Query ArXiv")
|
| 546 |
q = st.text_input("🔍 Query:")
|
|
|
|
| 547 |
|
| 548 |
st.markdown("### 🎛 Options")
|
| 549 |
vocal_summary = st.checkbox("🎙ShortAudio", value=True)
|
| 550 |
extended_refs = st.checkbox("📜LongRefs", value=False)
|
| 551 |
titles_summary = st.checkbox("🔖TitlesOnly", value=True)
|
| 552 |
full_audio = st.checkbox("📚FullAudio", value=False,
|
| 553 |
-
|
| 554 |
full_transcript = st.checkbox("🧾FullTranscript", value=False,
|
| 555 |
-
|
| 556 |
|
| 557 |
if q and st.button("🔍Run"):
|
| 558 |
-
result = perform_ai_lookup(q, vocal_summary=vocal_summary,
|
| 559 |
-
|
|
|
|
|
|
|
| 560 |
if full_transcript:
|
| 561 |
save_full_transcript(q, result)
|
| 562 |
|
| 563 |
st.markdown("### Change Prompt & Re-Run")
|
| 564 |
q_new = st.text_input("🔄 Modify Query:")
|
|
|
|
| 565 |
if q_new and st.button("🔄 Re-Run with Modified Query"):
|
| 566 |
-
result = perform_ai_lookup(q_new, vocal_summary=vocal_summary,
|
| 567 |
-
|
|
|
|
|
|
|
| 568 |
if full_transcript:
|
| 569 |
save_full_transcript(q_new, result)
|
| 570 |
|
| 571 |
-
|
| 572 |
elif tab_main == "🎤 Voice":
|
| 573 |
st.subheader("🎤 Voice Input")
|
| 574 |
user_text = st.text_area("💬 Message:", height=100)
|
| 575 |
-
user_text =
|
| 576 |
if st.button("📨 Send"):
|
| 577 |
process_with_gpt(user_text)
|
| 578 |
st.subheader("📜 Chat History")
|
| 579 |
-
t1,t2=st.tabs(["Claude History","GPT-4o History"])
|
| 580 |
with t1:
|
| 581 |
for c in st.session_state.chat_history:
|
| 582 |
-
st.write("**You:**", c["user"])
|
| 583 |
-
st.write("**Claude:**", c["claude"])
|
| 584 |
with t2:
|
| 585 |
for m in st.session_state.messages:
|
| 586 |
with st.chat_message(m["role"]):
|
| 587 |
-
|
|
|
|
|
|
|
|
|
|
| 588 |
|
| 589 |
elif tab_main == "📸 Media":
|
| 590 |
st.header("📸 Images & 🎥 Videos")
|
| 591 |
tabs = st.tabs(["🖼 Images", "🎥 Video"])
|
| 592 |
with tabs[0]:
|
| 593 |
-
imgs = glob.glob("*.png")+glob.glob("*.jpg")
|
| 594 |
if imgs:
|
| 595 |
-
c = st.slider("Cols",1,5,3)
|
| 596 |
cols = st.columns(c)
|
| 597 |
-
for i,f in enumerate(imgs):
|
| 598 |
with cols[i%c]:
|
| 599 |
-
st.image(Image.open(f),use_container_width=True)
|
| 600 |
if st.button(f"👀 Analyze {os.path.basename(f)}", key=f"analyze_{f}"):
|
| 601 |
-
a = process_image(f,"Describe this image.")
|
| 602 |
-
st.markdown(a)
|
| 603 |
else:
|
| 604 |
st.write("No images found.")
|
| 605 |
with tabs[1]:
|
|
@@ -609,18 +664,22 @@ def main():
|
|
| 609 |
with st.expander(f"🎥 {os.path.basename(v)}"):
|
| 610 |
st.video(v)
|
| 611 |
if st.button(f"Analyze {os.path.basename(v)}", key=f"analyze_{v}"):
|
| 612 |
-
a = process_video_with_gpt(v,"Describe video.")
|
| 613 |
-
st.markdown(a)
|
| 614 |
else:
|
| 615 |
st.write("No videos found.")
|
| 616 |
|
| 617 |
elif tab_main == "📝 Editor":
|
| 618 |
-
if getattr(st.session_state,'current_file',None):
|
| 619 |
st.subheader(f"Editing: {st.session_state.current_file}")
|
| 620 |
-
|
|
|
|
|
|
|
|
|
|
| 621 |
if st.button("💾 Save"):
|
| 622 |
-
|
| 623 |
-
|
|
|
|
| 624 |
st.success("Updated!")
|
| 625 |
st.session_state.should_rerun = True
|
| 626 |
else:
|
|
@@ -637,8 +696,9 @@ def main():
|
|
| 637 |
ext = os.path.splitext(fname)[1].lower().strip('.')
|
| 638 |
st.write(f"### {fname}")
|
| 639 |
if ext == "md":
|
| 640 |
-
|
| 641 |
-
|
|
|
|
| 642 |
elif ext == "mp3":
|
| 643 |
st.audio(f)
|
| 644 |
else:
|
|
@@ -650,5 +710,5 @@ def main():
|
|
| 650 |
st.session_state.should_rerun = False
|
| 651 |
st.rerun()
|
| 652 |
|
| 653 |
-
if __name__=="__main__":
|
| 654 |
-
main()
|
|
|
|
| 34 |
)
|
| 35 |
load_dotenv()
|
| 36 |
|
| 37 |
+
# 🧠 2. Text Cleaning Functionality
|
| 38 |
+
class TextCleaner:
|
| 39 |
+
"""Helper class for text cleaning operations"""
|
| 40 |
+
def __init__(self):
|
| 41 |
+
self.replacements = {
|
| 42 |
+
"\\n": " ", # Replace escaped newlines
|
| 43 |
+
"</s>": "", # Remove end tags
|
| 44 |
+
"<s>": "", # Remove start tags
|
| 45 |
+
"\n": " ", # Replace actual newlines
|
| 46 |
+
"\r": " ", # Replace carriage returns
|
| 47 |
+
"\t": " ", # Replace tabs
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
self.preserve_replacements = {
|
| 51 |
+
"\\n": "\n", # Convert escaped to actual newlines
|
| 52 |
+
"</s>": "", # Remove end tags
|
| 53 |
+
"<s>": "", # Remove start tags
|
| 54 |
+
"\r": "\n", # Convert returns to newlines
|
| 55 |
+
"\t": " " # Convert tabs to spaces
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
def clean_text(self, text: str, preserve_format: bool = False) -> str:
|
| 59 |
+
"""
|
| 60 |
+
Clean text removing problematic characters and normalizing whitespace.
|
| 61 |
+
Args:
|
| 62 |
+
text: Text to clean
|
| 63 |
+
preserve_format: Whether to preserve some formatting (newlines etc)
|
| 64 |
+
Returns:
|
| 65 |
+
Cleaned text string
|
| 66 |
+
"""
|
| 67 |
+
if not text or not isinstance(text, str):
|
| 68 |
+
return ""
|
| 69 |
+
|
| 70 |
+
replacements = (self.preserve_replacements if preserve_format
|
| 71 |
+
else self.replacements)
|
| 72 |
+
|
| 73 |
+
cleaned = text
|
| 74 |
+
for old, new in replacements.items():
|
| 75 |
+
cleaned = cleaned.replace(old, new)
|
| 76 |
+
|
| 77 |
+
# Normalize whitespace while preserving paragraphs if needed
|
| 78 |
+
if preserve_format:
|
| 79 |
+
cleaned = re.sub(r'\n{3,}', '\n\n', cleaned)
|
| 80 |
+
else:
|
| 81 |
+
cleaned = re.sub(r'\s+', ' ', cleaned)
|
| 82 |
+
|
| 83 |
+
return cleaned.strip()
|
| 84 |
+
|
| 85 |
+
def clean_dict(self, data: dict, fields: list) -> dict:
|
| 86 |
+
"""Clean specified fields in a dictionary"""
|
| 87 |
+
if not data or not isinstance(data, dict):
|
| 88 |
+
return {}
|
| 89 |
+
|
| 90 |
+
cleaned = data.copy()
|
| 91 |
+
for field in fields:
|
| 92 |
+
if field in cleaned:
|
| 93 |
+
cleaned[field] = self.clean_text(cleaned[field])
|
| 94 |
+
return cleaned
|
| 95 |
+
|
| 96 |
+
def clean_list(self, items: list, fields: list) -> list:
|
| 97 |
+
"""Clean specified fields in a list of dictionaries"""
|
| 98 |
+
if not isinstance(items, list):
|
| 99 |
+
return []
|
| 100 |
+
return [self.clean_dict(item, fields) for item in items]
|
| 101 |
+
|
| 102 |
+
# Initialize cleaner
|
| 103 |
+
cleaner = TextCleaner()
|
| 104 |
+
|
| 105 |
+
# 🔑 3. API Setup & Clients
|
| 106 |
openai_api_key = os.getenv('OPENAI_API_KEY', "")
|
| 107 |
anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', "")
|
| 108 |
xai_key = os.getenv('xai',"")
|
|
|
|
| 117 |
HF_KEY = os.getenv('HF_KEY')
|
| 118 |
API_URL = os.getenv('API_URL')
|
| 119 |
|
| 120 |
+
# 📝 4. Session State Management
|
| 121 |
if 'transcript_history' not in st.session_state:
|
| 122 |
st.session_state['transcript_history'] = []
|
| 123 |
if 'chat_history' not in st.session_state:
|
| 124 |
st.session_state['chat_history'] = []
|
| 125 |
if 'openai_model' not in st.session_state:
|
| 126 |
+
st.session_state['openai_model'] = "gpt-4-1106-preview"
|
| 127 |
if 'messages' not in st.session_state:
|
| 128 |
st.session_state['messages'] = []
|
| 129 |
if 'last_voice_input' not in st.session_state:
|
|
|
|
| 134 |
st.session_state['edit_new_name'] = ""
|
| 135 |
if 'edit_new_content' not in st.session_state:
|
| 136 |
st.session_state['edit_new_content'] = ""
|
| 137 |
+
if 'viewing_prefix' not in st.session_state:
|
| 138 |
st.session_state['viewing_prefix'] = None
|
| 139 |
if 'should_rerun' not in st.session_state:
|
| 140 |
st.session_state['should_rerun'] = False
|
| 141 |
if 'old_val' not in st.session_state:
|
| 142 |
st.session_state['old_val'] = None
|
| 143 |
|
| 144 |
+
# 🎨 5. Custom CSS
|
| 145 |
st.markdown("""
|
| 146 |
<style>
|
| 147 |
.main { background: linear-gradient(to right, #1a1a1a, #2d2d2d); color: #fff; }
|
| 148 |
.stMarkdown { font-family: 'Helvetica Neue', sans-serif; }
|
| 149 |
+
.stButton>button { margin-right: 0.5rem; }
|
|
|
|
|
|
|
| 150 |
</style>
|
| 151 |
""", unsafe_allow_html=True)
|
| 152 |
|
|
|
|
| 155 |
"mp3": "🎵",
|
| 156 |
}
|
| 157 |
|
| 158 |
+
# 🧠 6. High-Information Content Extraction
|
| 159 |
def get_high_info_terms(text: str) -> list:
|
| 160 |
"""Extract high-information terms from text, including key phrases."""
|
| 161 |
+
text = cleaner.clean_text(text)
|
| 162 |
+
|
| 163 |
+
# ... rest of function remains the same ...
|
| 164 |
+
[Your existing get_high_info_terms implementation]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
def clean_text_for_filename(text: str) -> str:
|
| 167 |
"""Remove punctuation and short filler words, return a compact string."""
|
| 168 |
+
text = cleaner.clean_text(text)
|
| 169 |
+
|
| 170 |
+
# ... rest of function remains the same ...
|
| 171 |
+
[Your existing clean_text_for_filename implementation]
|
| 172 |
+
|
| 173 |
+
# 📁 7. File Operations
|
|
|
|
|
|
|
| 174 |
def generate_filename(prompt, response, file_type="md"):
|
| 175 |
+
"""Generate filename with meaningful terms."""
|
| 176 |
+
cleaned_prompt = cleaner.clean_text(prompt)
|
| 177 |
+
cleaned_response = cleaner.clean_text(response)
|
| 178 |
+
|
| 179 |
prefix = datetime.now().strftime("%y%m_%H%M") + "_"
|
| 180 |
+
combined = (cleaned_prompt + " " + cleaned_response).strip()
|
| 181 |
info_terms = get_high_info_terms(combined)
|
| 182 |
|
| 183 |
+
snippet = (cleaned_prompt[:100] + " " + cleaned_response[:100]).strip()
|
|
|
|
| 184 |
snippet_cleaned = clean_text_for_filename(snippet)
|
| 185 |
|
|
|
|
|
|
|
| 186 |
name_parts = info_terms + [snippet_cleaned]
|
| 187 |
full_name = '_'.join(name_parts)
|
| 188 |
|
|
|
|
| 189 |
if len(full_name) > 150:
|
| 190 |
full_name = full_name[:150]
|
| 191 |
|
|
|
|
| 195 |
def create_file(prompt, response, file_type="md"):
|
| 196 |
"""Create file with intelligent naming"""
|
| 197 |
filename = generate_filename(prompt.strip(), response.strip(), file_type)
|
| 198 |
+
|
| 199 |
+
cleaned_prompt = cleaner.clean_text(prompt)
|
| 200 |
+
cleaned_response = cleaner.clean_text(response, preserve_format=True)
|
| 201 |
+
|
| 202 |
with open(filename, 'w', encoding='utf-8') as f:
|
| 203 |
+
f.write(cleaned_prompt + "\n\n" + cleaned_response)
|
| 204 |
return filename
|
| 205 |
|
| 206 |
def get_download_link(file):
|
|
|
|
| 209 |
b64 = base64.b64encode(f.read()).decode()
|
| 210 |
return f'<a href="data:file/zip;base64,{b64}" download="{os.path.basename(file)}">📂 Download {os.path.basename(file)}</a>'
|
| 211 |
|
| 212 |
+
# 🔊 8. Audio Processing
|
| 213 |
def clean_for_speech(text: str) -> str:
|
| 214 |
"""Clean text for speech synthesis"""
|
| 215 |
+
text = cleaner.clean_text(text)
|
|
|
|
|
|
|
| 216 |
text = re.sub(r"\(https?:\/\/[^\)]+\)", "", text)
|
|
|
|
| 217 |
return text
|
| 218 |
|
| 219 |
@st.cache_resource
|
| 220 |
def speech_synthesis_html(result):
|
| 221 |
"""Create HTML for speech synthesis"""
|
| 222 |
+
cleaned_result = clean_for_speech(result)
|
| 223 |
html_code = f"""
|
| 224 |
<html><body>
|
| 225 |
<script>
|
| 226 |
+
var msg = new SpeechSynthesisUtterance("{cleaned_result.replace('"', '')}");
|
| 227 |
window.speechSynthesis.speak(msg);
|
| 228 |
</script>
|
| 229 |
</body></html>
|
|
|
|
| 253 |
dl_link = f'<a href="data:audio/mpeg;base64,{base64.b64encode(open(file_path,"rb").read()).decode()}" download="{os.path.basename(file_path)}">Download {os.path.basename(file_path)}</a>'
|
| 254 |
st.markdown(dl_link, unsafe_allow_html=True)
|
| 255 |
|
| 256 |
+
# 🎬 9. Media Processing
|
| 257 |
def process_image(image_path, user_prompt):
|
| 258 |
"""Process image with GPT-4V"""
|
| 259 |
with open(image_path, "rb") as imgf:
|
| 260 |
image_data = imgf.read()
|
| 261 |
b64img = base64.b64encode(image_data).decode("utf-8")
|
| 262 |
+
|
| 263 |
+
cleaned_prompt = cleaner.clean_text(user_prompt)
|
| 264 |
+
|
| 265 |
resp = openai_client.chat.completions.create(
|
| 266 |
model=st.session_state["openai_model"],
|
| 267 |
messages=[
|
| 268 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 269 |
{"role": "user", "content": [
|
| 270 |
+
{"type": "text", "text": cleaned_prompt},
|
| 271 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64img}"}}
|
| 272 |
]}
|
| 273 |
],
|
| 274 |
temperature=0.0,
|
| 275 |
)
|
| 276 |
+
return cleaner.clean_text(resp.choices[0].message.content, preserve_format=True)
|
| 277 |
|
| 278 |
def process_audio(audio_path):
|
| 279 |
"""Process audio with Whisper"""
|
| 280 |
with open(audio_path, "rb") as f:
|
| 281 |
transcription = openai_client.audio.transcriptions.create(model="whisper-1", file=f)
|
| 282 |
+
|
| 283 |
+
cleaned_text = cleaner.clean_text(transcription.text)
|
| 284 |
+
st.session_state.messages.append({
|
| 285 |
+
"role": "user",
|
| 286 |
+
"content": cleaned_text
|
| 287 |
+
})
|
| 288 |
+
return cleaned_text
|
| 289 |
|
| 290 |
def process_video(video_path, seconds_per_frame=1):
|
| 291 |
"""Extract frames from video"""
|
| 292 |
+
# ... function remains the same as it handles binary data ...
|
| 293 |
+
[Your existing process_video implementation]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
def process_video_with_gpt(video_path, prompt):
|
| 296 |
"""Analyze video frames with GPT-4V"""
|
| 297 |
frames = process_video(video_path)
|
| 298 |
+
cleaned_prompt = cleaner.clean_text(prompt)
|
| 299 |
+
|
| 300 |
resp = openai_client.chat.completions.create(
|
| 301 |
model=st.session_state["openai_model"],
|
| 302 |
messages=[
|
| 303 |
{"role":"system","content":"Analyze video frames."},
|
| 304 |
{"role":"user","content":[
|
| 305 |
+
{"type":"text","text":cleaned_prompt},
|
| 306 |
+
*[{"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{fr}"}}
|
| 307 |
+
for fr in frames]
|
| 308 |
]}
|
| 309 |
]
|
| 310 |
)
|
| 311 |
+
return cleaner.clean_text(resp.choices[0].message.content, preserve_format=True)
|
| 312 |
|
| 313 |
+
# 🤖 10. AI Model Integration
|
| 314 |
+
def process_with_claude(text):
|
| 315 |
+
"""Process text with Claude"""
|
| 316 |
+
if not text: return
|
| 317 |
+
|
| 318 |
+
cleaned_input = cleaner.clean_text(text)
|
| 319 |
+
with st.chat_message("user"):
|
| 320 |
+
st.markdown(cleaned_input)
|
| 321 |
+
|
| 322 |
+
with st.chat_message("assistant"):
|
| 323 |
+
r = claude_client.messages.create(
|
| 324 |
+
model="claude-3-sonnet-20240229",
|
| 325 |
+
max_tokens=1000,
|
| 326 |
+
messages=[{"role":"user","content":cleaned_input}]
|
| 327 |
+
)
|
| 328 |
+
raw_response = r.content[0].text
|
| 329 |
+
cleaned_response = cleaner.clean_text(raw_response, preserve_format=True)
|
| 330 |
+
|
| 331 |
+
st.write("Claude-3.5: " + cleaned_response)
|
| 332 |
+
create_file(cleaned_input, cleaned_response, "md")
|
| 333 |
+
st.session_state.chat_history.append({
|
| 334 |
+
"user": cleaned_input,
|
| 335 |
+
"claude": cleaned_response
|
| 336 |
+
})
|
| 337 |
+
return cleaned_response
|
| 338 |
|
| 339 |
+
def process_with_gpt(text):
|
| 340 |
+
"""Process text with GPT-4"""
|
| 341 |
+
if not text: return
|
| 342 |
+
|
| 343 |
+
cleaned_input = cleaner.clean_text(text)
|
| 344 |
+
st.session_state.messages.append({
|
| 345 |
+
"role": "user",
|
| 346 |
+
"content": cleaned_input
|
| 347 |
+
})
|
| 348 |
+
|
| 349 |
+
with st.chat_message("user"):
|
| 350 |
+
st.markdown(cleaned_input)
|
| 351 |
+
|
| 352 |
+
with st.chat_message("assistant"):
|
| 353 |
+
c = openai_client.chat.completions.create(
|
| 354 |
+
model=st.session_state["openai_model"],
|
| 355 |
+
messages=st.session_state.messages,
|
| 356 |
+
stream=False
|
| 357 |
+
)
|
| 358 |
+
raw_response = c.choices[0].message.content
|
| 359 |
+
cleaned_response = cleaner.clean_text(raw_response, preserve_format=True)
|
| 360 |
+
|
| 361 |
+
st.write("GPT-4o: " + cleaned_response)
|
| 362 |
+
create_file(cleaned_input, cleaned_response, "md")
|
| 363 |
+
st.session_state.messages.append({
|
| 364 |
+
"role": "assistant",
|
| 365 |
+
"content": cleaned_response
|
| 366 |
+
})
|
| 367 |
+
return cleaned_response
|
| 368 |
|
| 369 |
def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary=True, full_audio=False):
|
| 370 |
"""Perform Arxiv search and generate audio summaries"""
|
| 371 |
+
cleaned_query = cleaner.clean_text(q)
|
| 372 |
start = time.time()
|
| 373 |
+
|
| 374 |
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
| 375 |
+
refs = client.predict(cleaned_query, 20, "Semantic Search",
|
| 376 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 377 |
+
api_name="/update_with_rag_md")[0]
|
| 378 |
+
r2 = client.predict(cleaned_query, "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 379 |
+
True, api_name="/ask_llm")
|
| 380 |
+
|
| 381 |
+
# Clean responses
|
| 382 |
+
cleaned_r2 = cleaner.clean_text(r2, preserve_format=True)
|
| 383 |
+
cleaned_refs = cleaner.clean_text(refs, preserve_format=True)
|
| 384 |
+
|
| 385 |
+
result = f"### 🔎 {cleaned_query}\n\n{cleaned_r2}\n\n{cleaned_refs}"
|
| 386 |
st.markdown(result)
|
| 387 |
+
|
|
|
|
| 388 |
if full_audio:
|
| 389 |
+
complete_text = f"Complete response for query: {cleaned_query}. {clean_for_speech(cleaned_r2)} {clean_for_speech(cleaned_refs)}"
|
| 390 |
audio_file_full = speak_with_edge_tts(complete_text)
|
| 391 |
st.write("### 📚 Full Audio")
|
| 392 |
play_and_download_audio(audio_file_full)
|
| 393 |
|
| 394 |
if vocal_summary:
|
| 395 |
+
main_text = clean_for_speech(cleaned_r2)
|
| 396 |
audio_file_main = speak_with_edge_tts(main_text)
|
| 397 |
st.write("### 🎙 Short Audio")
|
| 398 |
play_and_download_audio(audio_file_main)
|
| 399 |
|
| 400 |
if extended_refs:
|
| 401 |
+
summaries_text = "Extended references: " + cleaned_refs.replace('"','')
|
| 402 |
summaries_text = clean_for_speech(summaries_text)
|
| 403 |
audio_file_refs = speak_with_edge_tts(summaries_text)
|
| 404 |
st.write("### 📜 Long Refs")
|
|
|
|
| 406 |
|
| 407 |
if titles_summary:
|
| 408 |
titles = []
|
| 409 |
+
for line in cleaned_refs.split('\n'):
|
| 410 |
m = re.search(r"\[([^\]]+)\]", line)
|
| 411 |
if m:
|
| 412 |
titles.append(m.group(1))
|
|
|
|
| 417 |
st.write("### 🔖 Titles")
|
| 418 |
play_and_download_audio(audio_file_titles)
|
| 419 |
|
| 420 |
+
elapsed = time.time() - start
|
| 421 |
st.write(f"**Total Elapsed:** {elapsed:.2f} s")
|
| 422 |
|
| 423 |
+
create_file(cleaned_query, result, "md")
|
|
|
|
|
|
|
| 424 |
return result
|
| 425 |
|
| 426 |
+
def save_full_transcript(query, text):
|
| 427 |
+
"""Save full transcript of results as a file."""
|
| 428 |
+
cleaned_query = cleaner.clean_text(query)
|
| 429 |
+
cleaned_text = cleaner.clean_text(text, preserve_format=True)
|
| 430 |
+
create_file(cleaned_query, cleaned_text, "md")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
+
# 📂 11. File Management
|
| 433 |
def create_zip_of_files(md_files, mp3_files):
|
| 434 |
"""Create zip with intelligent naming"""
|
| 435 |
md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
|
|
|
|
| 437 |
if not all_files:
|
| 438 |
return None
|
| 439 |
|
|
|
|
| 440 |
all_content = []
|
| 441 |
for f in all_files:
|
| 442 |
if f.endswith('.md'):
|
| 443 |
with open(f, 'r', encoding='utf-8') as file:
|
| 444 |
+
content = file.read()
|
| 445 |
+
cleaned_content = cleaner.clean_text(content)
|
| 446 |
+
all_content.append(cleaned_content)
|
| 447 |
elif f.endswith('.mp3'):
|
| 448 |
all_content.append(os.path.basename(f))
|
| 449 |
|
|
|
|
| 454 |
name_text = '_'.join(term.replace(' ', '-') for term in info_terms[:3])
|
| 455 |
zip_name = f"{timestamp}_{name_text}.zip"
|
| 456 |
|
| 457 |
+
with zipfile.ZipFile(zip_name, 'w') as z:
|
| 458 |
for f in all_files:
|
| 459 |
z.write(f)
|
| 460 |
|
|
|
|
| 487 |
text = ""
|
| 488 |
for f in files:
|
| 489 |
if f.endswith(".md"):
|
| 490 |
+
with open(f, 'r', encoding='utf-8') as file:
|
| 491 |
+
content = file.read()
|
| 492 |
+
cleaned_content = cleaner.clean_text(content)
|
| 493 |
+
text += " " + cleaned_content
|
| 494 |
return get_high_info_terms(text)
|
| 495 |
|
| 496 |
def display_file_manager_sidebar(groups, sorted_prefixes):
|
|
|
|
| 521 |
if st.button("⬇️ ZipAll"):
|
| 522 |
z = create_zip_of_files(all_md, all_mp3)
|
| 523 |
if z:
|
| 524 |
+
st.sidebar.markdown(get_download_link(z), unsafe_allow_html=True)
|
| 525 |
|
| 526 |
for prefix in sorted_prefixes:
|
| 527 |
files = groups[prefix]
|
| 528 |
kw = extract_keywords_from_md(files)
|
| 529 |
keywords_str = " ".join(kw) if kw else "No Keywords"
|
| 530 |
with st.sidebar.expander(f"{prefix} Files ({len(files)}) - KW: {keywords_str}", expanded=True):
|
| 531 |
+
c1, c2 = st.columns(2)
|
| 532 |
with c1:
|
| 533 |
if st.button("👀ViewGrp", key="view_group_"+prefix):
|
| 534 |
st.session_state.viewing_prefix = prefix
|
|
|
|
| 544 |
ctime = datetime.fromtimestamp(os.path.getmtime(f)).strftime("%Y-%m-%d %H:%M:%S")
|
| 545 |
st.write(f"**{fname}** - {ctime}")
|
| 546 |
|
| 547 |
+
# 🎯 12. Main Application
|
| 548 |
def main():
|
| 549 |
st.sidebar.markdown("### 🚲BikeAI🏆 Multi-Agent Research")
|
| 550 |
+
tab_main = st.radio("Action:", ["🎤 Voice", "📸 Media", "🔍 ArXiv", "📝 Editor"], horizontal=True)
|
| 551 |
|
| 552 |
mycomponent = components.declare_component("mycomponent", path="mycomponent")
|
| 553 |
val = mycomponent(my_input_value="Hello")
|
| 554 |
|
| 555 |
# Show input in a text box for editing if detected
|
| 556 |
if val:
|
| 557 |
+
cleaned_val = cleaner.clean_text(val)
|
| 558 |
+
edited_input = st.text_area("✏️ Edit Input:", value=cleaned_val, height=100)
|
| 559 |
run_option = st.selectbox("Model:", ["Arxiv", "GPT-4o", "Claude-3.5"])
|
| 560 |
col1, col2 = st.columns(2)
|
| 561 |
with col1:
|
| 562 |
autorun = st.checkbox("⚙ AutoRun", value=True)
|
| 563 |
with col2:
|
| 564 |
full_audio = st.checkbox("📚FullAudio", value=False,
|
| 565 |
+
help="Generate full audio response")
|
| 566 |
|
| 567 |
input_changed = (val != st.session_state.old_val)
|
| 568 |
|
|
|
|
| 570 |
st.session_state.old_val = val
|
| 571 |
if run_option == "Arxiv":
|
| 572 |
perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
|
| 573 |
+
titles_summary=True, full_audio=full_audio)
|
| 574 |
else:
|
| 575 |
if run_option == "GPT-4o":
|
| 576 |
process_with_gpt(edited_input)
|
|
|
|
| 581 |
st.session_state.old_val = val
|
| 582 |
if run_option == "Arxiv":
|
| 583 |
perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
|
| 584 |
+
titles_summary=True, full_audio=full_audio)
|
| 585 |
else:
|
| 586 |
if run_option == "GPT-4o":
|
| 587 |
process_with_gpt(edited_input)
|
|
|
|
| 591 |
if tab_main == "🔍 ArXiv":
|
| 592 |
st.subheader("🔍 Query ArXiv")
|
| 593 |
q = st.text_input("🔍 Query:")
|
| 594 |
+
q = cleaner.clean_text(q)
|
| 595 |
|
| 596 |
st.markdown("### 🎛 Options")
|
| 597 |
vocal_summary = st.checkbox("🎙ShortAudio", value=True)
|
| 598 |
extended_refs = st.checkbox("📜LongRefs", value=False)
|
| 599 |
titles_summary = st.checkbox("🔖TitlesOnly", value=True)
|
| 600 |
full_audio = st.checkbox("📚FullAudio", value=False,
|
| 601 |
+
help="Generate full audio response")
|
| 602 |
full_transcript = st.checkbox("🧾FullTranscript", value=False,
|
| 603 |
+
help="Generate a full transcript file")
|
| 604 |
|
| 605 |
if q and st.button("🔍Run"):
|
| 606 |
+
result = perform_ai_lookup(q, vocal_summary=vocal_summary,
|
| 607 |
+
extended_refs=extended_refs,
|
| 608 |
+
titles_summary=titles_summary,
|
| 609 |
+
full_audio=full_audio)
|
| 610 |
if full_transcript:
|
| 611 |
save_full_transcript(q, result)
|
| 612 |
|
| 613 |
st.markdown("### Change Prompt & Re-Run")
|
| 614 |
q_new = st.text_input("🔄 Modify Query:")
|
| 615 |
+
q_new = cleaner.clean_text(q_new)
|
| 616 |
if q_new and st.button("🔄 Re-Run with Modified Query"):
|
| 617 |
+
result = perform_ai_lookup(q_new, vocal_summary=vocal_summary,
|
| 618 |
+
extended_refs=extended_refs,
|
| 619 |
+
titles_summary=titles_summary,
|
| 620 |
+
full_audio=full_audio)
|
| 621 |
if full_transcript:
|
| 622 |
save_full_transcript(q_new, result)
|
| 623 |
|
|
|
|
| 624 |
elif tab_main == "🎤 Voice":
|
| 625 |
st.subheader("🎤 Voice Input")
|
| 626 |
user_text = st.text_area("💬 Message:", height=100)
|
| 627 |
+
user_text = cleaner.clean_text(user_text)
|
| 628 |
if st.button("📨 Send"):
|
| 629 |
process_with_gpt(user_text)
|
| 630 |
st.subheader("📜 Chat History")
|
| 631 |
+
t1, t2 = st.tabs(["Claude History", "GPT-4o History"])
|
| 632 |
with t1:
|
| 633 |
for c in st.session_state.chat_history:
|
| 634 |
+
st.write("**You:**", cleaner.clean_text(c["user"]))
|
| 635 |
+
st.write("**Claude:**", cleaner.clean_text(c["claude"], preserve_format=True))
|
| 636 |
with t2:
|
| 637 |
for m in st.session_state.messages:
|
| 638 |
with st.chat_message(m["role"]):
|
| 639 |
+
if m["role"] == "user":
|
| 640 |
+
st.markdown(cleaner.clean_text(m["content"]))
|
| 641 |
+
else:
|
| 642 |
+
st.markdown(cleaner.clean_text(m["content"], preserve_format=True))
|
| 643 |
|
| 644 |
elif tab_main == "📸 Media":
|
| 645 |
st.header("📸 Images & 🎥 Videos")
|
| 646 |
tabs = st.tabs(["🖼 Images", "🎥 Video"])
|
| 647 |
with tabs[0]:
|
| 648 |
+
imgs = glob.glob("*.png") + glob.glob("*.jpg")
|
| 649 |
if imgs:
|
| 650 |
+
c = st.slider("Cols", 1, 5, 3)
|
| 651 |
cols = st.columns(c)
|
| 652 |
+
for i, f in enumerate(imgs):
|
| 653 |
with cols[i%c]:
|
| 654 |
+
st.image(Image.open(f), use_container_width=True)
|
| 655 |
if st.button(f"👀 Analyze {os.path.basename(f)}", key=f"analyze_{f}"):
|
| 656 |
+
a = process_image(f, "Describe this image.")
|
| 657 |
+
st.markdown(cleaner.clean_text(a, preserve_format=True))
|
| 658 |
else:
|
| 659 |
st.write("No images found.")
|
| 660 |
with tabs[1]:
|
|
|
|
| 664 |
with st.expander(f"🎥 {os.path.basename(v)}"):
|
| 665 |
st.video(v)
|
| 666 |
if st.button(f"Analyze {os.path.basename(v)}", key=f"analyze_{v}"):
|
| 667 |
+
a = process_video_with_gpt(v, "Describe video.")
|
| 668 |
+
st.markdown(cleaner.clean_text(a, preserve_format=True))
|
| 669 |
else:
|
| 670 |
st.write("No videos found.")
|
| 671 |
|
| 672 |
elif tab_main == "📝 Editor":
|
| 673 |
+
if getattr(st.session_state, 'current_file', None):
|
| 674 |
st.subheader(f"Editing: {st.session_state.current_file}")
|
| 675 |
+
with open(st.session_state.current_file, 'r', encoding='utf-8') as f:
|
| 676 |
+
content = f.read()
|
| 677 |
+
content = cleaner.clean_text(content, preserve_format=True)
|
| 678 |
+
new_text = st.text_area("✏️ Content:", content, height=300)
|
| 679 |
if st.button("💾 Save"):
|
| 680 |
+
cleaned_content = cleaner.clean_text(new_text, preserve_format=True)
|
| 681 |
+
with open(st.session_state.current_file, 'w', encoding='utf-8') as f:
|
| 682 |
+
f.write(cleaned_content)
|
| 683 |
st.success("Updated!")
|
| 684 |
st.session_state.should_rerun = True
|
| 685 |
else:
|
|
|
|
| 696 |
ext = os.path.splitext(fname)[1].lower().strip('.')
|
| 697 |
st.write(f"### {fname}")
|
| 698 |
if ext == "md":
|
| 699 |
+
with open(f, 'r', encoding='utf-8') as file:
|
| 700 |
+
content = file.read()
|
| 701 |
+
st.markdown(cleaner.clean_text(content, preserve_format=True))
|
| 702 |
elif ext == "mp3":
|
| 703 |
st.audio(f)
|
| 704 |
else:
|
|
|
|
| 710 |
st.session_state.should_rerun = False
|
| 711 |
st.rerun()
|
| 712 |
|
| 713 |
+
if __name__ == "__main__":
|
| 714 |
+
main()
|