File size: 4,273 Bytes
f5d12b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# streamlit_ui.py
import streamlit as st
import requests
import base64
import tempfile
from backend.subtitle_utils import generate_srt_from_text, enhance_video_with_subtitles_and_bgm

st.set_page_config(
    page_title="Prompta - Text to Media Generator",
    page_icon="πŸŽ™οΈ",
    layout="wide",
    initial_sidebar_state="expanded"
)
st.title("πŸŽ™οΈπŸ–ΌοΈπŸŽžοΈ Prompta - Text to Media Generator")

API_BASE = "http://localhost:8000"

def render_media(file_bytes, media_type, label):
    b64 = base64.b64encode(file_bytes).decode()
    if media_type == "audio":
        st.audio(f"data:audio/wav;base64,{b64}", format="audio/wav")
    elif media_type == "video":
        st.video(f"data:video/mp4;base64,{b64}")
    elif media_type == "image":
        st.image(file_bytes, caption=label, use_column_width=True)

st.sidebar.header("πŸ› οΈ Settings")
TOKEN = st.sidebar.text_input("πŸ”‘ API Token", type="password")
HEADERS = {"Authorization": f"Bearer {TOKEN}"} if TOKEN else {}

tab = st.sidebar.radio("Select Task", ["Text to Audio", "Text to Image", "Text to Video"])

if tab == "Text to Audio":
    st.subheader("🎀 Text to Audio")
    text = st.text_area("Enter text")
    voice = st.selectbox("Choose voice/language", ["en-US", "hi-IN", "te-IN", "ta-IN"])

    if st.button("πŸ”Š Generate Audio"):
        with st.spinner("Generating audio..."):
            r = requests.post(f"{API_BASE}/audio/generate", json={"text": text, "voice": voice}, headers=HEADERS)
            if r.status_code == 200:
                render_media(r.content, "audio", "Generated Audio")
            else:
                st.error(f"❌ Failed: {r.json().get('detail')}")

elif tab == "Text to Image":
    st.subheader("πŸ–ΌοΈ Text to Image")
    prompt = st.text_area("Enter image prompt")
    model = st.selectbox("Choose model", ["sdxl", "deepfloyd", "kandinsky"])

    if st.button("🧠 Generate Image"):
        with st.spinner("Generating image..."):
            r = requests.post(f"{API_BASE}/image/generate", json={"prompt": prompt, "model": model}, headers=HEADERS)
            if r.status_code == 200:
                render_media(r.content, "image", "Generated Image")
            else:
                st.error(f"❌ Failed: {r.json().get('detail')}")

elif tab == "Text to Video":
    st.subheader("🎞️ Text to Video")
    prompt = st.text_area("Enter video prompt")
    tone = st.selectbox("Tone", ["formal", "casual", "emotional", "documentary"])
    domain = st.selectbox("Domain", ["health", "education", "governance", "entertainment"])
    environment = st.selectbox("Environment", ["urban", "rural", "nature", "futuristic"])

    transcript = st.text_area("Transcript (optional - for subtitles)", height=100)
    enhance = st.checkbox("✨ Add Subtitles and Background Music")

    if st.button("🎬 Generate Video"):
        with st.spinner("Generating video..."):
            r = requests.post(
                f"{API_BASE}/video/generate",
                json={"prompt": prompt, "tone": tone, "domain": domain, "environment": environment},
                headers=HEADERS
            )
            if r.status_code == 200:
                video_bytes = r.content
                if enhance and transcript:
                    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_vid:
                        tmp_vid.write(video_bytes)
                        tmp_vid_path = tmp_vid.name

                    srt_path = generate_srt_from_text(transcript, output_path="streamlit_subs.srt")
                    enhanced_path = "streamlit_final_video.mp4"
                    enhance_video_with_subtitles_and_bgm(
                        video_path=tmp_vid_path,
                        srt_path=srt_path,
                        bgm_path="default_bgm.mp3",
                        output_path=enhanced_path
                    )

                    with open(enhanced_path, "rb") as f:
                        render_media(f.read(), "video", "Enhanced Video")
                else:
                    render_media(video_bytes, "video", "Generated Video")
            else:
                st.error(f"❌ Failed: {r.json().get('detail')}")

st.sidebar.markdown("---")
st.sidebar.info("Built with ❀️ for AI GovTech Challenge 2025")