Update app.py
Browse files
app.py
CHANGED
|
@@ -37,13 +37,10 @@ for course_card in soup.find_all('header', class_='course-card__img-container'):
|
|
| 37 |
df = pd.DataFrame(courses)
|
| 38 |
|
| 39 |
# Step 3: Initialize the Groq client and set the API key
|
| 40 |
-
client = Groq(api_key=creds.api_key)
|
| 41 |
|
| 42 |
def search_courses(query):
|
| 43 |
try:
|
| 44 |
-
print(f"Searching for: {query}")
|
| 45 |
-
print(f"Number of courses in database: {len(df)}")
|
| 46 |
-
|
| 47 |
# Prepare the prompt for Groq
|
| 48 |
prompt = f"""Given the following query: "{query}"
|
| 49 |
Please analyze the query and rank the following courses based on their relevance to the query.
|
|
@@ -57,29 +54,22 @@ def search_courses(query):
|
|
| 57 |
{df['title'].to_string(index=False)}
|
| 58 |
"""
|
| 59 |
|
| 60 |
-
print("Sending request to Groq...")
|
| 61 |
# Get response from Groq
|
| 62 |
response = client.chat.completions.create(
|
| 63 |
-
model="mixtral-8x7b-32768",
|
| 64 |
messages=[{"role": "system", "content": "You are an AI assistant specialized in course recommendations."},
|
| 65 |
{"role": "user", "content": prompt}],
|
| 66 |
temperature=0.2,
|
| 67 |
max_tokens=1000
|
| 68 |
)
|
| 69 |
-
print("Received response from Groq")
|
| 70 |
|
| 71 |
# Parse Groq's response
|
| 72 |
results = []
|
| 73 |
-
print("Groq response content:")
|
| 74 |
-
print(response.choices[0].message.content)
|
| 75 |
-
|
| 76 |
for line in response.choices[0].message.content.split('\n'):
|
| 77 |
if line.startswith('Title:'):
|
| 78 |
title = line.split('Title:')[1].strip()
|
| 79 |
-
print(f"Found title: {title}")
|
| 80 |
elif line.startswith('Relevance:'):
|
| 81 |
relevance = float(line.split('Relevance:')[1].strip())
|
| 82 |
-
print(f"Relevance for {title}: {relevance}")
|
| 83 |
if relevance >= 0.5:
|
| 84 |
matching_courses = df[df['title'] == title]
|
| 85 |
if not matching_courses.empty:
|
|
@@ -90,15 +80,10 @@ def search_courses(query):
|
|
| 90 |
'course_link': course['course_link'],
|
| 91 |
'score': relevance
|
| 92 |
})
|
| 93 |
-
print(f"Added course: {title}")
|
| 94 |
-
else:
|
| 95 |
-
print(f"Warning: Course not found in database: {title}")
|
| 96 |
|
| 97 |
-
|
| 98 |
-
return sorted(results, key=lambda x: x['score'], reverse=True)[:10] # Return top 10 results
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
-
print(f"An error occurred in search_courses: {str(e)}")
|
| 102 |
return []
|
| 103 |
|
| 104 |
def gradio_search(query):
|
|
@@ -126,19 +111,18 @@ def gradio_search(query):
|
|
| 126 |
else:
|
| 127 |
return '<p class="no-results">No results found. Please try a different query.</p>'
|
| 128 |
|
| 129 |
-
#
|
| 130 |
custom_css = """
|
| 131 |
body {
|
| 132 |
font-family: Arial, sans-serif;
|
| 133 |
-
background-color: #
|
| 134 |
-
|
| 135 |
-
h1, h2, p, .container .examples {
|
| 136 |
-
color: #333; /* Darker color for better visibility */
|
| 137 |
}
|
| 138 |
.container {
|
| 139 |
max-width: 800px;
|
| 140 |
margin: 0 auto;
|
| 141 |
padding: 20px;
|
|
|
|
| 142 |
}
|
| 143 |
.results-container {
|
| 144 |
display: flex;
|
|
@@ -146,9 +130,9 @@ h1, h2, p, .container .examples {
|
|
| 146 |
justify-content: space-between;
|
| 147 |
}
|
| 148 |
.course-card {
|
| 149 |
-
background-color:
|
| 150 |
border-radius: 8px;
|
| 151 |
-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.
|
| 152 |
margin-bottom: 20px;
|
| 153 |
overflow: hidden;
|
| 154 |
width: 48%;
|
|
@@ -168,10 +152,10 @@ h1, h2, p, .container .examples {
|
|
| 168 |
.course-info h3 {
|
| 169 |
margin-top: 0;
|
| 170 |
font-size: 18px;
|
| 171 |
-
color: #
|
| 172 |
}
|
| 173 |
.course-info p {
|
| 174 |
-
color: #
|
| 175 |
font-size: 14px;
|
| 176 |
margin-bottom: 10px;
|
| 177 |
}
|
|
@@ -190,7 +174,7 @@ h1, h2, p, .container .examples {
|
|
| 190 |
}
|
| 191 |
.no-results {
|
| 192 |
text-align: center;
|
| 193 |
-
color: #
|
| 194 |
font-style: italic;
|
| 195 |
}
|
| 196 |
"""
|
|
|
|
| 37 |
df = pd.DataFrame(courses)
|
| 38 |
|
| 39 |
# Step 3: Initialize the Groq client and set the API key
|
| 40 |
+
client = Groq(api_key=creds.api_key)
|
| 41 |
|
| 42 |
def search_courses(query):
|
| 43 |
try:
|
|
|
|
|
|
|
|
|
|
| 44 |
# Prepare the prompt for Groq
|
| 45 |
prompt = f"""Given the following query: "{query}"
|
| 46 |
Please analyze the query and rank the following courses based on their relevance to the query.
|
|
|
|
| 54 |
{df['title'].to_string(index=False)}
|
| 55 |
"""
|
| 56 |
|
|
|
|
| 57 |
# Get response from Groq
|
| 58 |
response = client.chat.completions.create(
|
| 59 |
+
model="mixtral-8x7b-32768",
|
| 60 |
messages=[{"role": "system", "content": "You are an AI assistant specialized in course recommendations."},
|
| 61 |
{"role": "user", "content": prompt}],
|
| 62 |
temperature=0.2,
|
| 63 |
max_tokens=1000
|
| 64 |
)
|
|
|
|
| 65 |
|
| 66 |
# Parse Groq's response
|
| 67 |
results = []
|
|
|
|
|
|
|
|
|
|
| 68 |
for line in response.choices[0].message.content.split('\n'):
|
| 69 |
if line.startswith('Title:'):
|
| 70 |
title = line.split('Title:')[1].strip()
|
|
|
|
| 71 |
elif line.startswith('Relevance:'):
|
| 72 |
relevance = float(line.split('Relevance:')[1].strip())
|
|
|
|
| 73 |
if relevance >= 0.5:
|
| 74 |
matching_courses = df[df['title'] == title]
|
| 75 |
if not matching_courses.empty:
|
|
|
|
| 80 |
'course_link': course['course_link'],
|
| 81 |
'score': relevance
|
| 82 |
})
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
return sorted(results, key=lambda x: x['score'], reverse=True)[:10]
|
|
|
|
| 85 |
|
| 86 |
except Exception as e:
|
|
|
|
| 87 |
return []
|
| 88 |
|
| 89 |
def gradio_search(query):
|
|
|
|
| 111 |
else:
|
| 112 |
return '<p class="no-results">No results found. Please try a different query.</p>'
|
| 113 |
|
| 114 |
+
# Dark-themed CSS
|
| 115 |
custom_css = """
|
| 116 |
body {
|
| 117 |
font-family: Arial, sans-serif;
|
| 118 |
+
background-color: #121212; /* Dark background */
|
| 119 |
+
color: #E0E0E0; /* Light text color for dark background */
|
|
|
|
|
|
|
| 120 |
}
|
| 121 |
.container {
|
| 122 |
max-width: 800px;
|
| 123 |
margin: 0 auto;
|
| 124 |
padding: 20px;
|
| 125 |
+
color: #E0E0E0;
|
| 126 |
}
|
| 127 |
.results-container {
|
| 128 |
display: flex;
|
|
|
|
| 130 |
justify-content: space-between;
|
| 131 |
}
|
| 132 |
.course-card {
|
| 133 |
+
background-color: #1E1E1E; /* Darker card background */
|
| 134 |
border-radius: 8px;
|
| 135 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
|
| 136 |
margin-bottom: 20px;
|
| 137 |
overflow: hidden;
|
| 138 |
width: 48%;
|
|
|
|
| 152 |
.course-info h3 {
|
| 153 |
margin-top: 0;
|
| 154 |
font-size: 18px;
|
| 155 |
+
color: #E0E0E0; /* Light text color */
|
| 156 |
}
|
| 157 |
.course-info p {
|
| 158 |
+
color: #B0B0B0; /* Slightly darker text color for contrast */
|
| 159 |
font-size: 14px;
|
| 160 |
margin-bottom: 10px;
|
| 161 |
}
|
|
|
|
| 174 |
}
|
| 175 |
.no-results {
|
| 176 |
text-align: center;
|
| 177 |
+
color: #B0B0B0;
|
| 178 |
font-style: italic;
|
| 179 |
}
|
| 180 |
"""
|