-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (61 loc) · 2.04 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from fasthtml.common import *
import pandas as pd
from sentence_transformers import SentenceTransformer
from annoy import AnnoyIndex
import sys
app, rt = fast_app(live=True)
# Load the Sentence Transformer model
model = SentenceTransformer('BAAI/bge-large-en-v1.5')
# Load the Annoy index
index = AnnoyIndex(1024, 'euclidean')
index.load('data/office-lines-vec-db.ann')
dialogues_df = pd.read_csv('data/dilogues.csv')
from loguru import logger
log_format = (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level}</level> | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | "
"<level>{message}</level>"
)
# Adding a console handler with the custom format
logger.add(
sys.stdout,
format=log_format,
level="INFO",
colorize=True
)
# Adding a file handler that also logs to a file
logger.add(
"file.log",
format=log_format,
level="DEBUG",
rotation="10 MB", # Automatically rotate the log file after it reaches 10MB
compression="zip" # Compress old logs to save space
)
@rt("/")
def get():
return Titled("Reverse Text App",
Form(method="post")(
Label("Enter text:", Input(name="query", type="text")),
Button("Reverse", type="submit")
),
Div(id="result")
)
@rt("/")
def post(query: str):
# Convert the query to an embedding
logger.info(f"Searching for: {query}")
query_embedding = model.encode([query])[0] # Get the first (and only) embedding
logger.info(f"Query embedding shape: {query_embedding.shape}")
# Perform similarity search
nearest_ids = index.get_nns_by_vector(query_embedding, 1)
nearest_id = nearest_ids[0]
logger.info(f"Nearest ID: {nearest_id}")
# Fetch the corresponding dialogue text
dialogue_text = dialogues_df[dialogues_df['id'] == nearest_id]['line_text'].values[0]
logger.info(f"Dialogue: {dialogue_text}")
return Div(
P(f'Search results for "{query}":'),
Ul(Li(dialogue_text))
)
serve()