suriya7 commited on
Commit
f8d0caa
1 Parent(s): e9e4815

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -5
app.py CHANGED
@@ -6,6 +6,10 @@ 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"])
@@ -14,7 +18,11 @@ 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:
@@ -44,8 +52,10 @@ def conversational_chat(query):
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
 
@@ -64,6 +74,9 @@ if 'past' not in st.session_state:
64
 
65
  if 'docs' not in st.session_state:
66
  st.session_state['docs'] = []
 
 
 
67
 
68
  def get_pdf_text(pdf_docs):
69
  text = ""
@@ -73,14 +86,28 @@ def get_pdf_text(pdf_docs):
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()
 
6
  from langchain.prompts import PromptTemplate
7
  from langchain import LLMChain
8
  from langchain_google_genai import ChatGoogleGenerativeAI
9
+ import nest_asyncio
10
+ from langchain.document_loaders import WebBaseLoader
11
+
12
+ nest_asyncio.apply()
13
 
14
  os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
15
  genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
 
18
  temperature=0.4)
19
 
20
 
21
+
22
+
23
+ template = """You are a friendly chatbot called "CRETA" who give clear an well having a conversation with a human and you are created by suriya an AI Enthusiastic.
24
+ provied_url_extracted_text:
25
+ {extracted_text}
26
  provided document:
27
  {provided_docs}
28
  previous_chat:
 
52
  for j in st.session_state["docs"]:
53
  if j is not None:
54
  docs += j
55
+
56
+ ex_text = st.session_state["extracted_text"]
57
  provided_docs = docs
58
+ result = llm_chain.predict(chat_history=previous_response, human_input=query, provided_docs=provided_docs,extracted_text=ex_text)
59
  st.session_state['history'].append((query, result))
60
  return result
61
 
 
74
 
75
  if 'docs' not in st.session_state:
76
  st.session_state['docs'] = []
77
+
78
+ if "extracted_text" not in st.session_state:
79
+ st.session_state["extracted_text"] = ""
80
 
81
  def get_pdf_text(pdf_docs):
82
  text = ""
 
86
  text += page.extract_text()
87
  return text
88
 
89
+ def get_url_text(url_link):
90
+ website_url = url_link
91
+ loader = WebBaseLoader(website_url)
92
+ loader.requests_per_second = 1
93
+ docs = loader.aload()
94
+ extracted_text = ""
95
+ for page in docs:
96
+ extracted_text+=page.page_content
97
+
98
+ return extracted_text
99
+
100
+
101
  with st.sidebar:
102
  st.title("Add a file for CRETA memory:")
103
  uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
104
  uploaded_url = st.text_area("please upload an url..")
105
  if st.button("Submit & Process"):
106
+ if uploaded_file or uploaded_url:
107
+ with st.spinner("Processing..."):
108
+ st.session_state["docs"] += get_pdf_text(uploaded_file)
109
+ st.session_state["docs"] += get_url_text(uploaded_url)
110
+ st.success("Done")
111
 
112
  # Create containers for chat history and user input
113
  response_container = st.container()