Kieran Gookey commited on
Commit
8b6399b
·
1 Parent(s): 2ee7baf

Added new streamlit app

Browse files
Files changed (2) hide show
  1. app.py +52 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from io import StringIO
4
+ from llama_index.llms import HuggingFaceInferenceAPI
5
+ from llama_index.embeddings import HuggingFaceInferenceAPIEmbedding
6
+ from llama_index import ServiceContext, VectorStoreIndex
7
+ from llama_index.schema import Document
8
+ import uuid
9
+ from llama_index.vector_stores.types import MetadataFilters, ExactMatchFilter
10
+
11
+ inference_api_key = st.secrets["INFRERENCE_API_TOKEN"]
12
+
13
+ llm = HuggingFaceInferenceAPI(
14
+ model_name="mistralai/Mistral-7B-Instruct-v0.2", token=inference_api_key)
15
+
16
+ embed_model = HuggingFaceInferenceAPIEmbedding(
17
+ model_name="Gooly/gte-small-en-fine-tuned-e-commerce",
18
+ token=inference_api_key,
19
+ model_kwargs={"device": ""},
20
+ encode_kwargs={"normalize_embeddings": True},
21
+ )
22
+
23
+ service_context = ServiceContext.from_defaults(
24
+ embed_model=embed_model, llm=llm)
25
+
26
+ html_file = st.file_uploader("Upload a html file", type=["html"])
27
+
28
+ if html_file is not None:
29
+ stringio = StringIO(html_file.getvalue().decode("utf-8"))
30
+ st.write(stringio)
31
+
32
+ string_data = stringio.read()
33
+ st.write(string_data)
34
+
35
+ document_id = uuid.uuid4()
36
+
37
+ document = Document(text=string_data)
38
+ document.metadata["id"] = document_id
39
+ documents = [document]
40
+
41
+ filters = MetadataFilters(
42
+ filters=[ExactMatchFilter(key="id", value=document_id)])
43
+
44
+ index = VectorStoreIndex.from_documents(
45
+ documents, show_progress=True, metadata={"source": "HTML"}, service_context=service_context)
46
+
47
+ query_engine = index.as_query_engine(
48
+ filters=filters, service_context=service_context)
49
+
50
+ response = query_engine.query("What is the current price of this product?")
51
+
52
+ st.write(response)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ llama_index
3
+ uuid