Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,20 +4,33 @@ from model import RAGModel, load_configs
|
|
4 |
|
5 |
|
6 |
def run_on_start():
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
run_on_start()
|
14 |
|
15 |
|
16 |
def search(query):
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
st.title("Search Here Instead of Google")
|
@@ -29,32 +42,37 @@ if "doc" not in st.session_state:
|
|
29 |
st.session_state.doc = None
|
30 |
|
31 |
if "refresh" not in st.session_state:
|
32 |
-
st.session_state.refresh = True
|
33 |
|
34 |
for message in st.session_state.messages:
|
35 |
with st.chat_message(message["role"]):
|
36 |
st.markdown(message["content"])
|
37 |
|
38 |
|
39 |
-
if prompt := st.chat_input("Search Here
|
40 |
st.chat_message("user").markdown(prompt)
|
41 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
42 |
|
43 |
-
configs = st.session_state.configs
|
44 |
if st.session_state.refresh:
|
45 |
-
st.session_state.refresh = False
|
46 |
search(prompt)
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
st.
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
|
6 |
def run_on_start():
|
7 |
+
try:
|
8 |
+
if "configs" not in st.session_state:
|
9 |
+
st.session_state.configs = configs = load_configs(config_file="rag.configs.yml")
|
10 |
+
st.write("Configs Loaded:", configs)
|
11 |
+
|
12 |
+
if "model" not in st.session_state:
|
13 |
+
st.session_state.model = RAGModel(st.session_state.configs)
|
14 |
+
st.write("RAGModel Initialized Successfully")
|
15 |
+
except Exception as e:
|
16 |
+
st.error(f"Initialization Error: {e}")
|
17 |
+
raise
|
18 |
+
|
19 |
run_on_start()
|
20 |
|
21 |
|
22 |
def search(query):
|
23 |
+
try:
|
24 |
+
g = GoogleSearch(query)
|
25 |
+
data = g.all_page_data
|
26 |
+
st.write("Google Search Data:", data) # Debug GoogleSearch output
|
27 |
+
|
28 |
+
d = Document(data, min_char_len=st.session_state.configs["document"]["min_char_length"])
|
29 |
+
st.session_state.doc = d.doc()
|
30 |
+
st.write("Document Created Successfully")
|
31 |
+
except Exception as e:
|
32 |
+
st.error(f"Search Error: {e}")
|
33 |
+
raise
|
34 |
|
35 |
|
36 |
st.title("Search Here Instead of Google")
|
|
|
42 |
st.session_state.doc = None
|
43 |
|
44 |
if "refresh" not in st.session_state:
|
45 |
+
st.session_state.refresh = True
|
46 |
|
47 |
for message in st.session_state.messages:
|
48 |
with st.chat_message(message["role"]):
|
49 |
st.markdown(message["content"])
|
50 |
|
51 |
|
52 |
+
if prompt := st.chat_input("Search Here instead of Google"):
|
53 |
st.chat_message("user").markdown(prompt)
|
54 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
55 |
|
|
|
56 |
if st.session_state.refresh:
|
57 |
+
st.session_state.refresh = False
|
58 |
search(prompt)
|
59 |
|
60 |
+
try:
|
61 |
+
s = SemanticSearch(
|
62 |
+
st.session_state.doc,
|
63 |
+
st.session_state.configs["model"]["embeding_model"],
|
64 |
+
st.session_state.configs["model"]["device"],
|
65 |
+
)
|
66 |
+
topk, u = s.semantic_search(query=prompt, k=32)
|
67 |
+
st.write("Semantic Search Results:", topk)
|
68 |
+
|
69 |
+
output = st.session_state.model.answer_query(query=prompt, topk_items=topk)
|
70 |
+
response = output
|
71 |
+
with st.chat_message("assistant"):
|
72 |
+
st.markdown(response)
|
73 |
+
|
74 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
75 |
+
|
76 |
+
except Exception as e:
|
77 |
+
st.error(f"Error in Semantic Search or Model Response: {e}")
|
78 |
+
raise
|