Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
-
from gradio import Button
|
4 |
#from langchain.document_loaders import UnstructuredPDFLoader
|
5 |
from langchain.document_loaders import PyPDFLoader
|
6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
@@ -86,6 +85,11 @@ def generate_random_string(length):
|
|
86 |
return ''.join(random.choice(letters) for i in range(length))
|
87 |
random_string = generate_random_string(10)
|
88 |
|
|
|
|
|
|
|
|
|
|
|
89 |
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)
|
90 |
index_name = PINECONE_INDEX_NAME
|
91 |
#index_name = pinecone.Index(index_name)
|
@@ -128,33 +132,35 @@ PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "q
|
|
128 |
|
129 |
chain = load_qa_chain(llm=llm, chain_type="stuff", prompt=PROMPT)
|
130 |
|
131 |
-
#chain = load_qa_chain(llm=llm, chain_type="stuff")
|
132 |
|
133 |
-
def run_chain(
|
134 |
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)
|
135 |
if user_query !="" and not user_query.strip().isspace() and not user_query.isspace():
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
148 |
else:
|
149 |
-
|
150 |
|
151 |
-
def delete_index_namespace():
|
152 |
-
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)
|
153 |
-
index_namespace_to_delete = pinecone.Index(index_name=index_name)
|
154 |
-
index_namespace_to_delete.delete(delete_all=True, namespace=namespace)
|
|
|
|
|
|
|
155 |
|
156 |
-
delete_button =
|
157 |
-
|
158 |
-
|
159 |
-
iface = gr.Interface(fn=run_chain, inputs=[delete_button, "text"], outputs="text", title="AI Response")
|
160 |
-
iface.launch()
|
|
|
1 |
+
import atexit
|
2 |
import gradio as gr
|
|
|
3 |
#from langchain.document_loaders import UnstructuredPDFLoader
|
4 |
from langchain.document_loaders import PyPDFLoader
|
5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
85 |
return ''.join(random.choice(letters) for i in range(length))
|
86 |
random_string = generate_random_string(10)
|
87 |
|
88 |
+
def exit_handler():
|
89 |
+
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)
|
90 |
+
index_namespace_to_delete = pinecone.Index(index_name=index_name)
|
91 |
+
index_namespace_to_delete.delete(delete_all=True, namespace=namespace)
|
92 |
+
|
93 |
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)
|
94 |
index_name = PINECONE_INDEX_NAME
|
95 |
#index_name = pinecone.Index(index_name)
|
|
|
132 |
|
133 |
chain = load_qa_chain(llm=llm, chain_type="stuff", prompt=PROMPT)
|
134 |
|
135 |
+
#chain = load_qa_chain(llm=llm, chain_type="stuff")
|
136 |
|
137 |
+
def run_chain(user_query):
|
138 |
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)
|
139 |
if user_query !="" and not user_query.strip().isspace() and not user_query.isspace():
|
140 |
+
print("Your query:\n"+user_query)
|
141 |
+
vector_db_from_index = Pinecone.from_existing_index(index_name, hf_embeddings, namespace=namespace)
|
142 |
+
ss_results = vector_db_from_index.similarity_search(query=user_query, namespace=namespace, k=5)
|
143 |
+
initial_ai_response = chain.run(input_documents=ss_results, question=user_query, return_only_outputs=True)
|
144 |
+
#initial_ai_response=chain({"input_documents": ss_results, "question": user_query}, return_only_outputs=True)
|
145 |
+
temp_ai_response = initial_ai_response.partition('<|end|>')[0]
|
146 |
+
final_ai_response = temp_ai_response.replace('\n', '')
|
147 |
+
print(final_ai_response)
|
148 |
+
print(index_status)
|
149 |
+
print(index_name_extracted)
|
150 |
+
print(namespace)
|
151 |
+
print("****************")
|
152 |
+
return final_ai_response
|
153 |
else:
|
154 |
+
print("Invalid inputs.")
|
155 |
|
156 |
+
#def delete_index_namespace():
|
157 |
+
# pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)
|
158 |
+
# index_namespace_to_delete = pinecone.Index(index_name=index_name)
|
159 |
+
# index_namespace_to_delete.delete(delete_all=True, namespace=namespace)
|
160 |
+
|
161 |
+
iface = gr.Interface(fn=run_chain, inputs="text", outputs="text", title="AI Response")
|
162 |
+
iface.launch()
|
163 |
|
164 |
+
delete_button = Button("Exit & Clear Data")
|
165 |
+
if delete_button:
|
166 |
+
atexit.register(exit_handler)
|
|
|
|