Spaces:
Running
Running
Commit
·
f0947cd
1
Parent(s):
15047b7
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Basic similarity search example. Used in the original txtai demo.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
|
| 9 |
+
from txtai.embeddings import Embeddings
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class Application:
|
| 13 |
+
"""
|
| 14 |
+
Main application.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
def __init__(self):
|
| 18 |
+
"""
|
| 19 |
+
Creates a new application.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# Create embeddings model, backed by sentence-transformers & transformers
|
| 23 |
+
self.embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2"})
|
| 24 |
+
|
| 25 |
+
def run(self):
|
| 26 |
+
"""
|
| 27 |
+
Runs a Streamlit application.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
st.title("Similarity Search")
|
| 31 |
+
st.markdown("This application runs a basic similarity search that identifies the best matching row for a query.")
|
| 32 |
+
|
| 33 |
+
data = [
|
| 34 |
+
"US tops 5 million confirmed virus cases",
|
| 35 |
+
"Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg",
|
| 36 |
+
"Beijing mobilises invasion craft along coast as Taiwan tensions escalate",
|
| 37 |
+
"The National Park Service warns against sacrificing slower friends in a bear attack",
|
| 38 |
+
"Maine man wins $1M from $25 lottery ticket",
|
| 39 |
+
"Make huge profits without work, earn up to $100,000 a day",
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
data = st.text_area("Data", value="\n".join(data))
|
| 43 |
+
query = st.text_input("Query")
|
| 44 |
+
|
| 45 |
+
data = data.split("\n")
|
| 46 |
+
|
| 47 |
+
if query:
|
| 48 |
+
# Get index of best section that best matches query
|
| 49 |
+
uid = self.embeddings.similarity(query, data)[0][0]
|
| 50 |
+
st.write(data[uid])
|
| 51 |
+
|
| 52 |
+
@st.cache(allow_output_mutation=True)
|
| 53 |
+
def create():
|
| 54 |
+
"""
|
| 55 |
+
Creates and caches a Streamlit application.
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
Application
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
return Application()
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 66 |
+
|
| 67 |
+
# Create and run application
|
| 68 |
+
app = create()
|
| 69 |
+
app.run()
|