重构webui的页面代码

This commit is contained in:
puke
2025-11-17 20:44:07 +08:00
parent 1a5a7dfdd9
commit 8a3cfdedda
14 changed files with 1560 additions and 1309 deletions

View File

@@ -0,0 +1,172 @@
# Copyright (C) 2025 AIDC-AI
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Content input components for web UI (left column)
"""
import streamlit as st
from web.i18n import tr
from web.utils.async_helpers import get_project_version
def render_content_input():
"""Render content input section (left column)"""
with st.container(border=True):
st.markdown(f"**{tr('section.content_input')}**")
# Processing mode selection
mode = st.radio(
"Processing Mode",
["generate", "fixed"],
horizontal=True,
format_func=lambda x: tr(f"mode.{x}"),
label_visibility="collapsed"
)
# Text input (unified for both modes)
text_placeholder = tr("input.topic_placeholder") if mode == "generate" else tr("input.content_placeholder")
text_height = 120 if mode == "generate" else 200
text_help = tr("input.text_help_generate") if mode == "generate" else tr("input.text_help_fixed")
text = st.text_area(
tr("input.text"),
placeholder=text_placeholder,
height=text_height,
help=text_help
)
# Title input (optional for both modes)
title = st.text_input(
tr("input.title"),
placeholder=tr("input.title_placeholder"),
help=tr("input.title_help")
)
# Number of scenes (only show in generate mode)
if mode == "generate":
n_scenes = st.slider(
tr("video.frames"),
min_value=3,
max_value=30,
value=5,
help=tr("video.frames_help"),
label_visibility="collapsed"
)
st.caption(tr("video.frames_label", n=n_scenes))
else:
# Fixed mode: n_scenes is ignored, set default value
n_scenes = 5
st.info(tr("video.frames_fixed_mode_hint"))
return {
"mode": mode,
"text": text,
"title": title,
"n_scenes": n_scenes
}
def render_bgm_section():
"""Render BGM selection section"""
with st.container(border=True):
st.markdown(f"**{tr('section.bgm')}**")
with st.expander(tr("help.feature_description"), expanded=False):
st.markdown(f"**{tr('help.what')}**")
st.markdown(tr("bgm.what"))
st.markdown(f"**{tr('help.how')}**")
st.markdown(tr("bgm.how"))
# Dynamically scan bgm folder for music files (merged from bgm/ and data/bgm/)
from pixelle_video.utils.os_util import list_resource_files
try:
all_files = list_resource_files("bgm")
# Filter to audio files only
audio_extensions = ('.mp3', '.wav', '.flac', '.m4a', '.aac', '.ogg')
bgm_files = sorted([f for f in all_files if f.lower().endswith(audio_extensions)])
except Exception as e:
st.warning(f"Failed to load BGM files: {e}")
bgm_files = []
# Add special "None" option
bgm_options = [tr("bgm.none")] + bgm_files
# Default to "default.mp3" if exists, otherwise first option
default_index = 0
if "default.mp3" in bgm_files:
default_index = bgm_options.index("default.mp3")
bgm_choice = st.selectbox(
"BGM",
bgm_options,
index=default_index,
label_visibility="collapsed"
)
# BGM volume slider (only show when BGM is selected)
if bgm_choice != tr("bgm.none"):
bgm_volume = st.slider(
tr("bgm.volume"),
min_value=0.0,
max_value=0.5,
value=0.2,
step=0.01,
format="%.2f",
key="bgm_volume_slider",
help=tr("bgm.volume_help")
)
else:
bgm_volume = 0.2 # Default value when no BGM selected
# BGM preview button (only if BGM is not "None")
if bgm_choice != tr("bgm.none"):
if st.button(tr("bgm.preview"), key="preview_bgm", use_container_width=True):
from pixelle_video.utils.os_util import get_resource_path, resource_exists
try:
if resource_exists("bgm", bgm_choice):
bgm_file_path = get_resource_path("bgm", bgm_choice)
st.audio(bgm_file_path)
else:
st.error(tr("bgm.preview_failed", file=bgm_choice))
except Exception as e:
st.error(f"{tr('bgm.preview_failed', file=bgm_choice)}: {e}")
# Use full filename for bgm_path (including extension)
bgm_path = None if bgm_choice == tr("bgm.none") else bgm_choice
return {
"bgm_path": bgm_path,
"bgm_volume": bgm_volume
}
def render_version_info():
"""Render version info and GitHub link"""
with st.container(border=True):
st.markdown(f"**{tr('version.title')}**")
version = get_project_version()
github_url = "https://github.com/AIDC-AI/Pixelle-Video"
# Version and GitHub link in one line
github_url = "https://github.com/AIDC-AI/Pixelle-Video"
badge_url = "https://img.shields.io/github/stars/AIDC-AI/Pixelle-Video"
st.markdown(
f'{tr("version.current")}: `{version}`    '
f'<a href="{github_url}" target="_blank">'
f'<img src="{badge_url}" alt="GitHub stars" style="vertical-align: middle;">'
f'</a>',
unsafe_allow_html=True)