Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,29 @@
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
from streamlit_chat import message
|
|
|
4 |
import google.generativeai as genai
|
5 |
from langchain.prompts import PromptTemplate
|
6 |
from langchain import LLMChain
|
7 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
8 |
|
|
|
9 |
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
10 |
|
11 |
llm = ChatGoogleGenerativeAI(model="gemini-pro",
|
12 |
-
temperature=0.
|
13 |
|
14 |
|
15 |
-
template = """You are a friendly
|
|
|
|
|
16 |
previous_chat:
|
17 |
{chat_history}
|
18 |
Human: {human_input}
|
19 |
Chatbot:"""
|
20 |
|
21 |
prompt = PromptTemplate(
|
22 |
-
input_variables=["chat_history", "human_input"], template=template
|
23 |
)
|
24 |
|
25 |
llm_chain = LLMChain(
|
@@ -30,17 +34,23 @@ llm_chain = LLMChain(
|
|
30 |
|
31 |
|
32 |
previous_response = ""
|
|
|
33 |
def conversational_chat(query):
|
34 |
-
global previous_response
|
35 |
for i in st.session_state['history']:
|
36 |
if i is not None:
|
37 |
previous_response += f"Human: {i[0]}\n Chatbot: {i[1]}"
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
40 |
st.session_state['history'].append((query, result))
|
41 |
return result
|
42 |
|
43 |
-
st.title("
|
|
|
44 |
|
45 |
if 'history' not in st.session_state:
|
46 |
st.session_state['history'] = []
|
@@ -52,6 +62,26 @@ if 'generated' not in st.session_state:
|
|
52 |
if 'past' not in st.session_state:
|
53 |
st.session_state['past'] = [" "]
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
# Create containers for chat history and user input
|
56 |
response_container = st.container()
|
57 |
container = st.container()
|
@@ -64,7 +94,8 @@ with container:
|
|
64 |
# answer = response_generator(output)
|
65 |
st.session_state['past'].append(user_input)
|
66 |
st.session_state['generated'].append(output)
|
67 |
-
|
|
|
68 |
# Display chat history
|
69 |
if st.session_state['generated']:
|
70 |
with response_container:
|
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
from streamlit_chat import message
|
4 |
+
from PyPDF2 import PdfReader
|
5 |
import google.generativeai as genai
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
from langchain import LLMChain
|
8 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
9 |
|
10 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
11 |
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
12 |
|
13 |
llm = ChatGoogleGenerativeAI(model="gemini-pro",
|
14 |
+
temperature=0.4)
|
15 |
|
16 |
|
17 |
+
template = """You are a friendly chat assistant called "CRETA" having a conversation with a human and you are created by Pachaiappan an AI Specialist.
|
18 |
+
provided document:
|
19 |
+
{provided_docs}
|
20 |
previous_chat:
|
21 |
{chat_history}
|
22 |
Human: {human_input}
|
23 |
Chatbot:"""
|
24 |
|
25 |
prompt = PromptTemplate(
|
26 |
+
input_variables=["chat_history", "human_input", "provided_docs"], template=template
|
27 |
)
|
28 |
|
29 |
llm_chain = LLMChain(
|
|
|
34 |
|
35 |
|
36 |
previous_response = ""
|
37 |
+
provided_docs = ""
|
38 |
def conversational_chat(query):
|
39 |
+
global previous_response, provided_docs
|
40 |
for i in st.session_state['history']:
|
41 |
if i is not None:
|
42 |
previous_response += f"Human: {i[0]}\n Chatbot: {i[1]}"
|
43 |
+
docs = ""
|
44 |
+
for j in st.session_state["docs"]:
|
45 |
+
if j is not None:
|
46 |
+
docs += j
|
47 |
+
provided_docs = docs
|
48 |
+
result = llm_chain.predict(chat_history=previous_response, human_input=query, provided_docs=provided_docs)
|
49 |
st.session_state['history'].append((query, result))
|
50 |
return result
|
51 |
|
52 |
+
st.title("Chat Bot:")
|
53 |
+
st.text("I am CRETA Your Friendly Assitant")
|
54 |
|
55 |
if 'history' not in st.session_state:
|
56 |
st.session_state['history'] = []
|
|
|
62 |
if 'past' not in st.session_state:
|
63 |
st.session_state['past'] = [" "]
|
64 |
|
65 |
+
if 'docs' not in st.session_state:
|
66 |
+
st.session_state['docs'] = []
|
67 |
+
|
68 |
+
def get_pdf_text(pdf_docs):
|
69 |
+
text = ""
|
70 |
+
for pdf in pdf_docs:
|
71 |
+
pdf_reader = PdfReader(pdf)
|
72 |
+
for page in pdf_reader.pages:
|
73 |
+
text += page.extract_text()
|
74 |
+
return text
|
75 |
+
|
76 |
+
with st.sidebar:
|
77 |
+
st.title("Add a file for CRETA memory:")
|
78 |
+
uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
|
79 |
+
uploaded_url = st.text_area("please upload an url..")
|
80 |
+
if st.button("Submit & Process"):
|
81 |
+
with st.spinner("Processing..."):
|
82 |
+
st.session_state["docs"] += get_pdf_text(uploaded_file)
|
83 |
+
st.success("Done")
|
84 |
+
|
85 |
# Create containers for chat history and user input
|
86 |
response_container = st.container()
|
87 |
container = st.container()
|
|
|
94 |
# answer = response_generator(output)
|
95 |
st.session_state['past'].append(user_input)
|
96 |
st.session_state['generated'].append(output)
|
97 |
+
|
98 |
+
|
99 |
# Display chat history
|
100 |
if st.session_state['generated']:
|
101 |
with response_container:
|