Spaces:
Running
Running
File size: 10,778 Bytes
c884422 dfe7961 9c97c75 5839dd5 9c97c75 5839dd5 9c97c75 5839dd5 c884422 dfe7961 9c97c75 5839dd5 4ddb082 89cfcf5 5839dd5 dfe7961 9c97c75 7339830 9c97c75 7339830 9c97c75 97131a6 9c97c75 97131a6 9c97c75 97131a6 9c97c75 97131a6 9c97c75 7339830 97131a6 9c97c75 c884422 9c97c75 c884422 9c97c75 5839dd5 9c97c75 c884422 5839dd5 c884422 5839dd5 9c97c75 c884422 9c97c75 c884422 9c97c75 c884422 9c97c75 97131a6 c884422 97131a6 c884422 97131a6 c884422 9c97c75 5839dd5 7339830 9c97c75 7339830 9c97c75 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# import streamlit as st
# from langchain.prompts import PromptTemplate
# from langchain_community.llms import CTransformers
# from src.helper import download_hf_embeddings, text_split, download_hf_model
# from langchain_community.vectorstores import Pinecone as LangchainPinecone
# import os
# from dotenv import load_dotenv
# from src.prompt import prompt_template
# from langchain.chains import RetrievalQA
# import time
# from pinecone import Pinecone
# from tqdm.auto import tqdm
# # Load environment variables
# load_dotenv()
# PINECONE_API_KEY = os.getenv('PINECONE_API_KEY')
# index_name = "medicure-chatbot"
# # Set page configuration
# st.set_page_config(page_title="Medical Chatbot", page_icon="π₯", layout="wide")
# # Custom CSS for styling
# st.markdown("""
# <style>
# .stApp {
# background-color: #f0f8ff;
# }
# .stButton>button {
# background-color: #4CAF50;
# color: white;
# border-radius: 20px;
# border: none;
# padding: 10px 20px;
# transition: all 0.3s ease;
# }
# .stButton>button:hover {
# background-color: #333;
# transform: scale(1.05);
# color:#fff;
# }
# .footer {
# position: fixed;
# left: 0;
# bottom: 0;
# width: 100%;
# background-color: #333;
# color: white;
# text-align: center;
# padding: 10px 0;
# }
# .social-icons a {
# color: white;
# margin: 0 10px;
# font-size: 24px;
# }
# </style>
# """, unsafe_allow_html=True)
# # Initialize session state for chat history
# if 'chat_history' not in st.session_state:
# st.session_state.chat_history = []
# # Header
# st.title("π₯ Medicure RAG Chatbot")
# # Display welcome message
# st.write("Welcome to Medicure Chatbot! Ask any medical question and I'll do my best to help you.")
# st.write("#### Built with π€ Ctransformers, Langchain, and Pinecone. Powered by Metal-llama2-7b-chat quantized LLM")
# # Initialize the chatbot components
# @st.cache_resource
# def initialize_chatbot():
# embeddings = download_hf_embeddings()
# # model_name_or_path = "TheBloke/Llama-2-7B-Chat-GGML"
# # model_basename = "llama-2-7b-chat.ggmlv3.q4_0.bin"
# # model_path = download_hf_model(model_name_or_path, model_basename)
# model_path = "TheBloke/Llama-2-7B-Chat-GGML"
# llm = CTransformers(model=model_path,
# model_type="llama",
# config={'max_new_tokens': 512,
# 'temperature': 0.8})
# # initiaize pinecone
# pc = Pinecone(api_key=PINECONE_API_KEY)
# index = pc.Index(index_name)
# PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
# chain_type_kwargs = {"prompt": PROMPT}
# docsearch = LangchainPinecone(index, embeddings.embed_query, "text")
# qa = RetrievalQA.from_chain_type(
# llm=llm,
# chain_type="stuff",
# retriever=docsearch.as_retriever(search_kwargs={'k': 2}),
# return_source_documents=True,
# chain_type_kwargs=chain_type_kwargs)
# return qa
# qa = initialize_chatbot()
# # Chat interface
# user_input = st.text_input("Ask your question:")
# if st.button("Send", key="send"):
# if user_input:
# # Create a placeholder for the progress bar
# progress_placeholder = st.empty()
# # Simulate progress with tqdm
# total_steps = 100
# with tqdm(total=total_steps, file=progress_placeholder, desc="Thinking", bar_format='{l_bar}{bar}') as pbar:
# for i in range(total_steps):
# time.sleep(0.05) # Adjust this value to control the speed of the progress bar
# pbar.update(1)
# # Get the actual response
# result = qa({"query": user_input})
# response = result["result"]
# # Clear the progress bar
# progress_placeholder.empty()
# st.session_state.chat_history.append(("You", user_input))
# st.session_state.chat_history.append(("Bot", response))
# # Display chat history
# st.subheader("Chat History")
# for role, message in st.session_state.chat_history:
# if role == "You":
# st.markdown(f"**You:** {message}")
# else:
# st.markdown(f"**Bot:** {message}")
# # Animated loading for visual appeal
# def load_animation():
# with st.empty():
# for i in range(3):
# for j in ["β
", "β
β
", "β
β
β
", "β
β
β
β
"]:
# st.write(f"Loading{j}")
# time.sleep(0.2)
# st.write("")
# # Footer with social links
# st.markdown("""
# <div class="footer">
# <div class="social-icons">
# <a href="https://github.com/4darsh-Dev" target="_blank"><i class="fab fa-github"></i></a>
# <a href="https://linkedin.com/in/adarsh-maurya-dev" target="_blank"><i class="fab fa-linkedin"></i></a>
# <a href="https://adarshmaurya.onionreads.com" target="_blank"><i class="fas fa-globe"></i></a>
# <a href="https://www.kaggle.com/adarshm09" target="_blank"><i class="fab fa-kaggle"></i></a>
# </div>
# <p> <p style="text-align:center;">Made with β€οΈ by <a href="https://www.adarshmaurya.onionreads.com">Adarsh Maurya</a></p> </p>
# </div>
# """, unsafe_allow_html=True)
# # Load Font Awesome for icons
# 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)
import streamlit as st
from langchain.prompts import PromptTemplate
from langchain_community.llms import CTransformers
from src.helper import download_hf_embeddings, text_split, download_hf_model
from langchain_community.vectorstores import Pinecone as LangchainPinecone
import os
from dotenv import load_dotenv
from src.prompt import prompt_template
from langchain.chains import RetrievalQA
import time
from pinecone import Pinecone
from tqdm.auto import tqdm
# Load environment variables
load_dotenv()
PINECONE_API_KEY = os.getenv('PINECONE_API_KEY')
index_name = "medicure-chatbot"
# Set page configuration
st.set_page_config(page_title="Medical Chatbot", page_icon="π₯", layout="wide")
# Custom CSS for styling
st.markdown("""
<style>
.stApp {
background-color: #f0f8ff;
}
.stButton>button {
background-color: #4CAF50;
color: white;
border-radius: 20px;
border: none;
padding: 10px 20px;
transition: all 0.3s ease;
}
.stButton>button:hover {
background-color: #333;
transform: scale(1.05);
color:#fff;
}
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: #f0f8ff ;
color: #333;
text-align: center;
}
.social-icons a {
color: #333;
margin: 0 10px;
font-size: 24px;
}
.social-icons a>social-icons a:hover {
color: #4CAF50;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state for chat history
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
# Header
st.title("π₯ Medicure RAG Chatbot")
# Display welcome message
st.write("Welcome to Medicure Chatbot! Ask any medical question and I'll do my best to help you.")
st.write("#### Built with π€ Ctransformers, Langchain, and Pinecone VectorDB. Powered by Metal-llama2-7b-chat quantized LLM")
st.write("##### Resource Used π : The Gale Encyclopedia of Medicine ")
# Parameters section
st.sidebar.header("Parameters")
k_value = st.sidebar.slider("Number of relevant documents (k)", min_value=1, max_value=10, value=2)
max_new_tokens = st.sidebar.slider("Max new tokens", min_value=64, max_value=1024, value=512)
temperature = st.sidebar.slider("Temperature", min_value=0.1, max_value=1.0, value=0.8, step=0.1)
# Initialize the chatbot components
@st.cache_resource
def initialize_chatbot(k, max_tokens, temp):
embeddings = download_hf_embeddings()
model_path = "TheBloke/Llama-2-7B-Chat-GGML"
llm = CTransformers(model=model_path,
model_type="llama",
config={'max_new_tokens': max_tokens,
'temperature': temp})
# initialize pinecone
pc = Pinecone(api_key=PINECONE_API_KEY)
index = pc.Index(index_name)
PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
chain_type_kwargs = {"prompt": PROMPT}
docsearch = LangchainPinecone(index, embeddings.embed_query, "text")
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=docsearch.as_retriever(search_kwargs={'k': k}),
return_source_documents=True,
chain_type_kwargs=chain_type_kwargs)
return qa
qa = initialize_chatbot(k_value, max_new_tokens, temperature)
# Chat interface
user_input = st.text_input("Ask your question:")
if st.button("Send", key="send"):
if user_input:
# Create a progress bar
progress_bar = st.progress(0)
total_steps = 100
for i in range(total_steps):
time.sleep(0.05)
progress_bar.progress((i + 1) / total_steps)
# Get the actual response
result = qa({"query": user_input})
response = result["result"]
# Clear the progress bar
progress_bar.empty()
st.session_state.chat_history.append(("You", user_input))
st.session_state.chat_history.append(("Bot", response))
# Display chat history
st.subheader("Chat History")
for role, message in st.session_state.chat_history:
if role == "You":
st.markdown(f"**You:** {message}")
else:
st.markdown(f"**Bot:** {message}")
# Footer with social links
st.markdown("""
<div class="footer">
<div class="social-icons">
<a href="https://github.com/4darsh-Dev" target="_blank"><i class="fab fa-github"></i></a>
<a href="https://linkedin.com/in/adarsh-maurya-dev" target="_blank"><i class="fab fa-linkedin"></i></a>
<a href="https://adarshmaurya.onionreads.com" target="_blank"><i class="fas fa-globe"></i></a>
<a href="https://www.kaggle.com/adarshm09" target="_blank"><i class="fab fa-kaggle"></i></a>
</div>
<p> <p style="text-align:center;">Made with β€οΈ by <a href="https://www.adarshmaurya.onionreads.com">Adarsh Maurya</a></p> </p>
</div>
""", unsafe_allow_html=True)
# Load Font Awesome for icons
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) |