Spaces:
Build error
Build error
App added
Browse files- app.py +140 -0
- original.png +0 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from groq import Groq
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
# Step 1: Scrape the free courses from Analytics Vidhya
|
| 11 |
+
url = "https://courses.analyticsvidhya.com/pages/all-free-courses"
|
| 12 |
+
response = requests.get(url)
|
| 13 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 14 |
+
|
| 15 |
+
courses = []
|
| 16 |
+
|
| 17 |
+
# Extracting course title, image, and course link
|
| 18 |
+
for course_card in soup.find_all('header', class_='course-card__img-container'):
|
| 19 |
+
img_tag = course_card.find('img', class_='course-card__img')
|
| 20 |
+
|
| 21 |
+
if img_tag:
|
| 22 |
+
title = img_tag.get('alt')
|
| 23 |
+
image_url = img_tag.get('src')
|
| 24 |
+
|
| 25 |
+
link_tag = course_card.find_previous('a')
|
| 26 |
+
if link_tag:
|
| 27 |
+
course_link = link_tag.get('href')
|
| 28 |
+
if not course_link.startswith('http'):
|
| 29 |
+
course_link = 'https://courses.analyticsvidhya.com' + course_link
|
| 30 |
+
|
| 31 |
+
courses.append({
|
| 32 |
+
'title': title,
|
| 33 |
+
'image_url': image_url,
|
| 34 |
+
'course_link': course_link
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
# Step 2: Create DataFrame
|
| 38 |
+
df = pd.DataFrame(courses)
|
| 39 |
+
|
| 40 |
+
load_dotenv()
|
| 41 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 42 |
+
|
| 43 |
+
def search_courses(query):
|
| 44 |
+
try:
|
| 45 |
+
# Prepare the prompt for Groq
|
| 46 |
+
prompt = f"""Given the following query: "{query}"
|
| 47 |
+
Please analyze the query and rank the following courses based on their relevance to the query.
|
| 48 |
+
Prioritize courses from Analytics Vidhya. Provide a relevance score from 0 to 1 for each course.
|
| 49 |
+
Only return courses with a relevance score of 0.5 or higher.
|
| 50 |
+
Return the results in the following format:
|
| 51 |
+
Title: [Course Title]
|
| 52 |
+
Relevance: [Score]
|
| 53 |
+
|
| 54 |
+
Courses:
|
| 55 |
+
{df['title'].to_string(index=False)}
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
# Get response from Groq
|
| 59 |
+
response = client.chat.completions.create(
|
| 60 |
+
model="llama-3.2-1b-preview",
|
| 61 |
+
messages=[
|
| 62 |
+
{"role": "system", "content": "You are an AI assistant specialized in course recommendations."},
|
| 63 |
+
{"role": "user", "content": prompt}
|
| 64 |
+
],
|
| 65 |
+
temperature=0.2,
|
| 66 |
+
max_tokens=1000
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Parse Groq's response
|
| 70 |
+
results = []
|
| 71 |
+
matches = re.findall(r'\*\*(.+?)\*\*\s*\(Relevance Score: (0\.\d+)\)', response.choices[0].message.content)
|
| 72 |
+
|
| 73 |
+
for title, score in matches:
|
| 74 |
+
title = title.strip()
|
| 75 |
+
score = float(score)
|
| 76 |
+
if score >= 0.5:
|
| 77 |
+
matching_courses = df[df['title'].str.contains(title[:30], case=False, na=False)]
|
| 78 |
+
if not matching_courses.empty:
|
| 79 |
+
course = matching_courses.iloc[0]
|
| 80 |
+
results.append({
|
| 81 |
+
'title': course['title'], # Use the full title from the database
|
| 82 |
+
'image_url': course['image_url'],
|
| 83 |
+
'course_link': course['course_link'],
|
| 84 |
+
'score': score
|
| 85 |
+
})
|
| 86 |
+
return sorted(results, key=lambda x: x['score'], reverse=True)[:10] # Return top 10 results
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
st.error(f"An error occurred in search_courses: {str(e)}")
|
| 90 |
+
return []
|
| 91 |
+
|
| 92 |
+
def display_search_results(result_list):
|
| 93 |
+
if result_list:
|
| 94 |
+
for item in result_list:
|
| 95 |
+
course_title = item['title']
|
| 96 |
+
course_image = item['image_url']
|
| 97 |
+
course_link = item['course_link']
|
| 98 |
+
relevance_score = round(item['score'] * 100, 2)
|
| 99 |
+
|
| 100 |
+
st.image(course_image, use_column_width=True)
|
| 101 |
+
st.write(f"### {course_title}")
|
| 102 |
+
st.write(f"Relevance: {relevance_score}%")
|
| 103 |
+
st.markdown(f"[View Course]({course_link})", unsafe_allow_html=True)
|
| 104 |
+
else:
|
| 105 |
+
st.write("No results found. Please try a different query.")
|
| 106 |
+
|
| 107 |
+
#Streamlit UI
|
| 108 |
+
|
| 109 |
+
st.title("Analytics Vidhya Free Courses🔍")
|
| 110 |
+
st.image("original.png")
|
| 111 |
+
st.markdown("#### 🔍🌐 Get the most appropriate course as per your learning requirement.")
|
| 112 |
+
st.markdown("<hr style='border:1px solid #eee;'>", unsafe_allow_html=True)
|
| 113 |
+
|
| 114 |
+
query = st.text_input(
|
| 115 |
+
"Enter course related keywords...",
|
| 116 |
+
placeholder="e.g., machine learning, data science, python",
|
| 117 |
+
help="Type in a keyword to find related free courses"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
search_button = st.button("Search")
|
| 121 |
+
|
| 122 |
+
# Search results section
|
| 123 |
+
if search_button and query:
|
| 124 |
+
st.write("Collecting courses......")
|
| 125 |
+
result_list = search_courses(query)
|
| 126 |
+
|
| 127 |
+
# Display search results if available
|
| 128 |
+
if result_list:
|
| 129 |
+
st.markdown(f"### Top results for: {query}")
|
| 130 |
+
display_search_results(result_list)
|
| 131 |
+
else:
|
| 132 |
+
st.write("No results found. Please try a different keyword.")
|
| 133 |
+
else:
|
| 134 |
+
st.write("Please enter a search query to find relevant courses.")
|
| 135 |
+
|
| 136 |
+
# Footer with subtle text
|
| 137 |
+
st.markdown(
|
| 138 |
+
"<p style='text-align:center; color:grey;'>Made by @metechmohit </p>",
|
| 139 |
+
unsafe_allow_html=True
|
| 140 |
+
)
|
original.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.39.0
|
| 2 |
+
requests==2.32.3
|
| 3 |
+
pandas==2.2.3
|
| 4 |
+
beautifulsoup4==4.12.3
|
| 5 |
+
groq==0.11.0
|
| 6 |
+
python-dotenv
|
| 7 |
+
#gradio==4.44.1
|