File size: 2,217 Bytes
7229566
 
 
 
 
 
 
 
 
 
 
c8ca8c1
afe3aa1
7229566
 
 
 
 
 
 
 
 
03bd750
 
7229566
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afe3aa1
c1a2475
7229566
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import os
from streamlit_chat import message
from llama_index.readers.schema.base import Document
from llama_index import LLMPredictor, GPTSimpleVectorIndex, PromptHelper, GPTTreeIndex
from langchain import OpenAI
import functions as f
import pandas as pd

#f.set_api_key("openai_key.txt") # Not needed as it's set in the huggingface environment.

llm_predictor = f.config_llm_predictor()

base_index = {}

for application in os.listdir("indices_vector"):
    if application == ".DS_Store":
        continue
    else:
        name = application.split(".")[0]

    base_index[name] = f.load_index(f"indices_vector/{application}")
    
base_index = {key: value for key, value in sorted(base_index.items())}

df = f.get_data()

#Creating the chatbot interface
st.title("Chat with your reviews")

application = st.selectbox("Choose application", options=list(base_index.keys()))
index = base_index[application]
data = df[df["application"] == application.lower()]

# Storing the chat
if "generated" not in st.session_state:
    st.session_state["generated"] = []

if "past" not in st.session_state:
    st.session_state["past"] = []

if 'chat_sent' not in st.session_state:
    st.session_state.chat_sent = ''

tab1, tab2 = st.tabs(["Chat", "Reviews"])

with tab1:

    chat_input = f.get_chat_input()

    if chat_input:
        output = f.generate_response(chat_input, index, llm_predictor)
        output = str(output).strip()

        # store the output
        st.session_state.past.append(chat_input)
        st.session_state.generated.append(output)

        # Empty state so that chat input is not accidentally resent
        del chat_input
        st.session_state.chat_sent = ''

    # Push things 
    if st.session_state["generated"]:
        for i in range(len(st.session_state["generated"]) - 1, -1, -1):
            message(st.session_state["generated"][i], key=str(i))
            message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")

with tab2:

    output = f.get_search(data)

    if len(output) > 0:
        for i, info in output[:100].iterrows():
            st.write(info["review"], info["rating"], info["date"].split(" ")[0])
            st.write("______")