Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ from nltk.corpus import stopwords
|
|
| 9 |
from nltk.tokenize import word_tokenize
|
| 10 |
from memory_store import log_chat, build_or_update_index, search_similar
|
| 11 |
|
| 12 |
-
# Download NLTK resources if not present
|
| 13 |
nltk.download("punkt")
|
| 14 |
nltk.download("stopwords")
|
| 15 |
|
|
@@ -17,67 +16,70 @@ GOOGLE_API_KEY = "AIzaSyCgOaCQEVvOuNtZhOkrmcnqzuiiPDA6b1Q" # π Replace this
|
|
| 17 |
SEARCH_ENGINE_ID = "17357d675fc704764" # π Replace this
|
| 18 |
COURSE_DB_PATH = "mock_courses.json"
|
| 19 |
|
| 20 |
-
# === Load LinkedIn Course Data ===
|
| 21 |
if os.path.exists(COURSE_DB_PATH):
|
| 22 |
with open(COURSE_DB_PATH, "r") as f:
|
| 23 |
linkedin_courses = json.load(f)
|
| 24 |
else:
|
| 25 |
linkedin_courses = []
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
def extract_keywords(text):
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
query_words = set(extract_keywords(query))
|
| 36 |
-
best_course = None
|
| 37 |
-
best_score = 0
|
| 38 |
-
|
| 39 |
-
for course in courses:
|
| 40 |
-
title_words = set(extract_keywords(course["title"]))
|
| 41 |
-
score = len(query_words & title_words)
|
| 42 |
-
if score > best_score and score >= min_match:
|
| 43 |
-
best_course = course
|
| 44 |
-
best_score = score
|
| 45 |
-
return best_course
|
| 46 |
-
|
| 47 |
-
# === Google Course Search ===
|
| 48 |
-
def google_search(query, num_results=3):
|
| 49 |
-
api_url = f"https://www.googleapis.com/customsearch/v1?key={GOOGLE_API_KEY}&cx={SEARCH_ENGINE_ID}&q={query}"
|
| 50 |
try:
|
| 51 |
response = requests.get(api_url)
|
| 52 |
data = response.json()
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Google Search Error: {e}")
|
| 59 |
return None
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def course_agent(profession, query):
|
| 63 |
if not query:
|
| 64 |
return "β Please enter a course or topic."
|
| 65 |
|
| 66 |
keywords = extract_keywords(query)
|
| 67 |
if not keywords:
|
| 68 |
-
return "β Could not extract keywords
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
if course:
|
| 77 |
result = f"β
Found on LinkedIn:\n{course['title']} - {course['url']}"
|
| 78 |
else:
|
| 79 |
-
|
| 80 |
-
g_result = google_search(search_term)
|
| 81 |
if g_result:
|
| 82 |
result = f"π Found via Google:\n{g_result['title']} - {g_result['url']}"
|
| 83 |
else:
|
|
@@ -87,7 +89,6 @@ def course_agent(profession, query):
|
|
| 87 |
build_or_update_index(profession, query, result)
|
| 88 |
return result
|
| 89 |
|
| 90 |
-
# === Simple Echo Chat Handler ===
|
| 91 |
def chat_with_user(message, history):
|
| 92 |
answer = f"Echo: {message}"
|
| 93 |
log_chat(message, answer)
|
|
@@ -96,7 +97,6 @@ def chat_with_user(message, history):
|
|
| 96 |
{"role": "assistant", "content": answer}
|
| 97 |
], ""
|
| 98 |
|
| 99 |
-
# === UI Definition ===
|
| 100 |
with gr.Blocks(title="π§ AI Course Finder with Self-Learning") as demo:
|
| 101 |
with gr.Tab("Course Finder"):
|
| 102 |
gr.Markdown("## π Smart Agent with FAISS Logging")
|
|
@@ -106,12 +106,11 @@ with gr.Blocks(title="π§ AI Course Finder with Self-Learning") as demo:
|
|
| 106 |
query = gr.Textbox(label="π Topic")
|
| 107 |
search_btn = gr.Button("π― Search")
|
| 108 |
result_box = gr.Textbox(label="π Result", lines=4)
|
| 109 |
-
|
| 110 |
search_btn.click(fn=course_agent, inputs=[profession, query], outputs=result_box)
|
| 111 |
|
| 112 |
with gr.Tab("Chat"):
|
| 113 |
chatbot = gr.Chatbot(label="Chat", type="messages")
|
| 114 |
txt = gr.Textbox(show_label=False, placeholder="Type your message...")
|
| 115 |
-
txt.submit(
|
| 116 |
|
| 117 |
demo.launch()
|
|
|
|
| 9 |
from nltk.tokenize import word_tokenize
|
| 10 |
from memory_store import log_chat, build_or_update_index, search_similar
|
| 11 |
|
|
|
|
| 12 |
nltk.download("punkt")
|
| 13 |
nltk.download("stopwords")
|
| 14 |
|
|
|
|
| 16 |
SEARCH_ENGINE_ID = "17357d675fc704764" # π Replace this
|
| 17 |
COURSE_DB_PATH = "mock_courses.json"
|
| 18 |
|
|
|
|
| 19 |
if os.path.exists(COURSE_DB_PATH):
|
| 20 |
with open(COURSE_DB_PATH, "r") as f:
|
| 21 |
linkedin_courses = json.load(f)
|
| 22 |
else:
|
| 23 |
linkedin_courses = []
|
| 24 |
|
| 25 |
+
stop_words = set(stopwords.words("english"))
|
| 26 |
+
CRITICAL_WORDS = {"self", "diabetes", "finance", "stress", "communication", "team", "conflict"}
|
| 27 |
+
|
| 28 |
def extract_keywords(text):
|
| 29 |
+
words = word_tokenize(text.lower())
|
| 30 |
+
return [w for w in words if w.isalnum() and w not in stop_words]
|
| 31 |
+
|
| 32 |
+
def google_search(query, num_results=1):
|
| 33 |
+
search_term = f"{query} site:linkedin.com/learning OR site:udemy.com OR site:coursera.org"
|
| 34 |
+
api_url = f"https://www.googleapis.com/customsearch/v1?key={GOOGLE_API_KEY}&cx={SEARCH_ENGINE_ID}&q={search_term}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
response = requests.get(api_url)
|
| 37 |
data = response.json()
|
| 38 |
+
items = data.get("items", [])
|
| 39 |
+
if items:
|
| 40 |
+
top = items[0]
|
| 41 |
+
return {"title": top.get("title", "No Title"), "url": top.get("link", "No URL")}
|
| 42 |
except Exception as e:
|
| 43 |
print(f"Google Search Error: {e}")
|
| 44 |
return None
|
| 45 |
|
| 46 |
+
def filter_by_keywords(query, course_list, min_match=2):
|
| 47 |
+
query_keywords = set(extract_keywords(query))
|
| 48 |
+
for course in course_list:
|
| 49 |
+
title_keywords = set(extract_keywords(course["title"]))
|
| 50 |
+
if len(query_keywords.intersection(title_keywords)) >= min_match:
|
| 51 |
+
return course
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
def course_agent(profession, query):
|
| 55 |
if not query:
|
| 56 |
return "β Please enter a course or topic."
|
| 57 |
|
| 58 |
keywords = extract_keywords(query)
|
| 59 |
if not keywords:
|
| 60 |
+
return "β Could not extract keywords."
|
| 61 |
|
| 62 |
+
titles = [course["title"] for course in linkedin_courses]
|
| 63 |
+
match = difflib.get_close_matches(query, titles, n=1, cutoff=0.6)
|
| 64 |
+
course = None
|
| 65 |
|
| 66 |
+
if match:
|
| 67 |
+
course = next((c for c in linkedin_courses if c["title"] == match[0]), None)
|
| 68 |
+
|
| 69 |
+
# Improved keyword matching
|
| 70 |
+
if not course:
|
| 71 |
+
course = filter_by_keywords(query, linkedin_courses, min_match=2)
|
| 72 |
+
|
| 73 |
+
# Reject if critical keywords in query not present in title
|
| 74 |
+
if course:
|
| 75 |
+
title_keywords = set(extract_keywords(course["title"]))
|
| 76 |
+
if any(k in keywords and k not in title_keywords for k in CRITICAL_WORDS):
|
| 77 |
+
course = None
|
| 78 |
|
| 79 |
if course:
|
| 80 |
result = f"β
Found on LinkedIn:\n{course['title']} - {course['url']}"
|
| 81 |
else:
|
| 82 |
+
g_result = google_search(query)
|
|
|
|
| 83 |
if g_result:
|
| 84 |
result = f"π Found via Google:\n{g_result['title']} - {g_result['url']}"
|
| 85 |
else:
|
|
|
|
| 89 |
build_or_update_index(profession, query, result)
|
| 90 |
return result
|
| 91 |
|
|
|
|
| 92 |
def chat_with_user(message, history):
|
| 93 |
answer = f"Echo: {message}"
|
| 94 |
log_chat(message, answer)
|
|
|
|
| 97 |
{"role": "assistant", "content": answer}
|
| 98 |
], ""
|
| 99 |
|
|
|
|
| 100 |
with gr.Blocks(title="π§ AI Course Finder with Self-Learning") as demo:
|
| 101 |
with gr.Tab("Course Finder"):
|
| 102 |
gr.Markdown("## π Smart Agent with FAISS Logging")
|
|
|
|
| 106 |
query = gr.Textbox(label="π Topic")
|
| 107 |
search_btn = gr.Button("π― Search")
|
| 108 |
result_box = gr.Textbox(label="π Result", lines=4)
|
|
|
|
| 109 |
search_btn.click(fn=course_agent, inputs=[profession, query], outputs=result_box)
|
| 110 |
|
| 111 |
with gr.Tab("Chat"):
|
| 112 |
chatbot = gr.Chatbot(label="Chat", type="messages")
|
| 113 |
txt = gr.Textbox(show_label=False, placeholder="Type your message...")
|
| 114 |
+
txt.submit(chat_with_user, [txt, chatbot], [chatbot, txt])
|
| 115 |
|
| 116 |
demo.launch()
|