Spaces:
Runtime error
Runtime error
Muhammad Abdullah
commited on
Commit
·
f49d848
1
Parent(s):
d6e08c0
updating minor
Browse files
app.py
CHANGED
|
@@ -25,19 +25,6 @@ from PIL import Image
|
|
| 25 |
from llama_index.readers.file import ImageReader
|
| 26 |
|
| 27 |
|
| 28 |
-
@st.cache_resource
|
| 29 |
-
def get_file_extractor():
|
| 30 |
-
image_parser = ImageReader(keep_image=True, parse_text=True)
|
| 31 |
-
file_extractor = {
|
| 32 |
-
".jpg": image_parser,
|
| 33 |
-
".png": image_parser,
|
| 34 |
-
".jpeg": image_parser,
|
| 35 |
-
}
|
| 36 |
-
return file_extractor
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
file_extractor = get_file_extractor()
|
| 40 |
-
|
| 41 |
# Text QA templates
|
| 42 |
DEFAULT_TEXT_QA_PROMPT_TMPL = (
|
| 43 |
"Context information is below. \n"
|
|
@@ -195,7 +182,6 @@ def insert_terms(terms_to_definition):
|
|
| 195 |
@st.cache_resource
|
| 196 |
def initialize_index(llm_name, model_temperature, api_key):
|
| 197 |
"""Create the VectorStoreIndex object."""
|
| 198 |
-
# TODO update this thing in doc
|
| 199 |
Settings.llm = get_llm(llm_name, model_temperature, api_key)
|
| 200 |
|
| 201 |
# create a vector store index for each folder
|
|
@@ -204,21 +190,29 @@ def initialize_index(llm_name, model_temperature, api_key):
|
|
| 204 |
StorageContext.from_defaults(persist_dir="./initial_index")
|
| 205 |
)
|
| 206 |
except Exception as e:
|
|
|
|
| 207 |
docs = [
|
| 208 |
-
Document(text=
|
|
|
|
| 209 |
]
|
| 210 |
index = VectorStoreIndex.from_documents(docs)
|
| 211 |
index.storage_context.persist(persist_dir="./initial_index")
|
| 212 |
-
# TODO update this in docs
|
| 213 |
return index
|
| 214 |
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
st.title("🦙 Llama Index Term Extractor 🦙")
|
| 224 |
|
|
@@ -271,12 +265,12 @@ with upload_tab:
|
|
| 271 |
)
|
| 272 |
)
|
| 273 |
if uploaded_file:
|
| 274 |
-
breakpoint()
|
| 275 |
Image.open(uploaded_file).convert("RGB").save("temp.png")
|
| 276 |
img_reader = SimpleDirectoryReader(
|
| 277 |
input_files=["temp.png"], file_extractor=file_extractor
|
| 278 |
)
|
| 279 |
img_docs = img_reader.load_data()
|
|
|
|
| 280 |
terms_docs.update(
|
| 281 |
extract_terms(
|
| 282 |
img_docs,
|
|
@@ -321,10 +315,7 @@ with query_tab:
|
|
| 321 |
if "llama_index" in st.session_state:
|
| 322 |
query_text = st.text_input("Ask about a term or definition:")
|
| 323 |
if query_text:
|
| 324 |
-
query_text =
|
| 325 |
-
query_text
|
| 326 |
-
+ "\nIf you can't find the answer, answer the query with the best of your knowledge."
|
| 327 |
-
)
|
| 328 |
# breakpoint()
|
| 329 |
with st.spinner("Generating answer..."):
|
| 330 |
response = (
|
|
|
|
| 25 |
from llama_index.readers.file import ImageReader
|
| 26 |
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# Text QA templates
|
| 29 |
DEFAULT_TEXT_QA_PROMPT_TMPL = (
|
| 30 |
"Context information is below. \n"
|
|
|
|
| 182 |
@st.cache_resource
|
| 183 |
def initialize_index(llm_name, model_temperature, api_key):
|
| 184 |
"""Create the VectorStoreIndex object."""
|
|
|
|
| 185 |
Settings.llm = get_llm(llm_name, model_temperature, api_key)
|
| 186 |
|
| 187 |
# create a vector store index for each folder
|
|
|
|
| 190 |
StorageContext.from_defaults(persist_dir="./initial_index")
|
| 191 |
)
|
| 192 |
except Exception as e:
|
| 193 |
+
print(e)
|
| 194 |
docs = [
|
| 195 |
+
Document(text=f"Term: {key}\nDefinition: {value}")
|
| 196 |
+
for key, value in DEFAULT_TERMS.items()
|
| 197 |
]
|
| 198 |
index = VectorStoreIndex.from_documents(docs)
|
| 199 |
index.storage_context.persist(persist_dir="./initial_index")
|
|
|
|
| 200 |
return index
|
| 201 |
|
| 202 |
|
| 203 |
+
@st.cache_resource
|
| 204 |
+
def get_file_extractor():
|
| 205 |
+
image_parser = ImageReader(keep_image=True, parse_text=True)
|
| 206 |
+
file_extractor = {
|
| 207 |
+
".jpg": image_parser,
|
| 208 |
+
".png": image_parser,
|
| 209 |
+
".jpeg": image_parser,
|
| 210 |
+
}
|
| 211 |
+
return file_extractor
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
file_extractor = get_file_extractor()
|
| 215 |
+
|
| 216 |
|
| 217 |
st.title("🦙 Llama Index Term Extractor 🦙")
|
| 218 |
|
|
|
|
| 265 |
)
|
| 266 |
)
|
| 267 |
if uploaded_file:
|
|
|
|
| 268 |
Image.open(uploaded_file).convert("RGB").save("temp.png")
|
| 269 |
img_reader = SimpleDirectoryReader(
|
| 270 |
input_files=["temp.png"], file_extractor=file_extractor
|
| 271 |
)
|
| 272 |
img_docs = img_reader.load_data()
|
| 273 |
+
os.remove("temp.png")
|
| 274 |
terms_docs.update(
|
| 275 |
extract_terms(
|
| 276 |
img_docs,
|
|
|
|
| 315 |
if "llama_index" in st.session_state:
|
| 316 |
query_text = st.text_input("Ask about a term or definition:")
|
| 317 |
if query_text:
|
| 318 |
+
query_text = query_text
|
|
|
|
|
|
|
|
|
|
| 319 |
# breakpoint()
|
| 320 |
with st.spinner("Generating answer..."):
|
| 321 |
response = (
|