aiqcamp / app.py
seawolf2357's picture
Update app.py
bb92fc2 verified
raw
history blame
8.15 kB
import streamlit as st
import requests
import streamlit.components.v1 as components
from googletrans import Translator, LANGUAGES
from gtts import gTTS
from gtts.lang import tts_langs
from io import BytesIO
# νŽ˜μ΄μ§€ μ„€μ •
st.set_page_config(page_title="ViDraft", layout="wide")
# μ‚¬μ΄λ“œλ°” 타이틀 μ„€μ •
st.sidebar.title("ViDraft")
# 메뉴 및 ν•˜μœ„ 메뉴 μ •μ˜
menus = {
"Home": [],
"Free Stock": ["Template Video", "Search Video", "Search Image"],
"Image": ["Generation", "Face ID", "Inpainting", "Remove Background", "Studio"],
"Video": ["Generation", "Talking Face", "Remove Background", "Studio"],
"Sound": ["Video SFX", "Video Music", "TTS(Voice)", "Image SFX", "Image Music"],
"Scripts": []
}
# μ„Έμ…˜ μƒνƒœ μ΄ˆκΈ°ν™”
if 'current_menu' not in st.session_state:
st.session_state['current_menu'] = 'Home'
if 'current_sub_menu' not in st.session_state:
st.session_state['current_sub_menu'] = ''
# 메인 메뉴 선택
selected_menu = st.sidebar.selectbox("Menu", list(menus.keys()), key='main_menu')
st.session_state['current_menu'] = selected_menu
# ν•˜μœ„ 메뉴 선택 (ν•΄λ‹Ήλ˜λŠ” 경우)
if selected_menu in menus:
selected_sub_menu = st.sidebar.selectbox("Sub Menu", [""] + menus[selected_menu], key=f'sub_menu_{selected_menu}')
st.session_state['current_sub_menu'] = selected_sub_menu
else:
st.session_state['current_sub_menu'] = ''
translator = Translator()
# 'Sound' λ©”λ‰΄μ˜ 'TTS(Voice)' 선택 μ‹œ
if selected_menu == "Sound" and selected_sub_menu == "TTS(Voice)":
st.header("Text-to-Speech with Translation")
# ν…μŠ€νŠΈ μž…λ ₯
text_to_translate = st.text_area("Enter text to translate and synthesize", "Hello, welcome to ViDraft TTS service.")
# νƒ€κ²Ÿ μ–Έμ–΄ 선택
target_language = st.selectbox("Choose Target Language for Translation", options=list(LANGUAGES.values()), index=list(LANGUAGES.values()).index('English'))
# λ²ˆμ—­ λ²„νŠΌ
if st.button("Translate"):
# λ²ˆμ—­ μ‹€ν–‰
translation = translator.translate(text_to_translate, dest=list(LANGUAGES.keys())[list(LANGUAGES.values()).index(target_language)])
st.text_area("Translated Text", translation.text, height=100)
# μŒμ„± 생성 λ²„νŠΌ
if st.button("Synthesize Voice"):
try:
# λ²ˆμ—­λœ ν…μŠ€νŠΈλ₯Ό μŒμ„±μœΌλ‘œ λ³€ν™˜
tts = gTTS(text=translation.text, lang=list(LANGUAGES.keys())[list(LANGUAGES.values()).index(target_language)], slow=False)
audio_file = BytesIO()
tts.write_to_fp(audio_file)
audio_file.seek(0)
# μƒμ„±λœ μ˜€λ””μ˜€ νŒŒμΌμ„ μž¬μƒ
st.audio(audio_file, format="audio/mp3")
except Exception as e:
st.error(f"Error: {e}")
# Pexels API ν‚€ μ„€μ •
PEXELS_API_KEY = "5woz23MGx1QrSY0WHFb0BRi29JvbXPu97Hg0xnklYgHUI8G0w23FKH62"
def search_images(keyword, per_page=80):
"""이미지 검색 ν•¨μˆ˜"""
url = f"https://api.pexels.com/v1/search?query={keyword}&per_page={per_page}"
headers = {"Authorization": PEXELS_API_KEY}
response = requests.get(url, headers=headers).json()
images = []
if 'photos' in response:
for photo in response['photos']:
images.append(photo['src']['original'])
return images
def search_videos(keyword, per_page=80):
"""λΉ„λ””μ˜€ 검색 ν•¨μˆ˜"""
url = f"https://api.pexels.com/videos/search?query={keyword}&per_page={per_page}"
headers = {"Authorization": PEXELS_API_KEY}
response = requests.get(url, headers=headers).json()
videos = []
if 'videos' in response:
for video in response['videos']:
videos.append(video['video_files'][0]['link'])
return videos
# 'Search Image' 선택 μ‹œ
if selected_menu == "Free Stock" and st.session_state['current_sub_menu'] == "Search Image":
keyword = st.text_input("Enter a keyword to search for images")
if keyword:
images = search_images(keyword)
cols = st.columns(3) # 3μ—΄λ‘œ 이미지 ν‘œμ‹œ
for idx, img in enumerate(images):
with cols[idx % 3]:
st.image(img)
# 'Search Video' 선택 μ‹œ
if selected_menu == "Free Stock" and st.session_state['current_sub_menu'] == "Search Video":
keyword = st.text_input("Enter a keyword to search for videos")
if keyword:
videos = search_videos(keyword)
cols = st.columns(3) # 3μ—΄λ‘œ λΉ„λ””μ˜€ ν‘œμ‹œ
for idx, video in enumerate(videos):
with cols[idx % 3]:
st.video(video)
# 'Image' λ©”λ‰΄μ—μ„œ 'Remove Background' 선택 μ‹œ
if selected_menu == "Image" and st.session_state['current_sub_menu'] == "Remove Background":
st.header("Remove Background")
# iframe을 μ‚¬μš©ν•˜μ—¬ μ™ΈλΆ€ URL μž„λ² λ“œ
components.iframe("https://seawolf2357-gttstest.hf.space", width=None, height=600, scrolling=True)
# 'Home' νŽ˜μ΄μ§€ ν‘œμ‹œ
if selected_menu == "Home":
st.header("Welcome to ViDraft")
st.write("Choose an option from the sidebar to get started.")
elif selected_menu == "Free Stock":
# 'Free Stock' 메뉴 선택 μ‹œ 처리 둜직
if st.session_state['current_sub_menu'] == "Template Video":
st.header("Template Videos")
st.write("Explore a variety of video templates.")
# λΉ„λ””μ˜€ 파일 경둜 μ„€μ • 및 λΉ„λ””μ˜€ ν‘œμ‹œ 둜직
video_files = ["ex1.mp4", "ex2.mp4", "ex3.mp4", "ex4.mp4", "ex5.mp4", "ex6.mp4", "ex7.mp4", "ex8.mp4", "ex9.mp4", "ex10.mp4", "ex11.mp4", "ex12.mp4", "ex13.mp4", "ex14.mp4", "ex15.mp4", "ex16.mp4", "ex17.mp4", "ex18.mp4"]
# 가러리 ν˜•νƒœλ‘œ λΉ„λ””μ˜€ ν‘œμ‹œ
cols = st.columns(2) # 2개의 μ—΄λ‘œ λ„“κ²Œ 배치
for index, video_file in enumerate(video_files):
with cols[index % 2]:
st.video(video_file)
# 'Image' 메뉴 선택 μ‹œ 처리 둜직
elif selected_menu == "Image":
if st.session_state['current_sub_menu'] == "Generation":
st.header("Image Generation")
st.write("Generate images using AI.")
elif st.session_state['current_sub_menu'] == "Face ID":
st.header("Face ID")
st.write("Identify and analyze faces in images.")
elif st.session_state['current_sub_menu'] == "Inpainting":
st.header("Inpainting")
st.write("Fill in missing parts of images.")
elif st.session_state['current_sub_menu'] == "Studio":
st.header("Image Studio")
st.write("Access a suite of image editing tools.")
# 'Video' 메뉴 선택 μ‹œ 처리 둜직
elif selected_menu == "Video":
if st.session_state['current_sub_menu'] == "Generation":
st.header("Video Generation")
st.write("Create videos with our generation tools.")
elif st.session_state['current_sub_menu'] == "Talking Face":
st.header("Talking Face Videos")
st.write("Generate talking face videos from images.")
elif st.session_state['current_sub_menu'] == "Remove Background":
st.header("Video Background Removal")
st.write("Remove backgrounds from your videos effortlessly.")
elif st.session_state['current_sub_menu'] == "Studio":
st.header("Video Studio")
st.write("Explore our video editing studio for advanced editing.")
# 'Sound' 메뉴 선택 μ‹œ 처리 둜직
elif selected_menu == "Sound":
if st.session_state['current_sub_menu'] == "Video SFX":
st.header("Video Sound Effects")
st.write("Enhance your videos with sound effects.")
elif st.session_state['current_sub_menu'] == "Video Music":
st.header("Video Music")
st.write("Find the perfect music for your video content.")
elif st.session_state['current_sub_menu'] == "Image SFX":
st.header("Image Sound Effects")
st.write("Add sound effects to your images.")
elif st.session_state['current_sub_menu'] == "Image Music":
st.header("Image Music")
st.write("Associate music tracks with your images.")
# 'Scripts' 메뉴 선택 μ‹œ 처리 둜직
elif selected_menu == "Scripts":
st.header("Scripts")
st.write("Script writing tools and resources.")