Spaces:
Sleeping
Sleeping
feat: demo app
#1
by
micpst
- opened
- app.py +73 -0
- clients.db +0 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import datetime
|
| 3 |
+
from typing import Annotated
|
| 4 |
+
|
| 5 |
+
import dbally
|
| 6 |
+
import sqlalchemy
|
| 7 |
+
from dbally import SqlAlchemyBaseView
|
| 8 |
+
from dbally.audit import CLIEventHandler
|
| 9 |
+
from dbally.embeddings import LiteLLMEmbeddingClient
|
| 10 |
+
from dbally.gradio import create_gradio_interface
|
| 11 |
+
from dbally.llms import LiteLLM
|
| 12 |
+
from dbally.similarity import SimilarityIndex, SimpleSqlAlchemyFetcher, FaissStore
|
| 13 |
+
from dbally.views import decorators
|
| 14 |
+
from dotenv import load_dotenv
|
| 15 |
+
from sqlalchemy import create_engine
|
| 16 |
+
from sqlalchemy.ext.automap import automap_base
|
| 17 |
+
|
| 18 |
+
dbally.event_handlers = [CLIEventHandler()]
|
| 19 |
+
|
| 20 |
+
engine = create_engine('sqlite:///clients.db')
|
| 21 |
+
load_dotenv()
|
| 22 |
+
|
| 23 |
+
Base = automap_base()
|
| 24 |
+
Base.prepare(autoload_with=engine)
|
| 25 |
+
Clients = Base.classes.clients
|
| 26 |
+
|
| 27 |
+
cities_fetcher = SimpleSqlAlchemyFetcher(
|
| 28 |
+
sqlalchemy_engine=engine,
|
| 29 |
+
table=Clients,
|
| 30 |
+
column=Clients.city,
|
| 31 |
+
)
|
| 32 |
+
cities_store = FaissStore(
|
| 33 |
+
index_dir="indexes",
|
| 34 |
+
index_name="cities_index",
|
| 35 |
+
embedding_client=LiteLLMEmbeddingClient("text-embedding-3-small"),
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
CityIndex = SimilarityIndex(
|
| 39 |
+
fetcher=cities_fetcher,
|
| 40 |
+
store=cities_store,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class ClientsView(SqlAlchemyBaseView):
|
| 45 |
+
|
| 46 |
+
def get_select(self) -> sqlalchemy.Select:
|
| 47 |
+
return sqlalchemy.select(Clients)
|
| 48 |
+
|
| 49 |
+
@decorators.view_filter()
|
| 50 |
+
def filter_by_city(self, city: Annotated[str, CityIndex]):
|
| 51 |
+
return Clients.city == city
|
| 52 |
+
|
| 53 |
+
@decorators.view_filter()
|
| 54 |
+
def eligible_for_loyalty_program(self):
|
| 55 |
+
total_orders_check = Clients.total_orders > 3
|
| 56 |
+
date_joined_check = Clients.date_joined < (datetime.datetime.now() - datetime.timedelta(days=365))
|
| 57 |
+
return total_orders_check & date_joined_check
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
async def main() -> None:
|
| 61 |
+
llm = LiteLLM(model_name="gpt-4-turbo")
|
| 62 |
+
|
| 63 |
+
collection = dbally.create_collection("clients", llm=llm)
|
| 64 |
+
collection.add(ClientsView, lambda: ClientsView(engine))
|
| 65 |
+
|
| 66 |
+
await collection.update_similarity_indexes()
|
| 67 |
+
|
| 68 |
+
interface = create_gradio_interface(collection)
|
| 69 |
+
interface.launch()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
asyncio.run(main())
|
clients.db
ADDED
|
Binary file (8.19 kB). View file
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
dbally[litellm,faiss,gradio]==0.7.0
|