trying to fix layouts again

This commit is contained in:
tcsenpai 2024-12-25 18:47:10 +01:00
parent 0b5ffc05a7
commit 55323e0df9

View File

@ -177,7 +177,7 @@ def show_error(message):
def show_info(message): def show_info(message):
update_header("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> " + message) update_header("> " + message)
def update_header(message): def update_header(message):
@ -319,6 +319,75 @@ def summarize_video(
} }
def fix_transcript(
video_url, model, ollama_url, fallback_to_whisper=True, force_whisper=False
):
video_id = None
# Get the video id from the url if it's a valid youtube or invidious or any other url that contains a video id
if "v=" in video_url:
video_id = video_url.split("v=")[-1]
# Support short urls as well
elif "youtu.be/" in video_url:
video_id = video_url.split("youtu.be/")[-1]
# Also cut out any part of the url after the video id
video_id = video_id.split("&")[0]
st.write(f"Video ID: {video_id}")
with st.spinner("Fetching transcript..."):
transcript = get_transcript(video_id)
show_info("Transcript fetched successfully!")
# Forcing whisper if specified
if force_whisper:
show_warning("Forcing whisper...")
fallback_to_whisper = True
transcript = None
if not transcript:
print("No transcript found, trying to download audio...")
if not fallback_to_whisper:
print("Fallback to whisper is disabled")
return "Unable to fetch transcript (and fallback to whisper is disabled)"
if not force_whisper:
show_warning("Unable to fetch transcript. Trying to download audio...")
try:
print("Downloading audio...")
download_audio(video_url)
show_info("Audio downloaded successfully!")
show_warning("Starting transcription...it might take a while...")
transcript = transcribe("downloads/output.m4a")
show_info("Transcription completed successfully!")
os.remove("downloads/output.m4a")
except Exception as e:
print(f"Error downloading audio or transcribing: {e}")
show_error(f"Error downloading audio or transcribing: {e}")
if os.path.exists("downloads/output.m4a"):
os.remove("downloads/output.m4a")
return "Unable to fetch transcript."
ollama_client = OllamaClient(ollama_url, model)
show_info(f"Ollama client created with model: {model}")
show_warning("Starting transcript enhancement...")
with st.spinner("Enhancing transcript..."):
prompt = f"""Fix the grammar and punctuation of the following transcript, maintaining the exact same content and meaning.
Only correct grammatical errors, add proper punctuation, and fix sentence structure where needed.
Do not rephrase or change the content:\n\n{transcript}"""
enhanced = ollama_client.generate(prompt)
show_info("Transcript enhanced successfully!")
with st.spinner("Fetching video info..."):
video_info = get_video_info(video_id)
st.success("Video info fetched successfully!")
return {
"title": video_info["title"],
"channel": video_info["channel"],
"transcript": transcript,
"enhanced": enhanced,
}
def main(): def main():
# Settings section # Settings section
st.write("## AI Video Summarizer") st.write("## AI Video Summarizer")
@ -352,19 +421,19 @@ def main():
# Video URL input section # Video URL input section
with st.container(): with st.container():
url_col, button_col1, button_col2 = st.columns([3, 1, 1]) col1, col2, col3 = st.columns([3, 1, 1])
with url_col: with col1:
video_url = st.text_input( video_url = st.text_input(
"🎥 Video URL", "🎥 Video URL",
placeholder="https://www.youtube.com/watch?v=...", placeholder="https://www.youtube.com/watch?v=...",
) )
with button_col1: with col2:
summarize_button = st.button("🚀 Summarize", use_container_width=True) summarize_button = st.button("🚀 Summarize", use_container_width=True)
with button_col2: with col3:
rephrase_button = st.button("🔄 Rephrase Only", use_container_width=True) enhance_button = st.button("✨ Enhance", use_container_width=True)
# Advanced settings in collapsible sections # Advanced settings in collapsible sections
with st.expander("⚙️ Advanced Settings"): with st.expander("⚙️ Advanced Settings"):
@ -393,111 +462,104 @@ def main():
with adv_col2: with adv_col2:
fallback_to_whisper = st.checkbox("Fallback to Whisper", value=True) fallback_to_whisper = st.checkbox("Fallback to Whisper", value=True)
if (summarize_button or rephrase_button) and video_url: if (summarize_button or enhance_button) and video_url:
video_id = None if enhance_button:
if "v=" in video_url: # Enhance transcript
video_id = video_url.split("v=")[-1] result = fix_transcript(
elif "youtu.be/" in video_url: video_url,
video_id = video_url.split("youtu.be/")[-1] selected_model,
video_id = video_id.split("&")[0] ollama_url,
st.write(f"Video ID: {video_id}") fallback_to_whisper=fallback_to_whisper,
force_whisper=force_whisper,
)
with st.spinner("Fetching transcript..."): # Display results
transcript = get_transcript(video_id) st.subheader("📺 Video Information")
info_col1, info_col2 = st.columns(2)
with info_col1:
st.write(f"**Title:** {result['title']}")
with info_col2:
st.write(f"**Channel:** {result['channel']}")
if transcript: st.subheader("📝 Enhanced Transcript")
show_info("Transcript fetched successfully!") st.markdown(result["enhanced"])
if rephrase_button: # Original transcript in expander
# Only rephrase the transcript with st.expander("📝 Original Transcript", expanded=False):
show_warning("Starting grammar and punctuation fixes...") st.text_area(
with st.spinner("Fixing transcript..."): "Raw Transcript",
ollama_client = OllamaClient(ollama_url, selected_model) result["transcript"],
prompt = f"""Fix the grammar and punctuation of the following transcript, maintaining the exact same content and meaning. height=200,
Only correct grammatical errors, add proper punctuation, and fix sentence structure where needed. disabled=True,
Do not rephrase or change the content:\n\n{transcript}"""
rephrased = ollama_client.generate(prompt)
video_info = get_video_info(video_id)
# Display results
st.subheader("📺 Video Information")
info_col1, info_col2 = st.columns(2)
with info_col1:
st.write(f"**Title:** {video_info['title']}")
with info_col2:
st.write(f"**Channel:** {video_info['channel']}")
st.subheader("📝 Fixed Transcript")
st.markdown(rephrased)
elif summarize_button:
# Continue with existing summarize functionality
summary = summarize_video(
video_url,
selected_model,
ollama_url,
fallback_to_whisper=fallback_to_whisper,
force_whisper=force_whisper,
) )
# Video Information elif summarize_button:
st.subheader("📺 Video Information") # Continue with existing summarize functionality
info_col1, info_col2 = st.columns(2) summary = summarize_video(
with info_col1: video_url,
st.write(f"**Title:** {summary['title']}") selected_model,
with info_col2: ollama_url,
st.write(f"**Channel:** {summary['channel']}") fallback_to_whisper=fallback_to_whisper,
force_whisper=force_whisper,
)
# Transcript Section # Video Information
with st.expander("📝 Original Transcript", expanded=False): st.subheader("📺 Video Information")
col1, col2 = st.columns([3, 1]) info_col1, info_col2 = st.columns(2)
with col1: with info_col1:
st.text_area( st.write(f"**Title:** {summary['title']}")
"Raw Transcript", with info_col2:
summary["transcript"], st.write(f"**Channel:** {summary['channel']}")
height=200,
disabled=True,
)
with col2:
if st.button("🔄 Rephrase"):
with st.spinner("Rephrasing transcript..."):
ollama_client = OllamaClient(ollama_url, selected_model)
prompt = f"Rephrase the following transcript to make it more readable and well-formatted, keeping the main content intact:\n\n{summary['transcript']}"
st.session_state.rephrased_transcript = (
ollama_client.generate(prompt)
)
if st.button("📋 Share"): # Transcript Section
try: with st.expander("📝 Original Transcript", expanded=False):
content = f"""Video Title: {summary['title']} col1, col2 = st.columns([3, 1])
with col1:
st.text_area(
"Raw Transcript",
summary["transcript"],
height=200,
disabled=True,
)
with col2:
if st.button("🔄 Rephrase"):
with st.spinner("Rephrasing transcript..."):
ollama_client = OllamaClient(ollama_url, selected_model)
prompt = f"Rephrase the following transcript to make it more readable and well-formatted, keeping the main content intact:\n\n{summary['transcript']}"
st.session_state.rephrased_transcript = (
ollama_client.generate(prompt)
)
if st.button("📋 Share"):
try:
content = f"""Video Title: {summary['title']}
Channel: {summary['channel']} Channel: {summary['channel']}
URL: {video_url} URL: {video_url}
--- Transcript --- --- Transcript ---
{summary['transcript']}""" {summary['transcript']}"""
paste_url = create_paste( paste_url = create_paste(
f"Transcript: {summary['title']}", content f"Transcript: {summary['title']}", content
)
st.success(
f"Transcript shared successfully! [View here]({paste_url})"
)
except Exception as e:
if "PASTEBIN_API_KEY" not in os.environ:
st.warning(
"PASTEBIN_API_KEY not found in environment variables"
) )
st.success( else:
f"Transcript shared successfully! [View here]({paste_url})" st.error(f"Error sharing transcript: {str(e)}")
)
except Exception as e:
if "PASTEBIN_API_KEY" not in os.environ:
st.warning(
"PASTEBIN_API_KEY not found in environment variables"
)
else:
st.error(f"Error sharing transcript: {str(e)}")
# Summary Section # Summary Section
st.subheader("📊 AI Summary") st.subheader("📊 AI Summary")
st.markdown(summary["summary"]) st.markdown(summary["summary"])
# After the rephrase button, add: # After the rephrase button, add:
if st.session_state.rephrased_transcript: if st.session_state.rephrased_transcript:
st.markdown(st.session_state.rephrased_transcript) st.markdown(st.session_state.rephrased_transcript)
if __name__ == "__main__": if __name__ == "__main__":