File size: 3,073 Bytes
7f46a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06e14df
7f46a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import toml
from omegaconf import OmegaConf
from query import VectaraQuery
import os

import streamlit as st
from PIL import Image
from functools import partial

def set_query(q: str):
    st.session_state['query'] = q

def launch_bot():
    def get_answer(question):
        response = vq.submit_query(question)
        return response

    corpus_ids = list(eval(os.environ['corpus_ids']))
    questions = list(eval(os.environ['examples']))
    cfg = OmegaConf.create({
        'customer_id': os.environ['customer_id'],
        'corpus_ids': corpus_ids,
        'api_key': os.environ['api_key'],
        'title': os.environ['title'],
        'description': os.environ['description'],
        'examples': questions,
        'source_data_desc': os.environ['source_data_desc']
    })
    vq = VectaraQuery(cfg.api_key, cfg.customer_id, cfg.corpus_ids)
    st.set_page_config(page_title=cfg.title, layout="wide")

    # left side content
    with st.sidebar:
        image = Image.open('Vectara-logo.png')
        st.markdown(f"## Welcome to {cfg.title}\n\n"
                    f"With this demo uses Retieval Augmented Generation to ask questions about {cfg.source_data_desc}\n\n")

        st.markdown("---")
        st.markdown(
            "## How this works?\n"
            "This app was built with [Vectara](https://vectara.com).\n"
            "Vectara's [Indexing API](https://docs.vectara.com/docs/api-reference/indexing-apis/indexing) was used to ingest the data into a Vectara corpus (or index).\n\n"
            "This app uses Vectara API to query the corpus and present the results to you, answering your question.\n\n"
        )
        st.markdown("---")
        st.image(image, width=250)

    st.markdown(f"<center> <h2> Vectara demo app: {cfg.title} </h2> </center>", unsafe_allow_html=True)
    st.markdown(f"<center> <h4> {cfg.description} <h4> </center>", unsafe_allow_html=True)

    # Setup a split column layout
    main_col, questions_col = st.columns([4, 2], gap="medium")
    with main_col:        
        cols = st.columns([1, 8], gap="small")
        cols[0].markdown("""<h5>Search</h5>""", unsafe_allow_html=True)             
        cols[1].text_input(label="search", key='query', max_chars=256, label_visibility='collapsed', help="Enter your question here")

        st.markdown("<h5>Response</h5>", unsafe_allow_html=True)
        response_text = st.empty()
        response_text.text_area(f" ", placeholder="The answer will appear here.", disabled=True,
                                key="response", height=1, label_visibility='collapsed')
    with questions_col:
        st.markdown("<h5 style='text-align:center; color: red'> Sample questions </h5>", unsafe_allow_html=True)
        for q in list(cfg.examples):
            st.button(q, on_click=partial(set_query, q), use_container_width=True)


    # run the main flow
    if st.session_state.get('query'):
        query = st.session_state['query']
        response = get_answer(query)
        response_text.markdown(response)

if __name__ == "__main__":
    launch_bot()