File size: 5,799 Bytes
893c5d9
 
 
 
 
 
2d02ed4
 
 
893c5d9
c150a4d
 
 
 
 
 
 
 
 
 
 
2d02ed4
 
 
c150a4d
2d02ed4
c150a4d
 
2d02ed4
 
c150a4d
 
2d02ed4
c150a4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d02ed4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
893c5d9
 
2d02ed4
893c5d9
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
from omegaconf import OmegaConf
from query import VectaraQuery
import os

import streamlit as st
from PIL import Image
import concurrent.futures

SCORE_THRESHOLD = 0.7

def inject_custom_css():
    st.markdown(
        """
        <style>
        @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');
        body {
            font-family: 'Roboto', sans-serif;
            background-color: #f5f5f5;
            color: #333;
        }
        body {
            font-family: 'Roboto', sans-serif;
            background-color: #f5f5f5;
            color: #333;
            padding-top: 0px;
            margin-top: 0px;
        }
        .stApp {
            padding-top: 0px;
            margin-top: 0px;
        }
        .stButton>button {
            margin-top: 25px;
            background-color: #4CAF50;
            color: white;
            padding: 10px 24px;
            border: none;
            cursor: pointer;
            border-radius: 4px;
        }
        .stButton>button:hover {
            background-color: #45a049;
        }
        .stTextInput>div>input {
            padding: 10px;
            border-radius: 4px;
            border: 1px solid #ccc;
            font-size: 16px;
        }
        .stTextInput>div>input:focus {
            border-color: #007BFF;
            outline: none;
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
        }
        .centered {
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .sidebar .stImage {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
        }
        .css-1lcbmhc.e1fqkh3o3 { /* This targets the sidebar */
            background-color: #ffffff !important;
            color: #333 !important;
        }
        .css-1d391kg { /* This targets the sidebar headings */
            color: #333 !important;
        }
        </style>
        """,
        unsafe_allow_html=True
    )    

def fetch_summary(vq, matching_text, doc_id):
    return vq.get_summary(matching_text, doc_id)

def launch_app():   
    with concurrent.futures.ThreadPoolExecutor() as executor:

        if 'cfg' not in st.session_state:
            cfg = OmegaConf.create({
                'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
                'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
                'api_key': str(os.environ['VECTARA_API_KEY']),
                'streaming': False
            })
            st.session_state.cfg = cfg
            st.session_state.vq = VectaraQuery(cfg.api_key, cfg.customer_id, [cfg.corpus_id])

        cfg = st.session_state.cfg
        vq = st.session_state.vq
        st.set_page_config(page_title="Media Demo", layout="wide")
        inject_custom_css()

        header_image = Image.open('header-image-2.png')
        cropped_image = header_image.crop((0, 0, header_image.width, 150))
        st.image(cropped_image, use_column_width=True)

        # left side content
        with st.sidebar:
            image = Image.open('vectara-logo.png')
            st.markdown("## Welcome to Media Demo\n\n"
                        "This demo uses Vectara to find the movie where a quote is from.\n\n"
                        "Covers movies from this [playlist](https://www.youtube.com/playlist?list=PLHPTxTxtC0ibVZrT2_WKWUl2SAxsKuKwx) of free movies.")

            st.markdown("---")
            st.markdown(
                "## How this works?\n"
                "This app was built with [Vectara](https://vectara.com).\n"
            )
            st.markdown("---")
            st.image(image, width=250)

        st.markdown("<center> <h3>\"Where did I hear that line?\"</h3> </center>", unsafe_allow_html=True)

        _, q_col, _ = st.columns([1, 4, 1])
        with q_col:
            quote = st.text_input("quote", label_visibility="hidden", placeholder="Enter a quote from a movie.")
            prev_quote = st.session_state.get('prev_quote', '')
            if quote != prev_quote:
                st.session_state.quote = quote
                st.session_state.prev_quote = quote
                st.session_state.movie_name, st.session_state.match_url, st.session_state.score, doc_id, matching_text = vq.submit_query(quote)
                if st.session_state.score < SCORE_THRESHOLD:
                    st.session_state.movie_name = None
                else:
                    future = executor.submit(fetch_summary, vq, matching_text, doc_id)
                    st.session_state.summary_future = future

        if 'score' in st.session_state and st.session_state.score:
            if st.session_state.movie_name is None:
                st.write("Sorry, I couldn't find a match for that quote. Please try another one.")
            else:
                video_url, start_time = st.session_state.match_url.split('&t=')
                video_url = f"{video_url}&cc_load_policy=1"
                start_time = start_time[:-1]  # remove the trailing 's'

                _, video_col, summary_col = st.columns([1, 4, 3])
                with video_col:
                    st.video(video_url, start_time=int(float(start_time)))
                with summary_col:  
                    # Display the summary when it's ready
                    if 'summary_future' in st.session_state:
                        if st.session_state.summary_future.done():
                            st.markdown("**Summary:**")
                            st.session_state.summary = st.session_state.summary_future.result()
                            st.markdown(st.session_state.summary)

            if not st.session_state.summary_future.done():
                st.rerun()


if __name__ == "__main__":
    launch_app()