Spaces:
Sleeping
Sleeping
adarsh
commited on
Commit
β’
9c97c75
1
Parent(s):
dfe7961
added chatbot feature
Browse files
app.py
CHANGED
@@ -1,5 +1,126 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from langchain.prompts import PromptTemplate
|
3 |
+
from langchain.llms import CTransformers
|
4 |
+
from src.helper import download_hf_embeddings, text_split, download_hf_model
|
5 |
+
from langchain.vectorstores import Pinecone
|
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 |
|
12 |
+
# Load environment variables
|
13 |
+
load_dotenv()
|
14 |
+
PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')
|
15 |
|
16 |
+
# Set page configuration
|
17 |
+
st.set_page_config(page_title="Medical Chatbot", page_icon="π₯", layout="wide")
|
18 |
+
|
19 |
+
# Custom CSS for styling
|
20 |
+
st.markdown("""
|
21 |
+
<style>
|
22 |
+
.stApp {
|
23 |
+
background-color: #f0f8ff;
|
24 |
+
}
|
25 |
+
.stButton>button {
|
26 |
+
background-color: #4CAF50;
|
27 |
+
color: white;
|
28 |
+
border-radius: 20px;
|
29 |
+
border: none;
|
30 |
+
padding: 10px 20px;
|
31 |
+
transition: all 0.3s ease;
|
32 |
+
}
|
33 |
+
.stButton>button:hover {
|
34 |
+
background-color: #45a049;
|
35 |
+
transform: scale(1.05);
|
36 |
+
}
|
37 |
+
.footer {
|
38 |
+
position: fixed;
|
39 |
+
left: 0;
|
40 |
+
bottom: 0;
|
41 |
+
width: 100%;
|
42 |
+
background-color: #333;
|
43 |
+
color: white;
|
44 |
+
text-align: center;
|
45 |
+
padding: 10px 0;
|
46 |
+
}
|
47 |
+
.social-icons a {
|
48 |
+
color: white;
|
49 |
+
margin: 0 10px;
|
50 |
+
font-size: 24px;
|
51 |
+
}
|
52 |
+
</style>
|
53 |
+
""", unsafe_allow_html=True)
|
54 |
+
|
55 |
+
# Initialize session state for chat history
|
56 |
+
if 'chat_history' not in st.session_state:
|
57 |
+
st.session_state.chat_history = []
|
58 |
+
|
59 |
+
# Header
|
60 |
+
st.title("π₯ Medical Chatbot")
|
61 |
+
|
62 |
+
# Initialize the chatbot components
|
63 |
+
@st.cache_resource
|
64 |
+
def initialize_chatbot():
|
65 |
+
embeddings = download_hf_embeddings()
|
66 |
+
model_name_or_path = "TheBloke/Llama-2-7B-Chat-GGML"
|
67 |
+
model_basename = "llama-2-7b-chat.ggmlv3.q4_0.bin"
|
68 |
+
model_path = download_hf_model(model_name_or_path, model_basename)
|
69 |
+
llm = CTransformers(model=model_path,
|
70 |
+
model_type="llama",
|
71 |
+
config={'max_new_tokens': 512,
|
72 |
+
'temperature': 0.8})
|
73 |
+
PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
74 |
+
chain_type_kwargs = {"prompt": PROMPT}
|
75 |
+
docsearch = LangchainPinecone(index, embeddings.embed_query, "text")
|
76 |
+
qa = RetrievalQA.from_chain_type(
|
77 |
+
llm=llm,
|
78 |
+
chain_type="stuff",
|
79 |
+
retriever=docsearch.as_retriever(search_kwargs={'k': 2}),
|
80 |
+
return_source_documents=True,
|
81 |
+
chain_type_kwargs=chain_type_kwargs)
|
82 |
+
return qa
|
83 |
+
|
84 |
+
qa = initialize_chatbot()
|
85 |
+
|
86 |
+
# Chat interface
|
87 |
+
user_input = st.text_input("Ask your medical question:")
|
88 |
+
if st.button("Send", key="send"):
|
89 |
+
if user_input:
|
90 |
+
with st.spinner("Thinking..."):
|
91 |
+
result = qa({"query": user_input})
|
92 |
+
response = result["result"]
|
93 |
+
st.session_state.chat_history.append(("You", user_input))
|
94 |
+
st.session_state.chat_history.append(("Bot", response))
|
95 |
+
|
96 |
+
# Display chat history
|
97 |
+
st.subheader("Chat History")
|
98 |
+
for role, message in st.session_state.chat_history:
|
99 |
+
if role == "You":
|
100 |
+
st.markdown(f"**You:** {message}")
|
101 |
+
else:
|
102 |
+
st.markdown(f"**Bot:** {message}")
|
103 |
+
|
104 |
+
# Animated loading for visual appeal
|
105 |
+
def load_animation():
|
106 |
+
with st.empty():
|
107 |
+
for i in range(3):
|
108 |
+
for j in ["β
", "β
β
", "β
β
β
", "β
β
β
β
"]:
|
109 |
+
st.write(f"Loading{j}")
|
110 |
+
time.sleep(0.2)
|
111 |
+
st.write("")
|
112 |
+
|
113 |
+
# Footer with social links
|
114 |
+
st.markdown("""
|
115 |
+
<div class="footer">
|
116 |
+
<div class="social-icons">
|
117 |
+
<a href="https://github.com/yourusername" target="_blank"><i class="fab fa-github"></i></a>
|
118 |
+
<a href="https://linkedin.com/in/yourusername" target="_blank"><i class="fab fa-linkedin"></i></a>
|
119 |
+
<a href="https://yourwebsite.com" target="_blank"><i class="fas fa-globe"></i></a>
|
120 |
+
</div>
|
121 |
+
<p>Β© 2024 Medical Chatbot. All rights reserved.</p>
|
122 |
+
</div>
|
123 |
+
""", unsafe_allow_html=True)
|
124 |
+
|
125 |
+
# Load Font Awesome for icons
|
126 |
+
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)
|