Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -944,6 +944,68 @@ def format_duration(seconds: float) -> str:
|
|
| 944 |
|
| 945 |
|
| 946 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 947 |
|
| 948 |
|
| 949 |
|
|
|
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
+
|
| 948 |
+
|
| 949 |
+
|
| 950 |
+
async def create_paper_audio_files(papers: List[Dict], input_question: str):
|
| 951 |
+
"""Generate audio files for papers asynchronously with improved naming."""
|
| 952 |
+
with PerformanceTimer("paper_audio_generation"):
|
| 953 |
+
tasks = []
|
| 954 |
+
for paper in papers:
|
| 955 |
+
try:
|
| 956 |
+
# Prepare text for audio generation
|
| 957 |
+
audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
|
| 958 |
+
audio_text = clean_for_speech(audio_text)
|
| 959 |
+
|
| 960 |
+
# Create sanitized title for filename
|
| 961 |
+
safe_title = paper['title'].lower()
|
| 962 |
+
safe_title = re.sub(r'[^\w\s-]', '', safe_title) # Remove special chars
|
| 963 |
+
safe_title = re.sub(r'\s+', '_', safe_title) # Replace spaces with underscores
|
| 964 |
+
safe_title = safe_title[:100] # Limit length
|
| 965 |
+
|
| 966 |
+
# Generate timestamp
|
| 967 |
+
timestamp = format_timestamp_prefix()
|
| 968 |
+
|
| 969 |
+
# Create filename with timestamp and title
|
| 970 |
+
filename = f"{timestamp}_{safe_title}.{st.session_state['audio_format']}"
|
| 971 |
+
|
| 972 |
+
# Create task for audio generation
|
| 973 |
+
async def generate_audio(text, filename):
|
| 974 |
+
rate_str = "0%"
|
| 975 |
+
pitch_str = "0Hz"
|
| 976 |
+
communicate = edge_tts.Communicate(text, st.session_state['tts_voice'])
|
| 977 |
+
await communicate.save(filename)
|
| 978 |
+
return filename
|
| 979 |
+
|
| 980 |
+
task = generate_audio(audio_text, filename)
|
| 981 |
+
tasks.append((paper, task, filename))
|
| 982 |
+
|
| 983 |
+
except Exception as e:
|
| 984 |
+
st.warning(f"Error preparing audio for paper {paper['title']}: {str(e)}")
|
| 985 |
+
continue
|
| 986 |
+
|
| 987 |
+
# Process all audio generation tasks concurrently
|
| 988 |
+
for paper, task, filename in tasks:
|
| 989 |
+
try:
|
| 990 |
+
audio_file = await task
|
| 991 |
+
if audio_file:
|
| 992 |
+
paper['full_audio'] = audio_file
|
| 993 |
+
if st.session_state['enable_download']:
|
| 994 |
+
paper['download_base64'] = create_download_link_with_cache(
|
| 995 |
+
audio_file,
|
| 996 |
+
st.session_state['audio_format']
|
| 997 |
+
)
|
| 998 |
+
except Exception as e:
|
| 999 |
+
st.warning(f"Error generating audio for paper {paper['title']}: {str(e)}")
|
| 1000 |
+
paper['full_audio'] = None
|
| 1001 |
+
paper['download_base64'] = ''
|
| 1002 |
+
|
| 1003 |
+
|
| 1004 |
+
|
| 1005 |
+
|
| 1006 |
+
|
| 1007 |
+
|
| 1008 |
+
|
| 1009 |
|
| 1010 |
|
| 1011 |
|