ruslanmv commited on
Commit
53881d8
1 Parent(s): 590ddca
Files changed (4) hide show
  1. .gitignore +1 -0
  2. .streamlit/config.toml +7 -0
  3. app.py +104 -9
  4. webchat.py +1 -0
.gitignore CHANGED
@@ -194,3 +194,4 @@ pyrightconfig.json
194
 
195
  # End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,python
196
  Untitled.java
 
 
194
 
195
  # End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,python
196
  Untitled.java
197
+ backup/
.streamlit/config.toml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ [theme]
2
+ base="light"
3
+ primaryColor="#0D62FE"
4
+ backgroundColor="#ffffff"
5
+ secondaryBackgroundColor="#f0f2f6"
6
+ textColor="#000000"
7
+ font="sans serif"
app.py CHANGED
@@ -9,42 +9,136 @@ url = "https://us-south.ml.cloud.ibm.com"
9
 
10
  # These global variables will be updated in get_credentials() function
11
  watsonx_project_id = ""
 
12
  api_key = ""
13
 
14
  def get_credentials():
15
  load_dotenv()
16
  # Update the global variables that will be used for authentication in another function
17
- globals()["api_key"] = os.getenv("API_KEY", "")
18
- globals()["watsonx_project_id"] = os.getenv("PROJECT_ID", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def main():
21
  # Get the API key and project id and update global variables
22
  get_credentials()
23
 
24
  # Use the full page instead of a narrow central column
25
- st.set_page_config(layout="wide")
26
 
27
- # Streamlit app title
28
- st.title("🌠Demo of RAG with a Web page")
 
 
 
 
 
 
 
 
 
29
 
30
  # Sidebar for settings
31
  st.sidebar.header("Settings")
32
- api_key_input = st.sidebar.text_input("API Key", api_key)
 
 
33
  project_id_input = st.sidebar.text_input("Project ID", watsonx_project_id)
34
-
35
  # Update credentials if provided by the user
36
  if api_key_input:
37
  globals()["api_key"] = api_key_input
38
  if project_id_input:
39
  globals()["watsonx_project_id"] = project_id_input
40
 
 
 
41
  user_url = st.text_input('Provide a URL')
42
- collection_name = st.text_input('Provide a unique name for this website (lower case). Use the same name for the same URL to avoid loading data multiple times.')
43
-
44
  # UI component to enter the question
45
  question = st.text_area('Question', height=100)
46
  button_clicked = st.button("Answer the question")
47
 
 
48
  st.subheader("Response")
49
 
50
  # Invoke the LLM when the button is clicked
@@ -52,5 +146,6 @@ def main():
52
  response = webchat.answer_questions_from_web(api_key, watsonx_project_id, user_url, question, collection_name)
53
  st.write(response)
54
 
 
55
  if __name__ == "__main__":
56
  main()
 
9
 
10
  # These global variables will be updated in get_credentials() function
11
  watsonx_project_id = ""
12
+ # Replace with your IBM Cloud key
13
  api_key = ""
14
 
15
  def get_credentials():
16
  load_dotenv()
17
  # Update the global variables that will be used for authentication in another function
18
+ globals()["api_key"] = os.getenv("api_key", None)
19
+ globals()["watsonx_project_id"] = os.getenv("project_id", None)
20
+
21
+
22
+ def set_theme():
23
+ st.markdown("""
24
+ <style>
25
+ .reportview-container, .main {
26
+ background: #ffffff;
27
+ color: #000000;
28
+ }
29
+ .sidebar .sidebar-content {
30
+ background: #f0f2f6;
31
+ color: #000000;
32
+ }
33
+ .stButton>button {
34
+ background-color: #0D62FE;
35
+ color: white;
36
+ }
37
+ .stTextInput>div>div>input {
38
+ color: #000000;
39
+ background-color: #ffffff;
40
+ }
41
+ tTextArea>div>textarea {
42
+ color: #000000;
43
+ background-color: #ffffff;
44
+ }
45
+ label, .stTextInput>label, .stTextArea>label {
46
+ color: #000000;
47
+ }
48
+ h1, h2, h3, h4, h5, h6 {
49
+ color: #000000;
50
+ }
51
+ .sidebar .sidebar-content h2, .sidebar .sidebar-content h3, .sidebar .sidebar-content h4, .sidebar .sidebar-content h5, .sidebar .sidebar-content h6,
52
+ .sidebar .sidebar-content label, .sidebar .sidebar-content .stTextInput>label, .sidebar .sidebar-content .stTextArea>label {
53
+ color: #000000;
54
+ }
55
+ .navbar {
56
+ overflow: hidden;
57
+ background-color: #000000;
58
+ position: fixed;
59
+ top: 0;
60
+ width: 100%;
61
+ z-index: 1000;
62
+ }
63
+ .navbar h1 {
64
+ float: left;
65
+ display: block;
66
+ color: #ffffff;
67
+ text-align: center;
68
+ padding: 14px 1x;
69
+ text-decoration: none;
70
+ font-size: 17px;
71
+ margin: 0;
72
+ }
73
+ .menu-bar {
74
+ background-color: #000000;
75
+ padding: 10px;
76
+ display: flex;
77
+ justify-content: space-between;
78
+ align-items: center;
79
+ }
80
+ .menu-bar h1 {
81
+ color: #ffffff;
82
+ margin: 0;
83
+ font-size: 18px; /* Reduced font size */
84
+ font-family: 'IBM Plex Sans', sans-serif; /* IBM font */
85
+ }
86
+ </style>
87
+ """, unsafe_allow_html=True)
88
+
89
+
90
+ from urllib.parse import urlparse
91
+
92
+ def create_collection_name(url):
93
+ parsed_url = urlparse(url)
94
+ domain_parts = parsed_url.netloc.split('.')
95
+ if len(domain_parts) >= 2:
96
+ return domain_parts[-2] # Extracting the second-level domain
97
+ else:
98
+ return "base"
99
 
100
  def main():
101
  # Get the API key and project id and update global variables
102
  get_credentials()
103
 
104
  # Use the full page instead of a narrow central column
105
+ st.set_page_config(layout="wide", page_title="RAG Web Demo", page_icon="")
106
 
107
+ # Set the theme
108
+ set_theme()
109
+
110
+ # Streamlit app title with style
111
+ st.markdown("""
112
+ <div class="menu-bar">
113
+ <h1>IBM watsonx.ai - webchat</h1>
114
+ </div>
115
+ <div style="margin-top: 20px;"><p>Insert the website you want to chat with and ask your question.</p></div>
116
+
117
+ """, unsafe_allow_html=True)
118
 
119
  # Sidebar for settings
120
  st.sidebar.header("Settings")
121
+ st.sidebar.markdown("Insert your credentials of [IBM Cloud](https://cloud.ibm.com/login) for watsonx.ai", unsafe_allow_html=True)
122
+ st.sidebar.markdown("<hr>", unsafe_allow_html=True)
123
+ api_key_input = st.sidebar.text_input("API Key", api_key, type="password")
124
  project_id_input = st.sidebar.text_input("Project ID", watsonx_project_id)
125
+
126
  # Update credentials if provided by the user
127
  if api_key_input:
128
  globals()["api_key"] = api_key_input
129
  if project_id_input:
130
  globals()["watsonx_project_id"] = project_id_input
131
 
132
+ # Main input area
133
+ #st.markdown("<hr>", unsafe_allow_html=True)
134
  user_url = st.text_input('Provide a URL')
135
+ # Provide a unique name for this website (lower case). Use the same name for the same URL to avoid loading data multiple times.
136
+ collection_name = create_collection_name(user_url)
137
  # UI component to enter the question
138
  question = st.text_area('Question', height=100)
139
  button_clicked = st.button("Answer the question")
140
 
141
+ st.markdown("<hr>", unsafe_allow_html=True)
142
  st.subheader("Response")
143
 
144
  # Invoke the LLM when the button is clicked
 
146
  response = webchat.answer_questions_from_web(api_key, watsonx_project_id, user_url, question, collection_name)
147
  st.write(response)
148
 
149
+
150
  if __name__ == "__main__":
151
  main()
webchat.py CHANGED
@@ -185,6 +185,7 @@ def answer_questions_from_web(request_api_key, request_project_id, url, question
185
 
186
  # Specify model parameters
187
  model_type = "meta-llama/llama-2-70b-chat"
 
188
  max_tokens = 100
189
  min_tokens = 50
190
  top_k = 50
 
185
 
186
  # Specify model parameters
187
  model_type = "meta-llama/llama-2-70b-chat"
188
+ #model_type = "meta-llama/llama-3-70b-instruct"
189
  max_tokens = 100
190
  min_tokens = 50
191
  top_k = 50