adarsh commited on
Commit
c884422
β€’
1 Parent(s): 7339830

added params slider

Browse files
Files changed (2) hide show
  1. app.py +194 -28
  2. requirements.txt +0 -0
app.py CHANGED
@@ -1,3 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from langchain.prompts import PromptTemplate
3
  from langchain_community.llms import CTransformers
@@ -9,7 +172,7 @@ from src.prompt import prompt_template
9
  from langchain.chains import RetrievalQA
10
  import time
11
  from pinecone import Pinecone
12
-
13
 
14
  # Load environment variables
15
  load_dotenv()
@@ -68,25 +231,23 @@ st.title("πŸ₯ Medicure RAG Chatbot")
68
  st.write("Welcome to Medicure Chatbot! Ask any medical question and I'll do my best to help you.")
69
  st.write("#### Built with πŸ€— Ctransformers, Langchain, and Pinecone. Powered by Metal-llama2-7b-chat quantized LLM")
70
 
 
 
 
 
 
 
71
  # Initialize the chatbot components
72
  @st.cache_resource
73
- def initialize_chatbot():
74
-
75
  embeddings = download_hf_embeddings()
76
- # model_name_or_path = "TheBloke/Llama-2-7B-Chat-GGML"
77
- # model_basename = "llama-2-7b-chat.ggmlv3.q4_0.bin"
78
- # model_path = download_hf_model(model_name_or_path, model_basename)
79
  model_path = "TheBloke/Llama-2-7B-Chat-GGML"
80
  llm = CTransformers(model=model_path,
81
  model_type="llama",
82
- config={'max_new_tokens': 512,
83
- 'temperature': 0.8})
84
-
85
-
86
-
87
-
88
- # initiaize pinecone
89
 
 
90
  pc = Pinecone(api_key=PINECONE_API_KEY)
91
  index = pc.Index(index_name)
92
 
@@ -96,20 +257,34 @@ def initialize_chatbot():
96
  qa = RetrievalQA.from_chain_type(
97
  llm=llm,
98
  chain_type="stuff",
99
- retriever=docsearch.as_retriever(search_kwargs={'k': 2}),
100
  return_source_documents=True,
101
  chain_type_kwargs=chain_type_kwargs)
102
  return qa
103
 
104
- qa = initialize_chatbot()
105
 
106
  # Chat interface
107
- user_input = st.text_input("Ask your medical question:")
108
  if st.button("Send", key="send"):
109
  if user_input:
110
- with st.spinner("Thinking..."):
111
- result = qa({"query": user_input})
112
- response = result["result"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  st.session_state.chat_history.append(("You", user_input))
114
  st.session_state.chat_history.append(("Bot", response))
115
 
@@ -121,15 +296,6 @@ for role, message in st.session_state.chat_history:
121
  else:
122
  st.markdown(f"**Bot:** {message}")
123
 
124
- # Animated loading for visual appeal
125
- def load_animation():
126
- with st.empty():
127
- for i in range(3):
128
- for j in ["β‹…", "β‹…β‹…", "β‹…β‹…β‹…", "β‹…β‹…β‹…β‹…"]:
129
- st.write(f"Loading{j}")
130
- time.sleep(0.2)
131
- st.write("")
132
-
133
  # Footer with social links
134
  st.markdown("""
135
  <div class="footer">
 
1
+ # import streamlit as st
2
+ # from langchain.prompts import PromptTemplate
3
+ # from langchain_community.llms import CTransformers
4
+ # from src.helper import download_hf_embeddings, text_split, download_hf_model
5
+ # from langchain_community.vectorstores import Pinecone as LangchainPinecone
6
+ # import os
7
+ # from dotenv import load_dotenv
8
+ # from src.prompt import prompt_template
9
+ # from langchain.chains import RetrievalQA
10
+ # import time
11
+ # from pinecone import Pinecone
12
+ # from tqdm.auto import tqdm
13
+
14
+ # # Load environment variables
15
+ # load_dotenv()
16
+
17
+ # PINECONE_API_KEY = os.getenv('PINECONE_API_KEY')
18
+ # index_name = "medicure-chatbot"
19
+
20
+ # # Set page configuration
21
+ # st.set_page_config(page_title="Medical Chatbot", page_icon="πŸ₯", layout="wide")
22
+
23
+ # # Custom CSS for styling
24
+ # st.markdown("""
25
+ # <style>
26
+ # .stApp {
27
+ # background-color: #f0f8ff;
28
+ # }
29
+ # .stButton>button {
30
+ # background-color: #4CAF50;
31
+ # color: white;
32
+ # border-radius: 20px;
33
+ # border: none;
34
+ # padding: 10px 20px;
35
+ # transition: all 0.3s ease;
36
+ # }
37
+ # .stButton>button:hover {
38
+ # background-color: #333;
39
+ # transform: scale(1.05);
40
+ # color:#fff;
41
+ # }
42
+ # .footer {
43
+ # position: fixed;
44
+ # left: 0;
45
+ # bottom: 0;
46
+ # width: 100%;
47
+ # background-color: #333;
48
+ # color: white;
49
+ # text-align: center;
50
+ # padding: 10px 0;
51
+ # }
52
+ # .social-icons a {
53
+ # color: white;
54
+ # margin: 0 10px;
55
+ # font-size: 24px;
56
+ # }
57
+ # </style>
58
+ # """, unsafe_allow_html=True)
59
+
60
+ # # Initialize session state for chat history
61
+ # if 'chat_history' not in st.session_state:
62
+ # st.session_state.chat_history = []
63
+
64
+ # # Header
65
+ # st.title("πŸ₯ Medicure RAG Chatbot")
66
+
67
+ # # Display welcome message
68
+ # st.write("Welcome to Medicure Chatbot! Ask any medical question and I'll do my best to help you.")
69
+ # st.write("#### Built with πŸ€— Ctransformers, Langchain, and Pinecone. Powered by Metal-llama2-7b-chat quantized LLM")
70
+
71
+ # # Initialize the chatbot components
72
+ # @st.cache_resource
73
+ # def initialize_chatbot():
74
+
75
+ # embeddings = download_hf_embeddings()
76
+ # # model_name_or_path = "TheBloke/Llama-2-7B-Chat-GGML"
77
+ # # model_basename = "llama-2-7b-chat.ggmlv3.q4_0.bin"
78
+ # # model_path = download_hf_model(model_name_or_path, model_basename)
79
+ # model_path = "TheBloke/Llama-2-7B-Chat-GGML"
80
+ # llm = CTransformers(model=model_path,
81
+ # model_type="llama",
82
+ # config={'max_new_tokens': 512,
83
+ # 'temperature': 0.8})
84
+
85
+
86
+
87
+
88
+ # # initiaize pinecone
89
+
90
+ # pc = Pinecone(api_key=PINECONE_API_KEY)
91
+ # index = pc.Index(index_name)
92
+
93
+ # PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
94
+ # chain_type_kwargs = {"prompt": PROMPT}
95
+ # docsearch = LangchainPinecone(index, embeddings.embed_query, "text")
96
+ # qa = RetrievalQA.from_chain_type(
97
+ # llm=llm,
98
+ # chain_type="stuff",
99
+ # retriever=docsearch.as_retriever(search_kwargs={'k': 2}),
100
+ # return_source_documents=True,
101
+ # chain_type_kwargs=chain_type_kwargs)
102
+ # return qa
103
+
104
+ # qa = initialize_chatbot()
105
+
106
+ # # Chat interface
107
+ # user_input = st.text_input("Ask your question:")
108
+ # if st.button("Send", key="send"):
109
+ # if user_input:
110
+ # # Create a placeholder for the progress bar
111
+ # progress_placeholder = st.empty()
112
+
113
+ # # Simulate progress with tqdm
114
+ # total_steps = 100
115
+ # with tqdm(total=total_steps, file=progress_placeholder, desc="Thinking", bar_format='{l_bar}{bar}') as pbar:
116
+ # for i in range(total_steps):
117
+ # time.sleep(0.05) # Adjust this value to control the speed of the progress bar
118
+ # pbar.update(1)
119
+
120
+ # # Get the actual response
121
+ # result = qa({"query": user_input})
122
+ # response = result["result"]
123
+
124
+ # # Clear the progress bar
125
+ # progress_placeholder.empty()
126
+
127
+ # st.session_state.chat_history.append(("You", user_input))
128
+ # st.session_state.chat_history.append(("Bot", response))
129
+
130
+ # # Display chat history
131
+ # st.subheader("Chat History")
132
+ # for role, message in st.session_state.chat_history:
133
+ # if role == "You":
134
+ # st.markdown(f"**You:** {message}")
135
+ # else:
136
+ # st.markdown(f"**Bot:** {message}")
137
+
138
+ # # Animated loading for visual appeal
139
+ # def load_animation():
140
+ # with st.empty():
141
+ # for i in range(3):
142
+ # for j in ["β‹…", "β‹…β‹…", "β‹…β‹…β‹…", "β‹…β‹…β‹…β‹…"]:
143
+ # st.write(f"Loading{j}")
144
+ # time.sleep(0.2)
145
+ # st.write("")
146
+
147
+ # # Footer with social links
148
+ # st.markdown("""
149
+ # <div class="footer">
150
+ # <div class="social-icons">
151
+ # <a href="https://github.com/4darsh-Dev" target="_blank"><i class="fab fa-github"></i></a>
152
+ # <a href="https://linkedin.com/in/adarsh-maurya-dev" target="_blank"><i class="fab fa-linkedin"></i></a>
153
+ # <a href="https://adarshmaurya.onionreads.com" target="_blank"><i class="fas fa-globe"></i></a>
154
+ # <a href="https://www.kaggle.com/adarshm09" target="_blank"><i class="fab fa-kaggle"></i></a>
155
+ # </div>
156
+ # <p> <p style="text-align:center;">Made with ❀️ by <a href="https://www.adarshmaurya.onionreads.com">Adarsh Maurya</a></p> </p>
157
+ # </div>
158
+ # """, unsafe_allow_html=True)
159
+
160
+ # # Load Font Awesome for icons
161
+ # st.markdown('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">', unsafe_allow_html=True)
162
+
163
+
164
  import streamlit as st
165
  from langchain.prompts import PromptTemplate
166
  from langchain_community.llms import CTransformers
 
172
  from langchain.chains import RetrievalQA
173
  import time
174
  from pinecone import Pinecone
175
+ from tqdm.auto import tqdm
176
 
177
  # Load environment variables
178
  load_dotenv()
 
231
  st.write("Welcome to Medicure Chatbot! Ask any medical question and I'll do my best to help you.")
232
  st.write("#### Built with πŸ€— Ctransformers, Langchain, and Pinecone. Powered by Metal-llama2-7b-chat quantized LLM")
233
 
234
+ # Parameters section
235
+ st.sidebar.header("Parameters")
236
+ k_value = st.sidebar.slider("Number of relevant documents (k)", min_value=1, max_value=10, value=2)
237
+ max_new_tokens = st.sidebar.slider("Max new tokens", min_value=64, max_value=1024, value=512)
238
+ temperature = st.sidebar.slider("Temperature", min_value=0.1, max_value=1.0, value=0.8, step=0.1)
239
+
240
  # Initialize the chatbot components
241
  @st.cache_resource
242
+ def initialize_chatbot(k, max_tokens, temp):
 
243
  embeddings = download_hf_embeddings()
 
 
 
244
  model_path = "TheBloke/Llama-2-7B-Chat-GGML"
245
  llm = CTransformers(model=model_path,
246
  model_type="llama",
247
+ config={'max_new_tokens': max_tokens,
248
+ 'temperature': temp})
 
 
 
 
 
249
 
250
+ # initialize pinecone
251
  pc = Pinecone(api_key=PINECONE_API_KEY)
252
  index = pc.Index(index_name)
253
 
 
257
  qa = RetrievalQA.from_chain_type(
258
  llm=llm,
259
  chain_type="stuff",
260
+ retriever=docsearch.as_retriever(search_kwargs={'k': k}),
261
  return_source_documents=True,
262
  chain_type_kwargs=chain_type_kwargs)
263
  return qa
264
 
265
+ qa = initialize_chatbot(k_value, max_new_tokens, temperature)
266
 
267
  # Chat interface
268
+ user_input = st.text_input("Ask your question:")
269
  if st.button("Send", key="send"):
270
  if user_input:
271
+ # Create a placeholder for the progress bar
272
+ progress_placeholder = st.empty()
273
+
274
+ # Simulate progress with tqdm
275
+ total_steps = 100
276
+ with tqdm(total=total_steps, file=progress_placeholder, desc="Thinking", bar_format='{l_bar}{bar}') as pbar:
277
+ for i in range(total_steps):
278
+ time.sleep(0.05) # Adjust this value to control the speed of the progress bar
279
+ pbar.update(1)
280
+
281
+ # Get the actual response
282
+ result = qa({"query": user_input})
283
+ response = result["result"]
284
+
285
+ # Clear the progress bar
286
+ progress_placeholder.empty()
287
+
288
  st.session_state.chat_history.append(("You", user_input))
289
  st.session_state.chat_history.append(("Bot", response))
290
 
 
296
  else:
297
  st.markdown(f"**Bot:** {message}")
298
 
 
 
 
 
 
 
 
 
 
299
  # Footer with social links
300
  st.markdown("""
301
  <div class="footer">
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ