Spaces:
Sleeping
Sleeping
ogeti siva satyanarayana
commited on
Commit
Β·
f5d12b4
1
Parent(s):
e079ab1
Update video_enhancer.py
Browse files- video_enhancer.py +100 -47
video_enhancer.py
CHANGED
@@ -1,47 +1,100 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
from
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# streamlit_ui.py
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
import base64
|
5 |
+
import tempfile
|
6 |
+
from backend.subtitle_utils import generate_srt_from_text, enhance_video_with_subtitles_and_bgm
|
7 |
+
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="Prompta - Text to Media Generator",
|
10 |
+
page_icon="ποΈ",
|
11 |
+
layout="wide",
|
12 |
+
initial_sidebar_state="expanded"
|
13 |
+
)
|
14 |
+
st.title("ποΈπΌοΈποΈ Prompta - Text to Media Generator")
|
15 |
+
|
16 |
+
API_BASE = "http://localhost:8000"
|
17 |
+
|
18 |
+
def render_media(file_bytes, media_type, label):
|
19 |
+
b64 = base64.b64encode(file_bytes).decode()
|
20 |
+
if media_type == "audio":
|
21 |
+
st.audio(f"data:audio/wav;base64,{b64}", format="audio/wav")
|
22 |
+
elif media_type == "video":
|
23 |
+
st.video(f"data:video/mp4;base64,{b64}")
|
24 |
+
elif media_type == "image":
|
25 |
+
st.image(file_bytes, caption=label, use_column_width=True)
|
26 |
+
|
27 |
+
st.sidebar.header("π οΈ Settings")
|
28 |
+
TOKEN = st.sidebar.text_input("π API Token", type="password")
|
29 |
+
HEADERS = {"Authorization": f"Bearer {TOKEN}"} if TOKEN else {}
|
30 |
+
|
31 |
+
tab = st.sidebar.radio("Select Task", ["Text to Audio", "Text to Image", "Text to Video"])
|
32 |
+
|
33 |
+
if tab == "Text to Audio":
|
34 |
+
st.subheader("π€ Text to Audio")
|
35 |
+
text = st.text_area("Enter text")
|
36 |
+
voice = st.selectbox("Choose voice/language", ["en-US", "hi-IN", "te-IN", "ta-IN"])
|
37 |
+
|
38 |
+
if st.button("π Generate Audio"):
|
39 |
+
with st.spinner("Generating audio..."):
|
40 |
+
r = requests.post(f"{API_BASE}/audio/generate", json={"text": text, "voice": voice}, headers=HEADERS)
|
41 |
+
if r.status_code == 200:
|
42 |
+
render_media(r.content, "audio", "Generated Audio")
|
43 |
+
else:
|
44 |
+
st.error(f"β Failed: {r.json().get('detail')}")
|
45 |
+
|
46 |
+
elif tab == "Text to Image":
|
47 |
+
st.subheader("πΌοΈ Text to Image")
|
48 |
+
prompt = st.text_area("Enter image prompt")
|
49 |
+
model = st.selectbox("Choose model", ["sdxl", "deepfloyd", "kandinsky"])
|
50 |
+
|
51 |
+
if st.button("π§ Generate Image"):
|
52 |
+
with st.spinner("Generating image..."):
|
53 |
+
r = requests.post(f"{API_BASE}/image/generate", json={"prompt": prompt, "model": model}, headers=HEADERS)
|
54 |
+
if r.status_code == 200:
|
55 |
+
render_media(r.content, "image", "Generated Image")
|
56 |
+
else:
|
57 |
+
st.error(f"β Failed: {r.json().get('detail')}")
|
58 |
+
|
59 |
+
elif tab == "Text to Video":
|
60 |
+
st.subheader("ποΈ Text to Video")
|
61 |
+
prompt = st.text_area("Enter video prompt")
|
62 |
+
tone = st.selectbox("Tone", ["formal", "casual", "emotional", "documentary"])
|
63 |
+
domain = st.selectbox("Domain", ["health", "education", "governance", "entertainment"])
|
64 |
+
environment = st.selectbox("Environment", ["urban", "rural", "nature", "futuristic"])
|
65 |
+
|
66 |
+
transcript = st.text_area("Transcript (optional - for subtitles)", height=100)
|
67 |
+
enhance = st.checkbox("β¨ Add Subtitles and Background Music")
|
68 |
+
|
69 |
+
if st.button("π¬ Generate Video"):
|
70 |
+
with st.spinner("Generating video..."):
|
71 |
+
r = requests.post(
|
72 |
+
f"{API_BASE}/video/generate",
|
73 |
+
json={"prompt": prompt, "tone": tone, "domain": domain, "environment": environment},
|
74 |
+
headers=HEADERS
|
75 |
+
)
|
76 |
+
if r.status_code == 200:
|
77 |
+
video_bytes = r.content
|
78 |
+
if enhance and transcript:
|
79 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_vid:
|
80 |
+
tmp_vid.write(video_bytes)
|
81 |
+
tmp_vid_path = tmp_vid.name
|
82 |
+
|
83 |
+
srt_path = generate_srt_from_text(transcript, output_path="streamlit_subs.srt")
|
84 |
+
enhanced_path = "streamlit_final_video.mp4"
|
85 |
+
enhance_video_with_subtitles_and_bgm(
|
86 |
+
video_path=tmp_vid_path,
|
87 |
+
srt_path=srt_path,
|
88 |
+
bgm_path="default_bgm.mp3",
|
89 |
+
output_path=enhanced_path
|
90 |
+
)
|
91 |
+
|
92 |
+
with open(enhanced_path, "rb") as f:
|
93 |
+
render_media(f.read(), "video", "Enhanced Video")
|
94 |
+
else:
|
95 |
+
render_media(video_bytes, "video", "Generated Video")
|
96 |
+
else:
|
97 |
+
st.error(f"β Failed: {r.json().get('detail')}")
|
98 |
+
|
99 |
+
st.sidebar.markdown("---")
|
100 |
+
st.sidebar.info("Built with β€οΈ for AI GovTech Challenge 2025")
|