Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -103,28 +103,31 @@ def extract_text(file):
|
|
| 103 |
# Add the text from each page to the text string
|
| 104 |
text += page.extract_text()
|
| 105 |
|
| 106 |
-
return text
|
| 107 |
|
| 108 |
-
|
| 109 |
-
def ask_ai_upload(doc, question):
|
| 110 |
-
|
| 111 |
-
text, file_name = extract_text(doc)
|
| 112 |
-
index = construct_index([Document(text)])
|
| 113 |
-
index2 = index
|
| 114 |
-
index2.storage_context.persist(f'{file_name}')
|
| 115 |
-
storage_context2 = StorageContext.from_defaults(persist_dir=f'{file_name}')
|
| 116 |
-
|
| 117 |
-
# Load index
|
| 118 |
-
index2 = load_index_from_storage(storage_context2)
|
| 119 |
-
|
| 120 |
-
# Save index to Azure blob storage
|
| 121 |
-
index.storage_context.persist(f'gpt/storage_demo/{file_name}', fs=fs)
|
| 122 |
-
|
| 123 |
-
# Rebuild storage context
|
| 124 |
-
storage_context = StorageContext.from_defaults(persist_dir=f'gpt/storage_demo/{file_name}', fs=fs)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
# Define the query & the querying method
|
| 130 |
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8))
|
|
|
|
| 103 |
# Add the text from each page to the text string
|
| 104 |
text += page.extract_text()
|
| 105 |
|
| 106 |
+
return text
|
| 107 |
|
| 108 |
+
def extract_name(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
return os.path.basename(file.name)
|
| 111 |
+
|
| 112 |
+
def ask_ai_upload(doc, question):
|
| 113 |
+
file_name = extract_name(doc)
|
| 114 |
+
try:
|
| 115 |
+
storage_context = StorageContext.from_defaults(persist_dir=f'gpt/storage_demo/{file_name}', fs=fs)
|
| 116 |
+
# Load index
|
| 117 |
+
index = load_index_from_storage(storage_context)
|
| 118 |
+
except:
|
| 119 |
+
text = extract_text(doc)
|
| 120 |
+
index = construct_index([Document(text)])
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# Save index to Azure blob storage
|
| 124 |
+
index.storage_context.persist(f'gpt/storage_demo/{file_name}', fs=fs)
|
| 125 |
+
|
| 126 |
+
# Rebuild storage context
|
| 127 |
+
storage_context = StorageContext.from_defaults(persist_dir=f'gpt/storage_demo/{file_name}', fs=fs)
|
| 128 |
+
|
| 129 |
+
# Load index
|
| 130 |
+
index = load_index_from_storage(storage_context)
|
| 131 |
|
| 132 |
# Define the query & the querying method
|
| 133 |
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8))
|