darthPanda commited on
Commit
f0bbcdf
·
1 Parent(s): 95b4a29

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message
3
+ import requests
4
+ from gpt_index import SimpleDirectoryReader,GPTListIndex,GPTSimpleVectorIndex,LLMPredictor,PromptHelper,ServiceContext
5
+ from langchain import OpenAI
6
+ import sys
7
+ import os
8
+
9
+ st.set_page_config(
10
+ page_title="Chatbot",
11
+ page_icon=":robot_face:"
12
+ )
13
+
14
+ def api_key_callback():
15
+ # api_key = st.session_state.api_key_input
16
+ os.environ["OPENAI_API_KEY"] = st.session_state.api_key_input
17
+ # print(st.session_state.api_key_input)
18
+
19
+ def api_form():
20
+ with st.form(key="search_form"):
21
+ st.subheader("API Key")
22
+ st.text_input("Enter your OpenAI API key", key="api_key_input")
23
+ st.form_submit_button(label="Submit", on_click=api_key_callback)
24
+ st.caption('''
25
+ This api key wil not be stored and deleted at the end of your web session.
26
+ For more details, you can check out the
27
+ [Source Code](https://huggingface.co/spaces/darthPanda/romeo_and_juliet_chatbot_with_gptIndex/tree/main).
28
+ If you want to be extra careful, you can delete your
29
+ [OpenAI API key](https://platform.openai.com/account/api-keys)
30
+ after using this space.
31
+ ''')
32
+
33
+ with st.sidebar:
34
+ st.title("Romeo and Juliet Chatbot")
35
+ st.markdown('''
36
+ <div>
37
+ This gpt3 based chatbot has been
38
+ trained on famous play <b>Romeo and Juliet</b> by William Shakespeare
39
+ </div>
40
+ ''', unsafe_allow_html=True)
41
+ api_form()
42
+ # st.markdown("[Book Link](https://www.gutenberg.org/ebooks/1513)")
43
+ st.markdown(
44
+ "<div style='position: fixed; bottom: 0;'>Created by Taaha Bajwa</div>",
45
+ unsafe_allow_html=True,
46
+ )
47
+
48
+ st.markdown('Ask me any question from Romeo and Juliet')
49
+
50
+ if 'generated' not in st.session_state:
51
+ st.session_state['generated'] = []
52
+
53
+ if 'past' not in st.session_state:
54
+ st.session_state['past'] = []
55
+
56
+ def query(payload):
57
+ vIndex = GPTSimpleVectorIndex.load_from_disk('vectorIndex.json')
58
+ response = vIndex.query(payload,response_mode='compact')
59
+ # print(str(response))
60
+ return (str(response)).lstrip()
61
+
62
+ def get_text():
63
+ input_text = st.text_input("You: ","", key="input")
64
+ return input_text
65
+
66
+
67
+ user_input = get_text()
68
+
69
+ if user_input:
70
+ output = query(user_input)
71
+ st.session_state.past.append(user_input)
72
+ st.session_state.generated.append(output)
73
+ # try:
74
+ # output = query(user_input)
75
+ # st.session_state.past.append(user_input)
76
+ # st.session_state.generated.append(output)
77
+ # except:
78
+ # st.error('Invalid API key', icon="🚨")
79
+
80
+ if st.session_state['generated']:
81
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
82
+ message(st.session_state["generated"][i], key=str(i))
83
+ message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')