Spaces:
Running
Running
Update backup11.app.py
Browse files- backup11.app.py +116 -0
backup11.app.py
CHANGED
@@ -280,6 +280,122 @@ def process_video_with_gpt(video_input, user_prompt):
|
|
280 |
)
|
281 |
return response.choices[0].message.content
|
282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
|
284 |
def extract_urls(text):
|
285 |
try:
|
|
|
280 |
)
|
281 |
return response.choices[0].message.content
|
282 |
|
283 |
+
|
284 |
+
|
285 |
+
def process_tts(text, voice="alloy"):
|
286 |
+
"""
|
287 |
+
Process text-to-speech using OpenAI's TTS API
|
288 |
+
Voices available: alloy, echo, fable, onyx, nova, shimmer
|
289 |
+
"""
|
290 |
+
try:
|
291 |
+
response = openai_client.audio.speech.create(
|
292 |
+
model="tts-1",
|
293 |
+
voice=voice,
|
294 |
+
input=text
|
295 |
+
)
|
296 |
+
|
297 |
+
# Generate a unique filename
|
298 |
+
filename = generate_filename("tts_output", "mp3")
|
299 |
+
|
300 |
+
# Save the audio file
|
301 |
+
response.stream_to_file(filename)
|
302 |
+
|
303 |
+
# Create audio player HTML
|
304 |
+
audio_html = f"""
|
305 |
+
<audio controls>
|
306 |
+
<source src="data:audio/mp3;base64,{base64.b64encode(open(filename, 'rb').read()).decode()}" type="audio/mp3">
|
307 |
+
Your browser does not support the audio element.
|
308 |
+
</audio>
|
309 |
+
"""
|
310 |
+
|
311 |
+
return filename, audio_html
|
312 |
+
except Exception as e:
|
313 |
+
st.error(f"TTS Error: {str(e)}")
|
314 |
+
return None, None
|
315 |
+
|
316 |
+
def update_chat_interface():
|
317 |
+
"""Update the chat interface to include voice selection and TTS playback"""
|
318 |
+
# Add voice selection to sidebar
|
319 |
+
st.sidebar.markdown("### 🗣️ TTS Voice Settings")
|
320 |
+
selected_voice = st.sidebar.selectbox(
|
321 |
+
"Choose TTS Voice:",
|
322 |
+
["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
|
323 |
+
help="Select the voice for text-to-speech playback"
|
324 |
+
)
|
325 |
+
|
326 |
+
# Store the selected voice in session state
|
327 |
+
if "selected_voice" not in st.session_state:
|
328 |
+
st.session_state.selected_voice = selected_voice
|
329 |
+
|
330 |
+
# Modify the chat processing functions to include TTS
|
331 |
+
def process_with_gpt(text_input):
|
332 |
+
"""Process text with GPT-4o and add TTS."""
|
333 |
+
if text_input:
|
334 |
+
st.session_state.messages.append({"role": "user", "content": text_input})
|
335 |
+
|
336 |
+
with st.chat_message("user"):
|
337 |
+
st.markdown(text_input)
|
338 |
+
|
339 |
+
with st.chat_message("assistant"):
|
340 |
+
completion = openai_client.chat.completions.create(
|
341 |
+
model=st.session_state["openai_model"],
|
342 |
+
messages=[
|
343 |
+
{"role": m["role"], "content": m["content"]}
|
344 |
+
for m in st.session_state.messages
|
345 |
+
],
|
346 |
+
stream=False
|
347 |
+
)
|
348 |
+
return_text = completion.choices[0].message.content
|
349 |
+
st.write("GPT-4o: " + return_text)
|
350 |
+
|
351 |
+
# Add TTS playback
|
352 |
+
filename, audio_html = process_tts(return_text, st.session_state.selected_voice)
|
353 |
+
if audio_html:
|
354 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|
355 |
+
|
356 |
+
# Original file handling
|
357 |
+
filename = generate_filename("GPT-4o: " + return_text, "md")
|
358 |
+
create_file(filename, text_input, return_text)
|
359 |
+
st.session_state.messages.append({"role": "assistant", "content": return_text})
|
360 |
+
return return_text
|
361 |
+
|
362 |
+
def process_with_claude(text_input):
|
363 |
+
"""Process text with Claude and add TTS."""
|
364 |
+
if text_input:
|
365 |
+
with st.chat_message("user"):
|
366 |
+
st.markdown(text_input)
|
367 |
+
|
368 |
+
with st.chat_message("assistant"):
|
369 |
+
response = claude_client.messages.create(
|
370 |
+
model="claude-3-sonnet-20240229",
|
371 |
+
max_tokens=1000,
|
372 |
+
messages=[
|
373 |
+
{"role": "user", "content": text_input}
|
374 |
+
]
|
375 |
+
)
|
376 |
+
response_text = response.content[0].text
|
377 |
+
st.write("Claude: " + response_text)
|
378 |
+
|
379 |
+
# Add TTS playback
|
380 |
+
filename, audio_html = process_tts(response_text, st.session_state.selected_voice)
|
381 |
+
if audio_html:
|
382 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|
383 |
+
|
384 |
+
# Original file handling
|
385 |
+
filename = generate_filename("Claude: " + response_text, "md")
|
386 |
+
create_file(filename, text_input, response_text)
|
387 |
+
|
388 |
+
st.session_state.chat_history.append({
|
389 |
+
"user": text_input,
|
390 |
+
"claude": response_text
|
391 |
+
})
|
392 |
+
return response_text
|
393 |
+
|
394 |
+
|
395 |
+
|
396 |
+
|
397 |
+
|
398 |
+
|
399 |
|
400 |
def extract_urls(text):
|
401 |
try:
|