Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import json
|
| 4 |
+
from typing import List, Dict
|
| 5 |
+
|
| 6 |
+
# Load courses from JSON file
|
| 7 |
+
def load_courses():
|
| 8 |
+
try:
|
| 9 |
+
with open('all_courses.json', 'r', encoding='utf-8') as f:
|
| 10 |
+
return json.load(f)
|
| 11 |
+
except Exception as e:
|
| 12 |
+
st.error(f"Error loading courses: {str(e)}")
|
| 13 |
+
return []
|
| 14 |
+
|
| 15 |
+
def setup_gemini():
|
| 16 |
+
"""Configure and return Gemini model using API key from Streamlit secrets"""
|
| 17 |
+
try:
|
| 18 |
+
# Access API key from Streamlit secrets
|
| 19 |
+
API_KEY = st.secrets["GEMINI_API_KEY"]
|
| 20 |
+
genai.configure(api_key=API_KEY)
|
| 21 |
+
return genai.GenerativeModel('gemini-pro')
|
| 22 |
+
except Exception as e:
|
| 23 |
+
st.error(f"Error setting up Gemini: {str(e)}")
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
def search_courses(query: str, model, courses: List[Dict]) -> str:
|
| 27 |
+
"""Search courses using Gemini model"""
|
| 28 |
+
if not model:
|
| 29 |
+
return "Error: Gemini model not initialized properly"
|
| 30 |
+
|
| 31 |
+
prompt = f"""
|
| 32 |
+
Given these Analytics Vidhya courses: {json.dumps(courses, indent=2)}
|
| 33 |
+
Find the most relevant courses for this search query: "{query}"
|
| 34 |
+
Respond in this format:
|
| 35 |
+
1. Brief explanation of why these courses match (2-3 sentences)
|
| 36 |
+
2. List of matching courses in order of relevance, with short explanation for each
|
| 37 |
+
Only include relevant courses. Focus on the title, description, and topics.
|
| 38 |
+
Keep the response concise and practical.
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
response = model.generate_content(prompt)
|
| 43 |
+
return response.text
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error performing search: {str(e)}"
|
| 46 |
+
|
| 47 |
+
def main():
|
| 48 |
+
st.set_page_config(
|
| 49 |
+
page_title="Analytics Vidhya Course Search",
|
| 50 |
+
page_icon="π",
|
| 51 |
+
layout="centered"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
st.title("π Analytics Vidhya Course Search Assistant")
|
| 55 |
+
|
| 56 |
+
# Load courses data
|
| 57 |
+
courses = load_courses()
|
| 58 |
+
if not courses:
|
| 59 |
+
st.error("No courses data available. Please ensure all_courses.json exists.")
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
# Display total number of courses
|
| 63 |
+
st.info(f"π Total available courses: {len(courses)}")
|
| 64 |
+
|
| 65 |
+
# Initialize Gemini model once at startup
|
| 66 |
+
model = setup_gemini()
|
| 67 |
+
if not model:
|
| 68 |
+
st.error("Failed to initialize Gemini model. Please check your API key.")
|
| 69 |
+
return
|
| 70 |
+
|
| 71 |
+
# Main search interface
|
| 72 |
+
query = st.text_input(
|
| 73 |
+
"What would you like to learn?",
|
| 74 |
+
placeholder="e.g., machine learning for beginners"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
search_button = st.button("π Search", type="primary", use_container_width=True)
|
| 78 |
+
|
| 79 |
+
# Sample queries suggestions
|
| 80 |
+
st.markdown("""
|
| 81 |
+
### Sample Queries:
|
| 82 |
+
- "machine learning courses"
|
| 83 |
+
- "python for data science"
|
| 84 |
+
- "deep learning and neural networks"
|
| 85 |
+
- "data visualization techniques"
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
if search_button:
|
| 89 |
+
if not query:
|
| 90 |
+
st.warning("β οΈ Please enter a search query")
|
| 91 |
+
return
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
with st.spinner("π Searching for relevant courses..."):
|
| 95 |
+
results = search_courses(query, model, courses)
|
| 96 |
+
st.markdown("### π― Search Results")
|
| 97 |
+
st.markdown(results)
|
| 98 |
+
except Exception as e:
|
| 99 |
+
st.error(f"β An error occurred: {str(e)}")
|
| 100 |
+
st.info("Please try again with a different query")
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
main()
|