Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import streamlit.components.v1 as components
|
|
| 5 |
from datetime import datetime
|
| 6 |
from audio_recorder_streamlit import audio_recorder
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
-
from collections import defaultdict, deque
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from gradio_client import Client
|
| 11 |
from huggingface_hub import InferenceClient
|
|
@@ -19,6 +19,7 @@ import extra_streamlit_components as stx
|
|
| 19 |
from streamlit.runtime.scriptrunner import get_script_run_ctx
|
| 20 |
import asyncio
|
| 21 |
import edge_tts
|
|
|
|
| 22 |
|
| 23 |
# 🎯 1. Core Configuration & Setup
|
| 24 |
st.set_page_config(
|
|
@@ -50,7 +51,8 @@ EDGE_TTS_VOICES = [
|
|
| 50 |
# Add this to your session state initialization section:
|
| 51 |
if 'tts_voice' not in st.session_state:
|
| 52 |
st.session_state['tts_voice'] = EDGE_TTS_VOICES[0] # Default voice
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
# 🔑 2. API Setup & Clients
|
| 56 |
openai_api_key = os.getenv('OPENAI_API_KEY', "")
|
|
@@ -90,6 +92,8 @@ if 'should_rerun' not in st.session_state:
|
|
| 90 |
st.session_state['should_rerun'] = False
|
| 91 |
if 'old_val' not in st.session_state:
|
| 92 |
st.session_state['old_val'] = None
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# 🎨 4. Custom CSS
|
| 95 |
st.markdown("""
|
|
@@ -105,10 +109,11 @@ st.markdown("""
|
|
| 105 |
FILE_EMOJIS = {
|
| 106 |
"md": "📝",
|
| 107 |
"mp3": "🎵",
|
|
|
|
| 108 |
}
|
| 109 |
|
| 110 |
# 🧠 5. High-Information Content Extraction
|
| 111 |
-
def get_high_info_terms(text: str) -> list:
|
| 112 |
"""Extract high-information terms from text, including key phrases."""
|
| 113 |
stop_words = set([
|
| 114 |
'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with',
|
|
@@ -130,35 +135,23 @@ def get_high_info_terms(text: str) -> list:
|
|
| 130 |
'research paper', 'scientific study', 'empirical analysis'
|
| 131 |
]
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
#
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
word.lower() for word in words
|
| 145 |
-
if len(word) > 3
|
| 146 |
-
and word.lower() not in stop_words
|
| 147 |
-
and not word.isdigit()
|
| 148 |
-
and any(c.isalpha() for c in word)
|
| 149 |
]
|
| 150 |
|
| 151 |
-
#
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
for term in all_terms:
|
| 156 |
-
if term not in seen:
|
| 157 |
-
seen.add(term)
|
| 158 |
-
unique_terms.append(term)
|
| 159 |
-
|
| 160 |
-
max_terms = 5
|
| 161 |
-
return unique_terms[:max_terms]
|
| 162 |
|
| 163 |
def clean_text_for_filename(text: str) -> str:
|
| 164 |
"""Remove punctuation and short filler words, return a compact string."""
|
|
@@ -177,7 +170,7 @@ def generate_filename(prompt, response, file_type="md"):
|
|
| 177 |
"""
|
| 178 |
prefix = datetime.now().strftime("%y%m_%H%M") + "_"
|
| 179 |
combined = (prompt + " " + response).strip()
|
| 180 |
-
info_terms = get_high_info_terms(combined)
|
| 181 |
|
| 182 |
# Include a short snippet from prompt and response
|
| 183 |
snippet = (prompt[:100] + " " + response[:100]).strip()
|
|
@@ -201,11 +194,20 @@ def create_file(prompt, response, file_type="md"):
|
|
| 201 |
f.write(prompt + "\n\n" + response)
|
| 202 |
return filename
|
| 203 |
|
| 204 |
-
def get_download_link(file):
|
| 205 |
"""Generate download link for file"""
|
| 206 |
with open(file, "rb") as f:
|
| 207 |
b64 = base64.b64encode(f.read()).decode()
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
# 🔊 7. Audio Processing
|
| 211 |
def clean_for_speech(text: str) -> str:
|
|
@@ -230,7 +232,7 @@ def speech_synthesis_html(result):
|
|
| 230 |
"""
|
| 231 |
components.html(html_code, height=0)
|
| 232 |
|
| 233 |
-
async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0):
|
| 234 |
"""Generate audio using Edge TTS"""
|
| 235 |
text = clean_for_speech(text)
|
| 236 |
if not text.strip():
|
|
@@ -238,19 +240,44 @@ async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=
|
|
| 238 |
rate_str = f"{rate:+d}%"
|
| 239 |
pitch_str = f"{pitch:+d}Hz"
|
| 240 |
communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
-
def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0):
|
| 246 |
"""Wrapper for edge TTS generation"""
|
| 247 |
-
return asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch))
|
| 248 |
|
| 249 |
-
def play_and_download_audio(file_path):
|
| 250 |
-
"""Play and provide download link for audio"""
|
| 251 |
if file_path and os.path.exists(file_path):
|
| 252 |
-
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
| 254 |
st.markdown(dl_link, unsafe_allow_html=True)
|
| 255 |
|
| 256 |
# 🎬 8. Media Processing
|
|
@@ -317,8 +344,6 @@ def save_full_transcript(query, text):
|
|
| 317 |
"""Save full transcript of Arxiv results as a file."""
|
| 318 |
create_file(query, text, "md")
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
| 322 |
def parse_arxiv_refs(ref_text: str):
|
| 323 |
"""
|
| 324 |
Parse papers by finding lines with two pipe characters as title lines.
|
|
@@ -379,29 +404,46 @@ def parse_arxiv_refs(ref_text: str):
|
|
| 379 |
|
| 380 |
return results[:20] # Ensure we return maximum 20 papers
|
| 381 |
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
def create_paper_audio_files(papers):
|
| 385 |
"""
|
| 386 |
Create audio files for each paper's content and add file paths to paper dict.
|
| 387 |
-
|
| 388 |
"""
|
|
|
|
|
|
|
|
|
|
| 389 |
for paper in papers:
|
| 390 |
try:
|
| 391 |
# Generate audio for full content only
|
| 392 |
full_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
|
| 393 |
full_text = clean_for_speech(full_text)
|
| 394 |
-
|
|
|
|
|
|
|
| 395 |
paper['full_audio'] = full_file
|
| 396 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
except Exception as e:
|
| 398 |
st.warning(f"Error generating audio for paper {paper['title']}: {str(e)}")
|
| 399 |
paper['full_audio'] = None
|
| 400 |
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
def display_papers(papers):
|
| 403 |
"""
|
| 404 |
-
Display papers with their audio controls using URLs as unique keys.
|
| 405 |
"""
|
| 406 |
st.write("## Research Papers")
|
| 407 |
|
|
@@ -411,15 +453,18 @@ def display_papers(papers):
|
|
| 411 |
st.markdown(f"*{paper['authors']}*")
|
| 412 |
st.markdown(paper['summary'])
|
| 413 |
|
| 414 |
-
# Single audio control for full content
|
| 415 |
if paper.get('full_audio'):
|
| 416 |
-
st.write("📚 Paper Audio")
|
| 417 |
-
|
| 418 |
-
|
|
|
|
|
|
|
|
|
|
| 419 |
|
| 420 |
def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
|
| 421 |
titles_summary=True, full_audio=False):
|
| 422 |
-
"""Perform Arxiv search with audio generation per paper."""
|
| 423 |
start = time.time()
|
| 424 |
|
| 425 |
# Query the HF RAG pipeline
|
|
@@ -437,7 +482,7 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
|
|
| 437 |
# Parse and process papers
|
| 438 |
papers = parse_arxiv_refs(refs)
|
| 439 |
if papers:
|
| 440 |
-
create_paper_audio_files(papers)
|
| 441 |
display_papers(papers)
|
| 442 |
else:
|
| 443 |
st.warning("No papers found in the response.")
|
|
@@ -449,9 +494,6 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
|
|
| 449 |
create_file(q, result, "md")
|
| 450 |
return result
|
| 451 |
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
def process_with_gpt(text):
|
| 456 |
"""Process text with GPT-4"""
|
| 457 |
if not text:
|
|
@@ -490,10 +532,11 @@ def process_with_claude(text):
|
|
| 490 |
return ans
|
| 491 |
|
| 492 |
# 📂 10. File Management
|
| 493 |
-
def create_zip_of_files(md_files, mp3_files):
|
| 494 |
-
"""Create zip with intelligent naming"""
|
|
|
|
| 495 |
md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
|
| 496 |
-
all_files = md_files + mp3_files
|
| 497 |
if not all_files:
|
| 498 |
return None
|
| 499 |
|
|
@@ -504,13 +547,24 @@ def create_zip_of_files(md_files, mp3_files):
|
|
| 504 |
with open(f, 'r', encoding='utf-8') as file:
|
| 505 |
all_content.append(file.read())
|
| 506 |
elif f.endswith('.mp3'):
|
| 507 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
|
| 509 |
combined_content = " ".join(all_content)
|
| 510 |
-
info_terms = get_high_info_terms(combined_content)
|
| 511 |
|
| 512 |
timestamp = datetime.now().strftime("%y%m_%H%M")
|
| 513 |
-
name_text = '_'.join(term.replace(' ', '-') for term in info_terms[:
|
| 514 |
zip_name = f"{timestamp}_{name_text}.zip"
|
| 515 |
|
| 516 |
with zipfile.ZipFile(zip_name,'w') as z:
|
|
@@ -523,23 +577,23 @@ def load_files_for_sidebar():
|
|
| 523 |
"""Load and group files for sidebar display"""
|
| 524 |
md_files = glob.glob("*.md")
|
| 525 |
mp3_files = glob.glob("*.mp3")
|
|
|
|
| 526 |
|
| 527 |
md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
|
| 528 |
-
all_files = md_files + mp3_files
|
| 529 |
|
| 530 |
groups = defaultdict(list)
|
| 531 |
for f in all_files:
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
|
|
|
|
|
|
|
|
|
| 535 |
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
sorted_prefixes = sorted(groups.keys(),
|
| 540 |
-
key=lambda pre: max(os.path.getmtime(x) for x in groups[pre]),
|
| 541 |
-
reverse=True)
|
| 542 |
-
return groups, sorted_prefixes
|
| 543 |
|
| 544 |
def extract_keywords_from_md(files):
|
| 545 |
"""Extract keywords from markdown files"""
|
|
@@ -548,22 +602,25 @@ def extract_keywords_from_md(files):
|
|
| 548 |
if f.endswith(".md"):
|
| 549 |
c = open(f,'r',encoding='utf-8').read()
|
| 550 |
text += " " + c
|
| 551 |
-
return get_high_info_terms(text)
|
| 552 |
|
| 553 |
-
def display_file_manager_sidebar(
|
| 554 |
"""Display file manager in sidebar"""
|
| 555 |
st.sidebar.title("🎵 Audio & Docs Manager")
|
| 556 |
|
| 557 |
all_md = []
|
| 558 |
all_mp3 = []
|
| 559 |
-
|
| 560 |
-
|
|
|
|
| 561 |
if f.endswith(".md"):
|
| 562 |
all_md.append(f)
|
| 563 |
elif f.endswith(".mp3"):
|
| 564 |
all_mp3.append(f)
|
|
|
|
|
|
|
| 565 |
|
| 566 |
-
top_bar = st.sidebar.columns(
|
| 567 |
with top_bar[0]:
|
| 568 |
if st.button("🗑 DelAllMD"):
|
| 569 |
for f in all_md:
|
|
@@ -575,25 +632,28 @@ def display_file_manager_sidebar(groups, sorted_prefixes):
|
|
| 575 |
os.remove(f)
|
| 576 |
st.session_state.should_rerun = True
|
| 577 |
with top_bar[2]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
if st.button("⬇️ ZipAll"):
|
| 579 |
-
|
| 580 |
-
if
|
| 581 |
-
st.sidebar.markdown(get_download_link(
|
| 582 |
-
|
| 583 |
-
for
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
keywords_str = " ".join(kw) if kw else "No Keywords"
|
| 587 |
-
with st.sidebar.expander(f"{prefix} Files ({len(files)}) - KW: {keywords_str}", expanded=True):
|
| 588 |
c1,c2 = st.columns(2)
|
| 589 |
with c1:
|
| 590 |
-
if st.button("👀ViewGrp", key="view_group_"+
|
| 591 |
-
st.session_state.viewing_prefix =
|
| 592 |
with c2:
|
| 593 |
-
if st.button("🗑DelGrp", key="del_group_"+
|
| 594 |
for f in files:
|
| 595 |
os.remove(f)
|
| 596 |
-
st.success(f"Deleted group {
|
| 597 |
st.session_state.should_rerun = True
|
| 598 |
|
| 599 |
for f in files:
|
|
@@ -613,11 +673,21 @@ def main():
|
|
| 613 |
index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
|
| 614 |
)
|
| 615 |
|
| 616 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
if selected_voice != st.session_state['tts_voice']:
|
| 618 |
st.session_state['tts_voice'] = selected_voice
|
| 619 |
st.rerun()
|
| 620 |
-
|
|
|
|
|
|
|
| 621 |
|
| 622 |
tab_main = st.radio("Action:",["🎤 Voice","📸 Media","🔍 ArXiv","📝 Editor"],horizontal=True)
|
| 623 |
|
|
@@ -642,6 +712,7 @@ def main():
|
|
| 642 |
|
| 643 |
if autorun and input_changed:
|
| 644 |
st.session_state.old_val = val
|
|
|
|
| 645 |
if run_option == "Arxiv":
|
| 646 |
perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
|
| 647 |
titles_summary=True, full_audio=full_audio)
|
|
@@ -653,6 +724,7 @@ def main():
|
|
| 653 |
else:
|
| 654 |
if st.button("▶ Run"):
|
| 655 |
st.session_state.old_val = val
|
|
|
|
| 656 |
if run_option == "Arxiv":
|
| 657 |
perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
|
| 658 |
titles_summary=True, full_audio=full_audio)
|
|
@@ -676,6 +748,7 @@ def main():
|
|
| 676 |
help="Generate a full transcript file")
|
| 677 |
|
| 678 |
if q and st.button("🔍Run"):
|
|
|
|
| 679 |
result = perform_ai_lookup(q, vocal_summary=vocal_summary, extended_refs=extended_refs,
|
| 680 |
titles_summary=titles_summary, full_audio=full_audio)
|
| 681 |
if full_transcript:
|
|
@@ -684,6 +757,7 @@ def main():
|
|
| 684 |
st.markdown("### Change Prompt & Re-Run")
|
| 685 |
q_new = st.text_input("🔄 Modify Query:")
|
| 686 |
if q_new and st.button("🔄 Re-Run with Modified Query"):
|
|
|
|
| 687 |
result = perform_ai_lookup(q_new, vocal_summary=vocal_summary, extended_refs=extended_refs,
|
| 688 |
titles_summary=titles_summary, full_audio=full_audio)
|
| 689 |
if full_transcript:
|
|
@@ -746,23 +820,29 @@ def main():
|
|
| 746 |
else:
|
| 747 |
st.write("Select a file from the sidebar to edit.")
|
| 748 |
|
| 749 |
-
|
| 750 |
-
|
|
|
|
| 751 |
|
| 752 |
-
if st.session_state.viewing_prefix and st.session_state.viewing_prefix in
|
| 753 |
st.write("---")
|
| 754 |
st.write(f"**Viewing Group:** {st.session_state.viewing_prefix}")
|
| 755 |
-
for
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 766 |
if st.button("❌ Close"):
|
| 767 |
st.session_state.viewing_prefix = None
|
| 768 |
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
from audio_recorder_streamlit import audio_recorder
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
+
from collections import defaultdict, deque, Counter
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from gradio_client import Client
|
| 11 |
from huggingface_hub import InferenceClient
|
|
|
|
| 19 |
from streamlit.runtime.scriptrunner import get_script_run_ctx
|
| 20 |
import asyncio
|
| 21 |
import edge_tts
|
| 22 |
+
import moviepy.editor as mp # 🆕 Import moviepy for MP4 generation
|
| 23 |
|
| 24 |
# 🎯 1. Core Configuration & Setup
|
| 25 |
st.set_page_config(
|
|
|
|
| 51 |
# Add this to your session state initialization section:
|
| 52 |
if 'tts_voice' not in st.session_state:
|
| 53 |
st.session_state['tts_voice'] = EDGE_TTS_VOICES[0] # Default voice
|
| 54 |
+
if 'audio_format' not in st.session_state:
|
| 55 |
+
st.session_state['audio_format'] = 'mp3' # 🆕 Default audio format
|
| 56 |
|
| 57 |
# 🔑 2. API Setup & Clients
|
| 58 |
openai_api_key = os.getenv('OPENAI_API_KEY', "")
|
|
|
|
| 92 |
st.session_state['should_rerun'] = False
|
| 93 |
if 'old_val' not in st.session_state:
|
| 94 |
st.session_state['old_val'] = None
|
| 95 |
+
if 'last_query' not in st.session_state:
|
| 96 |
+
st.session_state['last_query'] = "" # 🆕 Store the last query for zip naming
|
| 97 |
|
| 98 |
# 🎨 4. Custom CSS
|
| 99 |
st.markdown("""
|
|
|
|
| 109 |
FILE_EMOJIS = {
|
| 110 |
"md": "📝",
|
| 111 |
"mp3": "🎵",
|
| 112 |
+
"mp4": "🎬" # 🆕 Add emoji for MP4
|
| 113 |
}
|
| 114 |
|
| 115 |
# 🧠 5. High-Information Content Extraction
|
| 116 |
+
def get_high_info_terms(text: str, top_n=10) -> list:
|
| 117 |
"""Extract high-information terms from text, including key phrases."""
|
| 118 |
stop_words = set([
|
| 119 |
'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with',
|
|
|
|
| 135 |
'research paper', 'scientific study', 'empirical analysis'
|
| 136 |
]
|
| 137 |
|
| 138 |
+
# Extract bi-grams and uni-grams
|
| 139 |
+
words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
|
| 140 |
+
bi_grams = [' '.join(pair) for pair in zip(words, words[1:])]
|
| 141 |
+
combined = words + bi_grams
|
| 142 |
+
|
| 143 |
+
# Filter out stop words and short words
|
| 144 |
+
filtered = [
|
| 145 |
+
term for term in combined
|
| 146 |
+
if term not in stop_words
|
| 147 |
+
and len(term.split()) <= 2 # Limit to uni-grams and bi-grams
|
| 148 |
+
and any(c.isalpha() for c in term)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
]
|
| 150 |
|
| 151 |
+
# Count frequencies
|
| 152 |
+
counter = Counter(filtered)
|
| 153 |
+
most_common = [term for term, freq in counter.most_common(top_n)]
|
| 154 |
+
return most_common
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
def clean_text_for_filename(text: str) -> str:
|
| 157 |
"""Remove punctuation and short filler words, return a compact string."""
|
|
|
|
| 170 |
"""
|
| 171 |
prefix = datetime.now().strftime("%y%m_%H%M") + "_"
|
| 172 |
combined = (prompt + " " + response).strip()
|
| 173 |
+
info_terms = get_high_info_terms(combined, top_n=10)
|
| 174 |
|
| 175 |
# Include a short snippet from prompt and response
|
| 176 |
snippet = (prompt[:100] + " " + response[:100]).strip()
|
|
|
|
| 194 |
f.write(prompt + "\n\n" + response)
|
| 195 |
return filename
|
| 196 |
|
| 197 |
+
def get_download_link(file, file_type="zip"):
|
| 198 |
"""Generate download link for file"""
|
| 199 |
with open(file, "rb") as f:
|
| 200 |
b64 = base64.b64encode(f.read()).decode()
|
| 201 |
+
if file_type == "zip":
|
| 202 |
+
return f'<a href="data:application/zip;base64,{b64}" download="{os.path.basename(file)}">📂 Download {os.path.basename(file)}</a>'
|
| 203 |
+
elif file_type == "mp3":
|
| 204 |
+
return f'<a href="data:audio/mpeg;base64,{b64}" download="{os.path.basename(file)}">🎵 Download {os.path.basename(file)}</a>'
|
| 205 |
+
elif file_type == "mp4":
|
| 206 |
+
return f'<a href="data:video/mp4;base64,{b64}" download="{os.path.basename(file)}">🎬 Download {os.path.basename(file)}</a>' # 🆕 MP4 download link
|
| 207 |
+
elif file_type == "md":
|
| 208 |
+
return f'<a href="data:text/markdown;base64,{b64}" download="{os.path.basename(file)}">📝 Download {os.path.basename(file)}</a>'
|
| 209 |
+
else:
|
| 210 |
+
return f'<a href="data:application/octet-stream;base64,{b64}" download="{os.path.basename(file)}">Download {os.path.basename(file)}</a>'
|
| 211 |
|
| 212 |
# 🔊 7. Audio Processing
|
| 213 |
def clean_for_speech(text: str) -> str:
|
|
|
|
| 232 |
"""
|
| 233 |
components.html(html_code, height=0)
|
| 234 |
|
| 235 |
+
async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
|
| 236 |
"""Generate audio using Edge TTS"""
|
| 237 |
text = clean_for_speech(text)
|
| 238 |
if not text.strip():
|
|
|
|
| 240 |
rate_str = f"{rate:+d}%"
|
| 241 |
pitch_str = f"{pitch:+d}Hz"
|
| 242 |
communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
|
| 243 |
+
if file_format == "mp3":
|
| 244 |
+
out_fn = generate_filename(text, text, "mp3")
|
| 245 |
+
await communicate.save(out_fn)
|
| 246 |
+
return out_fn
|
| 247 |
+
elif file_format == "mp4":
|
| 248 |
+
# Generate MP3 first
|
| 249 |
+
mp3_filename = generate_filename(text, text, "mp3")
|
| 250 |
+
await communicate.save(mp3_filename)
|
| 251 |
+
|
| 252 |
+
# Create MP4 by combining MP3 with a placeholder image
|
| 253 |
+
placeholder_image = "placeholder.jpg" # 🆕 Ensure this image exists in your directory
|
| 254 |
+
if not os.path.exists(placeholder_image):
|
| 255 |
+
st.error(f"Placeholder image '{placeholder_image}' not found. Please add it to the directory.")
|
| 256 |
+
return mp3_filename # Return MP3 if image not found
|
| 257 |
+
|
| 258 |
+
video_filename = os.path.splitext(mp3_filename)[0] + ".mp4"
|
| 259 |
+
try:
|
| 260 |
+
audio_clip = mp.AudioFileClip(mp3_filename)
|
| 261 |
+
image_clip = mp.ImageClip(placeholder_image).set_duration(audio_clip.duration)
|
| 262 |
+
video_clip = image_clip.set_audio(audio_clip)
|
| 263 |
+
video_clip.write_videofile(video_filename, codec="libx264", audio_codec="aac", verbose=False, logger=None)
|
| 264 |
+
return video_filename
|
| 265 |
+
except Exception as e:
|
| 266 |
+
st.warning(f"Error generating MP4 for {mp3_filename}: {str(e)}")
|
| 267 |
+
return mp3_filename # Return MP3 if MP4 generation fails
|
| 268 |
|
| 269 |
+
def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
|
| 270 |
"""Wrapper for edge TTS generation"""
|
| 271 |
+
return asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch, file_format))
|
| 272 |
|
| 273 |
+
def play_and_download_audio(file_path, file_type="mp3"):
|
| 274 |
+
"""Play and provide download link for audio/video"""
|
| 275 |
if file_path and os.path.exists(file_path):
|
| 276 |
+
if file_type in ["mp3", "wav"]:
|
| 277 |
+
st.audio(file_path)
|
| 278 |
+
elif file_type == "mp4":
|
| 279 |
+
st.video(file_path) # 🆕 Use st.video for MP4 files
|
| 280 |
+
dl_link = get_download_link(file_path, file_type=file_type)
|
| 281 |
st.markdown(dl_link, unsafe_allow_html=True)
|
| 282 |
|
| 283 |
# 🎬 8. Media Processing
|
|
|
|
| 344 |
"""Save full transcript of Arxiv results as a file."""
|
| 345 |
create_file(query, text, "md")
|
| 346 |
|
|
|
|
|
|
|
| 347 |
def parse_arxiv_refs(ref_text: str):
|
| 348 |
"""
|
| 349 |
Parse papers by finding lines with two pipe characters as title lines.
|
|
|
|
| 404 |
|
| 405 |
return results[:20] # Ensure we return maximum 20 papers
|
| 406 |
|
| 407 |
+
def create_paper_audio_files(papers, input_question):
|
|
|
|
|
|
|
| 408 |
"""
|
| 409 |
Create audio files for each paper's content and add file paths to paper dict.
|
| 410 |
+
Also, display each audio as it's generated.
|
| 411 |
"""
|
| 412 |
+
# Collect all content for combined summary
|
| 413 |
+
combined_titles = []
|
| 414 |
+
|
| 415 |
for paper in papers:
|
| 416 |
try:
|
| 417 |
# Generate audio for full content only
|
| 418 |
full_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
|
| 419 |
full_text = clean_for_speech(full_text)
|
| 420 |
+
# Determine file format based on user selection
|
| 421 |
+
file_format = st.session_state['audio_format']
|
| 422 |
+
full_file = speak_with_edge_tts(full_text, voice=st.session_state['tts_voice'], file_format=file_format)
|
| 423 |
paper['full_audio'] = full_file
|
| 424 |
|
| 425 |
+
# Display the audio/video immediately after generation
|
| 426 |
+
st.write(f"### {FILE_EMOJIS.get(file_format, '')} {os.path.basename(full_file)}")
|
| 427 |
+
play_and_download_audio(full_file, file_type=file_format)
|
| 428 |
+
|
| 429 |
+
combined_titles.append(paper['title'])
|
| 430 |
+
|
| 431 |
except Exception as e:
|
| 432 |
st.warning(f"Error generating audio for paper {paper['title']}: {str(e)}")
|
| 433 |
paper['full_audio'] = None
|
| 434 |
|
| 435 |
+
# After all individual audios, create a combined summary audio/video
|
| 436 |
+
if combined_titles:
|
| 437 |
+
combined_text = f"Here are the titles of the papers related to your query: {'; '.join(combined_titles)}. Your original question was: {input_question}"
|
| 438 |
+
file_format = st.session_state['audio_format']
|
| 439 |
+
combined_file = speak_with_edge_tts(combined_text, voice=st.session_state['tts_voice'], file_format=file_format)
|
| 440 |
+
st.write(f"### {FILE_EMOJIS.get(file_format, '')} Combined Summary {'Video' if file_format=='mp4' else 'Audio'}")
|
| 441 |
+
play_and_download_audio(combined_file, file_type=file_format)
|
| 442 |
+
papers.append({'title': 'Combined Summary', 'full_audio': combined_file})
|
| 443 |
+
|
| 444 |
def display_papers(papers):
|
| 445 |
"""
|
| 446 |
+
Display papers with their audio/video controls using URLs as unique keys.
|
| 447 |
"""
|
| 448 |
st.write("## Research Papers")
|
| 449 |
|
|
|
|
| 453 |
st.markdown(f"*{paper['authors']}*")
|
| 454 |
st.markdown(paper['summary'])
|
| 455 |
|
| 456 |
+
# Single audio/video control for full content
|
| 457 |
if paper.get('full_audio'):
|
| 458 |
+
st.write("📚 Paper Audio/Video")
|
| 459 |
+
file_ext = os.path.splitext(paper['full_audio'])[1].lower()
|
| 460 |
+
if file_ext == ".mp3":
|
| 461 |
+
st.audio(paper['full_audio'])
|
| 462 |
+
elif file_ext == ".mp4":
|
| 463 |
+
st.video(paper['full_audio'])
|
| 464 |
|
| 465 |
def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
|
| 466 |
titles_summary=True, full_audio=False):
|
| 467 |
+
"""Perform Arxiv search with audio/video generation per paper."""
|
| 468 |
start = time.time()
|
| 469 |
|
| 470 |
# Query the HF RAG pipeline
|
|
|
|
| 482 |
# Parse and process papers
|
| 483 |
papers = parse_arxiv_refs(refs)
|
| 484 |
if papers:
|
| 485 |
+
create_paper_audio_files(papers, input_question=q)
|
| 486 |
display_papers(papers)
|
| 487 |
else:
|
| 488 |
st.warning("No papers found in the response.")
|
|
|
|
| 494 |
create_file(q, result, "md")
|
| 495 |
return result
|
| 496 |
|
|
|
|
|
|
|
|
|
|
| 497 |
def process_with_gpt(text):
|
| 498 |
"""Process text with GPT-4"""
|
| 499 |
if not text:
|
|
|
|
| 532 |
return ans
|
| 533 |
|
| 534 |
# 📂 10. File Management
|
| 535 |
+
def create_zip_of_files(md_files, mp3_files, mp4_files, input_question):
|
| 536 |
+
"""Create zip with intelligent naming based on top 10 common words."""
|
| 537 |
+
# Exclude 'readme.md'
|
| 538 |
md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
|
| 539 |
+
all_files = md_files + mp3_files + mp4_files
|
| 540 |
if not all_files:
|
| 541 |
return None
|
| 542 |
|
|
|
|
| 547 |
with open(f, 'r', encoding='utf-8') as file:
|
| 548 |
all_content.append(file.read())
|
| 549 |
elif f.endswith('.mp3'):
|
| 550 |
+
# Replace underscores with spaces and extract basename without extension
|
| 551 |
+
basename = os.path.splitext(os.path.basename(f))[0]
|
| 552 |
+
words = basename.replace('_', ' ')
|
| 553 |
+
all_content.append(words)
|
| 554 |
+
elif f.endswith('.mp4'):
|
| 555 |
+
# Replace underscores with spaces and extract basename without extension
|
| 556 |
+
basename = os.path.splitext(os.path.basename(f))[0]
|
| 557 |
+
words = basename.replace('_', ' ')
|
| 558 |
+
all_content.append(words)
|
| 559 |
+
|
| 560 |
+
# Include the input question
|
| 561 |
+
all_content.append(input_question)
|
| 562 |
|
| 563 |
combined_content = " ".join(all_content)
|
| 564 |
+
info_terms = get_high_info_terms(combined_content, top_n=10)
|
| 565 |
|
| 566 |
timestamp = datetime.now().strftime("%y%m_%H%M")
|
| 567 |
+
name_text = '_'.join(term.replace(' ', '-') for term in info_terms[:10])
|
| 568 |
zip_name = f"{timestamp}_{name_text}.zip"
|
| 569 |
|
| 570 |
with zipfile.ZipFile(zip_name,'w') as z:
|
|
|
|
| 577 |
"""Load and group files for sidebar display"""
|
| 578 |
md_files = glob.glob("*.md")
|
| 579 |
mp3_files = glob.glob("*.mp3")
|
| 580 |
+
mp4_files = glob.glob("*.mp4") # 🆕 Load MP4 files
|
| 581 |
|
| 582 |
md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
|
| 583 |
+
all_files = md_files + mp3_files + mp4_files
|
| 584 |
|
| 585 |
groups = defaultdict(list)
|
| 586 |
for f in all_files:
|
| 587 |
+
# Treat underscores as spaces and split into words
|
| 588 |
+
words = os.path.basename(f).replace('_', ' ').split()
|
| 589 |
+
# Extract keywords from filename
|
| 590 |
+
keywords = get_high_info_terms(' '.join(words), top_n=5)
|
| 591 |
+
group_name = '_'.join(keywords) if keywords else 'Miscellaneous'
|
| 592 |
+
groups[group_name].append(f)
|
| 593 |
|
| 594 |
+
# Sort groups based on latest file modification time
|
| 595 |
+
sorted_groups = sorted(groups.items(), key=lambda x: max(os.path.getmtime(f) for f in x[1]), reverse=True)
|
| 596 |
+
return sorted_groups
|
|
|
|
|
|
|
|
|
|
|
|
|
| 597 |
|
| 598 |
def extract_keywords_from_md(files):
|
| 599 |
"""Extract keywords from markdown files"""
|
|
|
|
| 602 |
if f.endswith(".md"):
|
| 603 |
c = open(f,'r',encoding='utf-8').read()
|
| 604 |
text += " " + c
|
| 605 |
+
return get_high_info_terms(text, top_n=5)
|
| 606 |
|
| 607 |
+
def display_file_manager_sidebar(groups_sorted):
|
| 608 |
"""Display file manager in sidebar"""
|
| 609 |
st.sidebar.title("🎵 Audio & Docs Manager")
|
| 610 |
|
| 611 |
all_md = []
|
| 612 |
all_mp3 = []
|
| 613 |
+
all_mp4 = [] # 🆕 List to hold MP4 files
|
| 614 |
+
for group_name, files in groups_sorted:
|
| 615 |
+
for f in files:
|
| 616 |
if f.endswith(".md"):
|
| 617 |
all_md.append(f)
|
| 618 |
elif f.endswith(".mp3"):
|
| 619 |
all_mp3.append(f)
|
| 620 |
+
elif f.endswith(".mp4"):
|
| 621 |
+
all_mp4.append(f) # 🆕 Append MP4 files
|
| 622 |
|
| 623 |
+
top_bar = st.sidebar.columns(4) # 🆕 Adjusted columns to accommodate MP4
|
| 624 |
with top_bar[0]:
|
| 625 |
if st.button("🗑 DelAllMD"):
|
| 626 |
for f in all_md:
|
|
|
|
| 632 |
os.remove(f)
|
| 633 |
st.session_state.should_rerun = True
|
| 634 |
with top_bar[2]:
|
| 635 |
+
if st.button("🗑 DelAllMP4"):
|
| 636 |
+
for f in all_mp4:
|
| 637 |
+
os.remove(f)
|
| 638 |
+
st.session_state.should_rerun = True
|
| 639 |
+
with top_bar[3]:
|
| 640 |
if st.button("⬇️ ZipAll"):
|
| 641 |
+
zip_name = create_zip_of_files(all_md, all_mp3, all_mp4, input_question=st.session_state.get('last_query', ''))
|
| 642 |
+
if zip_name:
|
| 643 |
+
st.sidebar.markdown(get_download_link(zip_name, file_type="zip"), unsafe_allow_html=True)
|
| 644 |
+
|
| 645 |
+
for group_name, files in groups_sorted:
|
| 646 |
+
keywords_str = group_name.replace('_', ' ') if group_name else "No Keywords"
|
| 647 |
+
with st.sidebar.expander(f"{FILE_EMOJIS.get('md', '')} {group_name} Files ({len(files)}) - KW: {keywords_str}", expanded=True):
|
|
|
|
|
|
|
| 648 |
c1,c2 = st.columns(2)
|
| 649 |
with c1:
|
| 650 |
+
if st.button("👀ViewGrp", key="view_group_"+group_name):
|
| 651 |
+
st.session_state.viewing_prefix = group_name
|
| 652 |
with c2:
|
| 653 |
+
if st.button("🗑DelGrp", key="del_group_"+group_name):
|
| 654 |
for f in files:
|
| 655 |
os.remove(f)
|
| 656 |
+
st.success(f"Deleted group {group_name}!")
|
| 657 |
st.session_state.should_rerun = True
|
| 658 |
|
| 659 |
for f in files:
|
|
|
|
| 673 |
index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
|
| 674 |
)
|
| 675 |
|
| 676 |
+
# Add audio format selector to sidebar
|
| 677 |
+
st.sidebar.markdown("### 🔊 Audio Format")
|
| 678 |
+
selected_format = st.sidebar.radio(
|
| 679 |
+
"Choose Audio Format:",
|
| 680 |
+
options=["MP3", "MP4"],
|
| 681 |
+
index=0 # Default to MP3
|
| 682 |
+
)
|
| 683 |
+
|
| 684 |
+
# Update session state if voice or format changes
|
| 685 |
if selected_voice != st.session_state['tts_voice']:
|
| 686 |
st.session_state['tts_voice'] = selected_voice
|
| 687 |
st.rerun()
|
| 688 |
+
if selected_format.lower() != st.session_state['audio_format']:
|
| 689 |
+
st.session_state['audio_format'] = selected_format.lower()
|
| 690 |
+
st.rerun()
|
| 691 |
|
| 692 |
tab_main = st.radio("Action:",["🎤 Voice","📸 Media","🔍 ArXiv","📝 Editor"],horizontal=True)
|
| 693 |
|
|
|
|
| 712 |
|
| 713 |
if autorun and input_changed:
|
| 714 |
st.session_state.old_val = val
|
| 715 |
+
st.session_state.last_query = edited_input # Store the last query for zip naming
|
| 716 |
if run_option == "Arxiv":
|
| 717 |
perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
|
| 718 |
titles_summary=True, full_audio=full_audio)
|
|
|
|
| 724 |
else:
|
| 725 |
if st.button("▶ Run"):
|
| 726 |
st.session_state.old_val = val
|
| 727 |
+
st.session_state.last_query = edited_input # Store the last query for zip naming
|
| 728 |
if run_option == "Arxiv":
|
| 729 |
perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
|
| 730 |
titles_summary=True, full_audio=full_audio)
|
|
|
|
| 748 |
help="Generate a full transcript file")
|
| 749 |
|
| 750 |
if q and st.button("🔍Run"):
|
| 751 |
+
st.session_state.last_query = q # Store the last query for zip naming
|
| 752 |
result = perform_ai_lookup(q, vocal_summary=vocal_summary, extended_refs=extended_refs,
|
| 753 |
titles_summary=titles_summary, full_audio=full_audio)
|
| 754 |
if full_transcript:
|
|
|
|
| 757 |
st.markdown("### Change Prompt & Re-Run")
|
| 758 |
q_new = st.text_input("🔄 Modify Query:")
|
| 759 |
if q_new and st.button("🔄 Re-Run with Modified Query"):
|
| 760 |
+
st.session_state.last_query = q_new # Update last query
|
| 761 |
result = perform_ai_lookup(q_new, vocal_summary=vocal_summary, extended_refs=extended_refs,
|
| 762 |
titles_summary=titles_summary, full_audio=full_audio)
|
| 763 |
if full_transcript:
|
|
|
|
| 820 |
else:
|
| 821 |
st.write("Select a file from the sidebar to edit.")
|
| 822 |
|
| 823 |
+
# Load and display files in the sidebar
|
| 824 |
+
groups_sorted = load_files_for_sidebar()
|
| 825 |
+
display_file_manager_sidebar(groups_sorted)
|
| 826 |
|
| 827 |
+
if st.session_state.viewing_prefix and any(st.session_state.viewing_prefix == group for group, _ in groups_sorted):
|
| 828 |
st.write("---")
|
| 829 |
st.write(f"**Viewing Group:** {st.session_state.viewing_prefix}")
|
| 830 |
+
for group_name, files in groups_sorted:
|
| 831 |
+
if group_name == st.session_state.viewing_prefix:
|
| 832 |
+
for f in files:
|
| 833 |
+
fname = os.path.basename(f)
|
| 834 |
+
ext = os.path.splitext(fname)[1].lower().strip('.')
|
| 835 |
+
st.write(f"### {fname}")
|
| 836 |
+
if ext == "md":
|
| 837 |
+
content = open(f,'r',encoding='utf-8').read()
|
| 838 |
+
st.markdown(content)
|
| 839 |
+
elif ext == "mp3":
|
| 840 |
+
st.audio(f)
|
| 841 |
+
elif ext == "mp4":
|
| 842 |
+
st.video(f) # 🆕 Handle MP4 files
|
| 843 |
+
else:
|
| 844 |
+
st.markdown(get_download_link(f), unsafe_allow_html=True)
|
| 845 |
+
break
|
| 846 |
if st.button("❌ Close"):
|
| 847 |
st.session_state.viewing_prefix = None
|
| 848 |
|