Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from langchain.prompts import (
|
| 4 |
+
ChatPromptTemplate,
|
| 5 |
+
HumanMessagePromptTemplate,
|
| 6 |
+
MessagesPlaceholder,
|
| 7 |
+
)
|
| 8 |
+
from more_itertools import chunked
|
| 9 |
+
|
| 10 |
+
from langserve import RemoteRunnable
|
| 11 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 12 |
+
import os
|
| 13 |
+
from langchain import PromptTemplate
|
| 14 |
+
from langchain import LLMChain
|
| 15 |
+
from langchain_together import Together
|
| 16 |
+
import re
|
| 17 |
+
import pdfplumber
|
| 18 |
+
# Set the API key with double quotes
|
| 19 |
+
|
| 20 |
+
os.environ['TOGETHER_API_KEY'] = "5653bbfbaf1f7c1438206f18e5dfc2f5992b8f0b6aa9796b0131ea454648ccde"
|
| 21 |
+
|
| 22 |
+
text = ""
|
| 23 |
+
max_pages = 16
|
| 24 |
+
with pdfplumber.open("/content/AI Engineer Test.pdf") as pdf:
|
| 25 |
+
for i, page in enumerate(pdf.pages):
|
| 26 |
+
if i >= max_pages:
|
| 27 |
+
break
|
| 28 |
+
text += page.extract_text() + "\n"
|
| 29 |
+
|
| 30 |
+
def Bot(Questions):
|
| 31 |
+
chat_template = """
|
| 32 |
+
Based on the provided context: {text}
|
| 33 |
+
Please answer the following question: {Questions}
|
| 34 |
+
Only provide answers that are directly related to the context. If the question is unrelated, respond with "I don't know".
|
| 35 |
+
"""
|
| 36 |
+
prompt = PromptTemplate(
|
| 37 |
+
input_variables=['text', 'Questions'],
|
| 38 |
+
template=chat_template
|
| 39 |
+
)
|
| 40 |
+
llama3 = Together(model="meta-llama/Llama-3-70b-chat-hf", max_tokens=250)
|
| 41 |
+
Generated_chat = LLMChain(llm=llama3, prompt=prompt)
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
response = Generated_chat.invoke({
|
| 45 |
+
"text": text,
|
| 46 |
+
"Questions": Questions
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
response_text = response['text']
|
| 50 |
+
|
| 51 |
+
response_text = response_text.replace("assistant", "")
|
| 52 |
+
|
| 53 |
+
# Post-processing to handle repeated words and ensure completeness
|
| 54 |
+
words = response_text.split()
|
| 55 |
+
seen = set()
|
| 56 |
+
filtered_words = [word for word in words if word.lower() not in seen and not seen.add(word.lower())]
|
| 57 |
+
response_text = ' '.join(filtered_words)
|
| 58 |
+
response_text = response_text.strip() # Ensuring no extra spaces at the ends
|
| 59 |
+
if not response_text.endswith('.'):
|
| 60 |
+
response_text += '.'
|
| 61 |
+
|
| 62 |
+
return response_text
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"Error in generating response: {e}"
|
| 65 |
+
|
| 66 |
+
def ChatBot(Questions):
|
| 67 |
+
greetings = ["hi", "hello", "hey", "greetings", "what's up", "howdy"]
|
| 68 |
+
# Check if the input question is a greeting
|
| 69 |
+
question_lower = Questions.lower().strip()
|
| 70 |
+
if question_lower in greetings or any(question_lower.startswith(greeting) for greeting in greetings):
|
| 71 |
+
return "Hello! How can I assist you with the document today?"
|
| 72 |
+
else:
|
| 73 |
+
response=Bot(Questions)
|
| 74 |
+
return response.translate(str.maketrans('', '', '\n'))
|
| 75 |
+
|
| 76 |
+
# --- Logo ---
|
| 77 |
+
st.set_page_config(
|
| 78 |
+
page_title="AI Engineer Test Chatbot",
|
| 79 |
+
page_icon="/content/Insight Therapy Solutions.png",
|
| 80 |
+
layout="wide",
|
| 81 |
+
)
|
| 82 |
+
st.sidebar.image("/content/Insight Therapy Solutions.png", width=200)
|
| 83 |
+
|
| 84 |
+
st.sidebar.title("Navigation")
|
| 85 |
+
st.sidebar.write("Reclaim Your Mental Health")
|
| 86 |
+
st.sidebar.markdown("[Visit us at](https://www.insighttherapysolutions.com/)")
|
| 87 |
+
|
| 88 |
+
rag_chain = RemoteRunnable("http://69.61.24.171:8000/rag_chain/")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
msgs = StreamlitChatMessageHistory(key="langchain_messages")
|
| 93 |
+
|
| 94 |
+
# --- Main Content ---
|
| 95 |
+
st.markdown("## 🔍 Chatbot For AI Engineer test:")
|
| 96 |
+
|
| 97 |
+
if len(msgs.messages) == 0:
|
| 98 |
+
msgs.add_ai_message("Hi! How can I assist you today?")
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
for msg in msgs.messages:
|
| 102 |
+
st.chat_message(msg.type).write(msg.content)
|
| 103 |
+
|
| 104 |
+
if prompt := st.chat_input():
|
| 105 |
+
st.chat_message("human").write(prompt)
|
| 106 |
+
|
| 107 |
+
with st.chat_message("assistant"):
|
| 108 |
+
message_placeholder = st.empty()
|
| 109 |
+
full_response = ""
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
_chat_history = st.session_state.langchain_messages[1:40]
|
| 113 |
+
_chat_history_tranform = list(
|
| 114 |
+
chunked([msg.content for msg in _chat_history], n=2)
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
response = rag_chain.stream(
|
| 118 |
+
{"question": prompt, "chat_history": _chat_history_tranform}
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
for res in response:
|
| 122 |
+
full_response += res or ""
|
| 123 |
+
message_placeholder.markdown(full_response + "|")
|
| 124 |
+
message_placeholder.markdown(full_response)
|
| 125 |
+
|
| 126 |
+
msgs.add_user_message(prompt)
|
| 127 |
+
msgs.add_ai_message(full_response)
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
st.error(f"An error occured. {e}")
|