File size: 6,237 Bytes
80b2a07
 
 
 
 
 
 
61e0a32
 
 
 
80b2a07
 
 
 
61e0a32
80b2a07
 
 
 
61e0a32
80b2a07
61e0a32
 
 
80b2a07
 
61e0a32
80b2a07
 
2aac829
 
 
 
80b2a07
 
 
61e0a32
80b2a07
 
61e0a32
 
80b2a07
61e0a32
 
80b2a07
61e0a32
 
80b2a07
 
61e0a32
 
80b2a07
61e0a32
80b2a07
 
61e0a32
 
80b2a07
 
 
61e0a32
80b2a07
61e0a32
80b2a07
 
61e0a32
 
 
 
 
 
 
 
80b2a07
 
61e0a32
80b2a07
 
61e0a32
 
 
 
 
 
 
 
 
 
 
 
80b2a07
61e0a32
 
80b2a07
 
 
61e0a32
 
80b2a07
 
 
 
61e0a32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80b2a07
 
 
61e0a32
 
 
 
80b2a07
 
 
 
 
61e0a32
80b2a07
 
61e0a32
 
80b2a07
61e0a32
 
80b2a07
61e0a32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80b2a07
61e0a32
80b2a07
61e0a32
 
80b2a07
 
 
 
 
61e0a32
80b2a07
 
61e0a32
 
 
 
 
 
80b2a07
 
61e0a32
80b2a07
 
 
61e0a32
 
80b2a07
 
 
 
61e0a32
80b2a07
 
61e0a32
 
 
80b2a07
 
 
61e0a32
80b2a07
61e0a32
 
 
 
80b2a07
 
 
 
 
61e0a32
80b2a07
61e0a32
80b2a07
 
 
 
61e0a32
80b2a07
 
61e0a32
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import streamlit as st
import os
import re
import base64
import tempfile
import time

# βœ… NEW Gemini SDK
from google import genai

# Local imports
from agents import get_content_enhancer_agent
from tools.image_tool import ImageGenerationTool
from local_knowledge_base import load_knowledge_base

# --- Page Config ---
st.set_page_config(page_title="Video Analyzer", layout="wide")
st.title("πŸŽ₯ Video Analysis & Content Generation")
st.markdown("Provide a video file or a YouTube link to generate an enriched summary with AI-generated images.")

# --- Gemini Client Initialization ---
try:
    client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
except Exception:
    st.error("GOOGLE_API_KEY not found. Add it to your environment/secrets.")
    st.stop()

# --- Load Knowledge Base ---
@st.cache_resource(show_spinner="Loading Local Knowledge Base...")
def get_local_kb():
    local_kn_b = load_knowledge_base()
    if local_kn_b:
        local_kn_b.load(recreate=False)
    return local_kn_b

local_kb = get_local_kb()

# --- Core Logic ---
def enrich_and_generate_images(video_summary: str) -> str:
    st.write("### Step 2: Enhancing Content with AI Agent...")

    with st.spinner("Enhancing content..."):
        content_agent = get_content_enhancer_agent(knowledge_base=local_kb)
        enriched_text_with_placeholders = "".join(list(content_agent.run(video_summary)))

    st.success("βœ… Content enhancement complete.")

    with st.expander("See Enriched Text"):
        st.text(enriched_text_with_placeholders)

    # --- Image Generation ---
    st.write("### Step 3: Generating Images...")
    final_output = enriched_text_with_placeholders

    try:
        image_tool = ImageGenerationTool(api_key=os.getenv("CLIPDROP_API_KEY"))
    except Exception:
        st.error("CLIPDROP_API_KEY missing.")
        st.stop()

    placeholders = re.findall(r'\[IMAGE: caption="(.*?)"\]', final_output)

    if not placeholders:
        st.warning("No image placeholders found.")
        return final_output

    progress_bar = st.progress(0)

    for i, caption in enumerate(placeholders):
        progress_bar.progress(i / len(placeholders))

        image_path = image_tool.generate_image(prompt=caption)

        placeholder = f'[IMAGE: caption="{caption}"]'

        if "Error:" in image_path or not os.path.exists(image_path):
            replacement = f"\n> Image failed: {caption}\n"
        else:
            with open(image_path, "rb") as f:
                encoded = base64.b64encode(f.read()).decode()

            replacement = f"""
<div align="center">
<img src="data:image/png;base64,{encoded}" style="max-width:100%; max-height:500px;">
<br><em>{caption}</em>
</div>
"""

        final_output = final_output.replace(placeholder, replacement, 1)

        time.sleep(3)

    progress_bar.progress(1.0)
    st.success("βœ… Images generated.")

    return final_output


# --- YouTube Workflow ---
def run_youtube_analysis_workflow(yt_url: str):
    st.write("### Step 1: Analyzing YouTube Video...")

    try:
        prompt = (
            "Analyze this video. Extract 2-3 core topics and provide a detailed summary in order."
        )

        response = client.models.generate_content(
            model="gemini-2.0-flash",
            contents=[
                {
                    "role": "user",
                    "parts": [
                        {"text": prompt},
                        {"file_data": {"file_uri": yt_url}}
                    ]
                }
            ]
        )

        video_summary = response.text

        st.success("βœ… YouTube analysis complete.")

        with st.expander("Raw Summary"):
            st.markdown(video_summary)

        return enrich_and_generate_images(video_summary)

    except Exception as e:
        st.error(f"YouTube analysis failed: {e}")
        return None


# --- Local File Workflow ---
def run_local_file_analysis_workflow(video_path: str):
    uploaded_file = None

    try:
        st.write("### Step 1: Uploading Video...")

        uploaded_file = client.files.upload(file=video_path)

        st.info("Processing video...")

        prompt = (
            "Analyze this video. Extract key topics and generate a detailed summary."
        )

        response = client.models.generate_content(
            model="gemini-2.0-flash",
            contents=[
                {
                    "role": "user",
                    "parts": [
                        {"text": prompt},
                        {"file_data": {"file_uri": uploaded_file.uri}}
                    ]
                }
            ]
        )

        video_summary = response.text

        st.success("βœ… Local video analysis complete.")

        with st.expander("Raw Summary"):
            st.markdown(video_summary)

        return enrich_and_generate_images(video_summary)

    except Exception as e:
        st.error(f"Local analysis failed: {e}")
        return None

    finally:
        if uploaded_file:
            try:
                client.files.delete(name=uploaded_file.name)
            except Exception:
                pass


# --- UI ---
st.divider()
st.subheader("Select Video Source")

tab1, tab2 = st.tabs(["YouTube Link", "Upload Video"])

video_path = None
final_response = None

with tab1:
    yt_url = st.text_input("YouTube URL")

with tab2:
    video_file = st.file_uploader("Upload video", type=["mp4", "mov", "avi", "webm"])

if st.button("πŸš€ Analyze", use_container_width=True):

    if yt_url:
        final_response = run_youtube_analysis_workflow(yt_url)

    elif video_file:
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
            tmp.write(video_file.getbuffer())
            video_path = tmp.name

        try:
            final_response = run_local_file_analysis_workflow(video_path)
        finally:
            if video_path and os.path.exists(video_path):
                os.remove(video_path)

    else:
        st.warning("Provide a URL or upload a file.")
        st.stop()

    if final_response:
        st.divider()
        st.markdown("## ✨ Final Output")
        st.markdown(final_response, unsafe_allow_html=True)
    else:
        st.error("No output generated.")