Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -41,12 +41,12 @@ import tempfile
|
|
41 |
import warnings
|
42 |
warnings.filterwarnings('ignore')
|
43 |
|
44 |
-
|
45 |
-
|
46 |
# openai.api_key = "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"
|
47 |
global openai_api_key
|
48 |
-
openai_api_key = "sk-
|
49 |
-
os.environ['OPENAI_API_KEY'] = "sk-
|
50 |
|
51 |
@st.cache_data
|
52 |
def parse_pdf (file: io.BytesIO)-> List[str]:
|
@@ -82,7 +82,7 @@ def text_to_docs(text: str) -> List [Document]:
|
|
82 |
doc_chunks = []
|
83 |
for doc in page_docs:
|
84 |
text_splitter = RecursiveCharacterTextSplitter(
|
85 |
-
chunk_size=
|
86 |
separators=["\n\n", "\n", ".", "!", "?", ",", " ", ""],
|
87 |
chunk_overlap=0,
|
88 |
)
|
@@ -108,7 +108,8 @@ def tool(index):
|
|
108 |
name="State of Union QA System",
|
109 |
func=qa.run,
|
110 |
description="Useful for when you need to answer questions about the aspects asked.\
|
111 |
-
Input may be a partial or fully formed question
|
|
|
112 |
)
|
113 |
]
|
114 |
return tools,qa
|
@@ -150,11 +151,12 @@ def process(kind, tools, qa):
|
|
150 |
agent_chain = AgentExecutor.from_agent_and_tools(
|
151 |
agent=agent, tools=tools, verbose=True, memory=st.session_state.memory
|
152 |
)
|
|
|
153 |
|
154 |
|
155 |
option = st.sidebar.selectbox(
|
156 |
'What do you want to ?',
|
157 |
-
('
|
158 |
|
159 |
api = st.sidebar.text_input(
|
160 |
"Open api key",
|
@@ -163,7 +165,7 @@ api = st.sidebar.text_input(
|
|
163 |
help="https://platform.openai.com/account/api-keys",
|
164 |
)
|
165 |
uploaded_file = st.sidebar.file_uploader(":blue[Upload]", type=["pdf"])
|
166 |
-
|
167 |
if api:
|
168 |
if option == "Sumarization":
|
169 |
|
@@ -188,9 +190,34 @@ if api:
|
|
188 |
index = FAISS.from_documents(pages, embeddings)
|
189 |
|
190 |
tools,qa = tool(index)
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
|
196 |
container = st.container()
|
@@ -213,6 +240,7 @@ if api:
|
|
213 |
st.session_state.messages.append({"role": "user", "content": query})
|
214 |
|
215 |
# response=llm_chain.memory.chat_memory.add_user_message(prompt)
|
|
|
216 |
if len(api) == 0:
|
217 |
response = f"""I will answer the question "{query}" if you give the API key"""
|
218 |
# st.write(response)
|
@@ -226,6 +254,7 @@ if api:
|
|
226 |
else:
|
227 |
|
228 |
with st.spinner("It's indexing. .."):
|
|
|
229 |
response = agent_chain.run(query)
|
230 |
# st.write(response)
|
231 |
# #f"Echo: {prompt}" get_completion(template_string) #
|
@@ -234,8 +263,8 @@ if api:
|
|
234 |
st.markdown(response)
|
235 |
# Add assistant response to chat history
|
236 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
237 |
-
|
238 |
-
|
239 |
|
240 |
elif option == "Chat":
|
241 |
|
|
|
41 |
import warnings
|
42 |
warnings.filterwarnings('ignore')
|
43 |
|
44 |
+
from dotenv import load_dotenv, find_dotenv
|
45 |
+
_ = load_dotenv(find_dotenv())
|
46 |
# openai.api_key = "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"
|
47 |
global openai_api_key
|
48 |
+
openai_api_key = "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"
|
49 |
+
os.environ['OPENAI_API_KEY'] = "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"
|
50 |
|
51 |
@st.cache_data
|
52 |
def parse_pdf (file: io.BytesIO)-> List[str]:
|
|
|
82 |
doc_chunks = []
|
83 |
for doc in page_docs:
|
84 |
text_splitter = RecursiveCharacterTextSplitter(
|
85 |
+
chunk_size=2500,
|
86 |
separators=["\n\n", "\n", ".", "!", "?", ",", " ", ""],
|
87 |
chunk_overlap=0,
|
88 |
)
|
|
|
108 |
name="State of Union QA System",
|
109 |
func=qa.run,
|
110 |
description="Useful for when you need to answer questions about the aspects asked.\
|
111 |
+
Input may be a partial or fully formed question,\
|
112 |
+
it also can be about some things else, use the chat history to reply the questions"
|
113 |
)
|
114 |
]
|
115 |
return tools,qa
|
|
|
151 |
agent_chain = AgentExecutor.from_agent_and_tools(
|
152 |
agent=agent, tools=tools, verbose=True, memory=st.session_state.memory
|
153 |
)
|
154 |
+
return agent_chain,llm_chain
|
155 |
|
156 |
|
157 |
option = st.sidebar.selectbox(
|
158 |
'What do you want to ?',
|
159 |
+
('Sumarization','Chat'))
|
160 |
|
161 |
api = st.sidebar.text_input(
|
162 |
"Open api key",
|
|
|
165 |
help="https://platform.openai.com/account/api-keys",
|
166 |
)
|
167 |
uploaded_file = st.sidebar.file_uploader(":blue[Upload]", type=["pdf"])
|
168 |
+
global agent_chain,llm_chain
|
169 |
if api:
|
170 |
if option == "Sumarization":
|
171 |
|
|
|
190 |
index = FAISS.from_documents(pages, embeddings)
|
191 |
|
192 |
tools,qa = tool(index)
|
193 |
+
prefix=""""Have a conversation with a human, answering the human questions as best you can based on the context and memory available. \
|
194 |
+
He may ask some not about the context but just answer the the question with a short sentence"""
|
195 |
+
suffix="""Begin!"
|
196 |
+
{chat_history}
|
197 |
+
Question: {input}
|
198 |
+
{agent_scratchpad}"""
|
199 |
+
prompt = ZeroShotAgent.create_prompt(
|
200 |
+
tools,
|
201 |
+
prefix=prefix,
|
202 |
+
suffix=suffix,
|
203 |
+
input_variables=["input", "chat_history", "agent_scratchpad"],
|
204 |
+
)
|
205 |
|
206 |
+
if "memory" not in st.session_state:
|
207 |
+
st.session_state.memory = ConversationBufferMemory(memory_key ="chat_history")
|
208 |
+
#Chain
|
209 |
+
# ZeroShotAgent
|
210 |
+
llm_chain = LLMChain(
|
211 |
+
llm=OpenAI(
|
212 |
+
temperature=0, openai_api_key=api, model_name="gpt-3.5-turbo"
|
213 |
+
),
|
214 |
+
prompt=prompt,
|
215 |
+
)
|
216 |
+
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
|
217 |
+
agent_chain = AgentExecutor.from_agent_and_tools(
|
218 |
+
agent=agent, tools=tools, verbose=True, memory=st.session_state.memory
|
219 |
+
)
|
220 |
+
# agent_chain,llm_chain = process("Sumarized",tools, qa)
|
221 |
|
222 |
|
223 |
container = st.container()
|
|
|
240 |
st.session_state.messages.append({"role": "user", "content": query})
|
241 |
|
242 |
# response=llm_chain.memory.chat_memory.add_user_message(prompt)
|
243 |
+
|
244 |
if len(api) == 0:
|
245 |
response = f"""I will answer the question "{query}" if you give the API key"""
|
246 |
# st.write(response)
|
|
|
254 |
else:
|
255 |
|
256 |
with st.spinner("It's indexing. .."):
|
257 |
+
|
258 |
response = agent_chain.run(query)
|
259 |
# st.write(response)
|
260 |
# #f"Echo: {prompt}" get_completion(template_string) #
|
|
|
263 |
st.markdown(response)
|
264 |
# Add assistant response to chat history
|
265 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
266 |
+
# with st.expander("History/Memory"):
|
267 |
+
# st.write(st.session_state.memory)
|
268 |
|
269 |
elif option == "Chat":
|
270 |
|