Spaces:
Running
Running
kartiksrma
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -23,17 +23,34 @@ try:
|
|
23 |
except Exception as e:
|
24 |
print(f"Error accessing the database or collection: {e}")
|
25 |
|
26 |
-
def generate_embedding(text: str) -> list[float]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
raise ValueError(f"Request failed with status code {response.status_code}: {response.text}")
|
35 |
|
36 |
-
return response.json()
|
37 |
|
38 |
# for doc in collection.find({'title':{"$exists": True}}).limit(75):
|
39 |
# doc['course_embedding_hf'] = generate_embedding(doc['merged_summary'])
|
@@ -97,7 +114,7 @@ if st.button("Search"):
|
|
97 |
with st.spinner("Fetching results..."):
|
98 |
results = getSearchResults(query, relevance_threshold)
|
99 |
try:
|
100 |
-
if results:
|
101 |
for course in results:
|
102 |
st.markdown(
|
103 |
f"""
|
@@ -108,14 +125,11 @@ if st.button("Search"):
|
|
108 |
- **Difficulty:** {course['difficulty']}
|
109 |
"""
|
110 |
)
|
111 |
-
st.markdown(
|
112 |
-
f"[![Go to Course](https://img.shields.io/badge/Go%20to%20Course-blue)]({course['course_url']})",
|
113 |
-
unsafe_allow_html=True,
|
114 |
-
)
|
115 |
|
116 |
st.markdown("---")
|
117 |
else:
|
118 |
-
st.
|
119 |
except Exception as e:
|
120 |
st.markdown(f"rate limit for searching has been completed try after few minutes\n",e)
|
121 |
else:
|
|
|
23 |
except Exception as e:
|
24 |
print(f"Error accessing the database or collection: {e}")
|
25 |
|
26 |
+
# def generate_embedding(text: str) -> list[float]:
|
27 |
+
|
28 |
+
# response = requests.post(
|
29 |
+
# embedding_url,
|
30 |
+
# headers={"Authorization": f"Bearer {hf_token}"},
|
31 |
+
# json={"inputs": text})
|
32 |
+
|
33 |
+
# if response.status_code != 200:
|
34 |
+
# raise ValueError(f"Request failed with status code {response.status_code}: {response.text}")
|
35 |
+
|
36 |
+
# return response.json()
|
37 |
|
38 |
+
def generate_embedding(text: str) -> list[float]:
|
39 |
+
for attempt in range(5): # Retry up to 5 times
|
40 |
+
response = requests.post(
|
41 |
+
embedding_url,
|
42 |
+
headers={"Authorization": f"Bearer {hf_token}"},
|
43 |
+
json={"inputs": text}
|
44 |
+
)
|
45 |
+
if response.status_code == 200:
|
46 |
+
return response.json()
|
47 |
+
elif response.status_code == 503:
|
48 |
+
time.sleep(5) # Wait before retrying
|
49 |
+
else:
|
50 |
+
raise ValueError(f"Request failed with status code {response.status_code}: {response.text}")
|
51 |
|
52 |
+
raise ValueError("Failed to generate embedding after multiple retries.")
|
|
|
53 |
|
|
|
54 |
|
55 |
# for doc in collection.find({'title':{"$exists": True}}).limit(75):
|
56 |
# doc['course_embedding_hf'] = generate_embedding(doc['merged_summary'])
|
|
|
114 |
with st.spinner("Fetching results..."):
|
115 |
results = getSearchResults(query, relevance_threshold)
|
116 |
try:
|
117 |
+
if len(list(results)):
|
118 |
for course in results:
|
119 |
st.markdown(
|
120 |
f"""
|
|
|
125 |
- **Difficulty:** {course['difficulty']}
|
126 |
"""
|
127 |
)
|
128 |
+
st.markdown(f"[![Go to Course](https://img.shields.io/badge/Go%20to%20Course-blue)]({course['course_url']})",unsafe_allow_html=True,)
|
|
|
|
|
|
|
129 |
|
130 |
st.markdown("---")
|
131 |
else:
|
132 |
+
st.warning("No matches found! Try adjusting the relevance slider or using different keywords.")
|
133 |
except Exception as e:
|
134 |
st.markdown(f"rate limit for searching has been completed try after few minutes\n",e)
|
135 |
else:
|