Spaces:
Sleeping
Sleeping
Mr-Vicky-01
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ 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"])
|
@@ -73,14 +74,38 @@ 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 |
-
|
79 |
-
|
80 |
-
with st.spinner("Processing..."):
|
81 |
-
st.session_state["docs"] += get_pdf_text(uploaded_file)
|
82 |
-
st.success("Done")
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
# Create containers for chat history and user input
|
85 |
response_container = st.container()
|
86 |
container = st.container()
|
@@ -101,4 +126,5 @@ if st.session_state['generated']:
|
|
101 |
for i in range(len(st.session_state['generated'])):
|
102 |
if i != 0:
|
103 |
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="adventurer")
|
104 |
-
message(st.session_state["generated"][i], key=str(i), avatar_style="bottts")
|
|
|
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
from langchain import LLMChain
|
8 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
9 |
+
from langchain_community.document_loaders import WebBaseLoader
|
10 |
|
11 |
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
12 |
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
|
|
74 |
text += page.extract_text()
|
75 |
return text
|
76 |
|
77 |
+
def get_url_text(url_link):
|
78 |
+
try:
|
79 |
+
loader = WebBaseLoader(url_link)
|
80 |
+
loader.requests_per_second = 1
|
81 |
+
docs = loader.aload()
|
82 |
+
extracted_text = ""
|
83 |
+
for page in docs:
|
84 |
+
extracted_text += page.page_content
|
85 |
+
return extracted_text
|
86 |
+
except Exception as e:
|
87 |
+
print(f"Error fetching or processing URL: {e}")
|
88 |
+
return ""
|
89 |
+
|
90 |
with st.sidebar:
|
91 |
st.title("Add a file for CRETA memory:")
|
92 |
+
uploaded_files = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
|
93 |
+
uploaded_url = st.text_input("Paste the Documentation URL:")
|
|
|
|
|
|
|
94 |
|
95 |
+
if st.button("Submit & Process"):
|
96 |
+
if uploaded_files or uploaded_url:
|
97 |
+
with st.spinner("Processing..."):
|
98 |
+
if uploaded_files:
|
99 |
+
st.session_state["docs"] += get_pdf_text(uploaded_files)
|
100 |
+
|
101 |
+
if uploaded_url:
|
102 |
+
st.session_state["docs"] += get_url_text(uploaded_url)
|
103 |
+
|
104 |
+
st.success("Processing complete!")
|
105 |
+
else:
|
106 |
+
st.error("Please upload at least one PDF file or provide a URL.")
|
107 |
+
|
108 |
+
|
109 |
# Create containers for chat history and user input
|
110 |
response_container = st.container()
|
111 |
container = st.container()
|
|
|
126 |
for i in range(len(st.session_state['generated'])):
|
127 |
if i != 0:
|
128 |
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="adventurer")
|
129 |
+
message(st.session_state["generated"][i], key=str(i), avatar_style="bottts")
|
130 |
+
|