Spaces:
Sleeping
Sleeping
Update learning_path_model.py
Browse files- learning_path_model.py +67 -67
learning_path_model.py
CHANGED
|
@@ -1,67 +1,67 @@
|
|
| 1 |
-
# learning_path_model.py
|
| 2 |
-
from sentence_transformers import SentenceTransformer
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
from langchain import PromptTemplate, LLMChain, SequentialChain
|
| 5 |
-
from langchain_groq import ChatGroq
|
| 6 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
-
from dotenv import load_dotenv
|
| 8 |
-
import os
|
| 9 |
-
|
| 10 |
-
load_dotenv()
|
| 11 |
-
api_key = os.getenv('GROQ_API_KEY')
|
| 12 |
-
# print(api_key)
|
| 13 |
-
|
| 14 |
-
class LearningPathModel:
|
| 15 |
-
def __init__(self):
|
| 16 |
-
# Initialize embedding and NLP models
|
| 17 |
-
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 18 |
-
self.qa_pipeline = pipeline('question-answering', model='distilbert-base-cased-distilled-squad')
|
| 19 |
-
self.summarizer = pipeline('summarization', model='facebook/bart-large-cnn')
|
| 20 |
-
|
| 21 |
-
# Define LangChain elements
|
| 22 |
-
self.embedding_chain = LLMChain(
|
| 23 |
-
llm=ChatGroq(model_name="llama-3.1-70b-versatile"), # Example: replace with Groq LLM chain if needed
|
| 24 |
-
prompt=PromptTemplate(template="Generate an embedding for the following text: {text}", input_variables=["text"])
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
self.qa_chain = LLMChain(
|
| 28 |
-
llm=ChatGroq(model_name="llama-3.1-70b-versatile"),
|
| 29 |
-
prompt=PromptTemplate(template="Based on the context provided, answer the question: {question}. Context: {context}", input_variables=["question", "context"])
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
self.summarization_chain = LLMChain(
|
| 33 |
-
llm=ChatGroq(model_name="llama-3.1-70b-versatile"),
|
| 34 |
-
prompt=PromptTemplate(template="Summarize the following text: {text}", input_variables=["text"])
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
# Combine chains into a sequential chain
|
| 38 |
-
self.sequential_chain = SequentialChain(chains=[self.embedding_chain, self.qa_chain, self.summarization_chain], input_variables=['text', 'question', 'context'])
|
| 39 |
-
|
| 40 |
-
def generate_embeddings(self, content_list):
|
| 41 |
-
# Generate embeddings for a list of content items
|
| 42 |
-
embeddings = [self.embedding_model.encode(content) for content in content_list]
|
| 43 |
-
return embeddings
|
| 44 |
-
|
| 45 |
-
def assess_knowledge(self, question, context):
|
| 46 |
-
# Use the QA pipeline to assess knowledge
|
| 47 |
-
response = self.qa_pipeline(question=question, context=context)
|
| 48 |
-
return response['answer'], response['score']
|
| 49 |
-
|
| 50 |
-
def summarize_content(self, content):
|
| 51 |
-
# Summarize content using the summarizer chain
|
| 52 |
-
summary = self.summarizer(content, max_length=60, min_length=30, do_sample=False)[0]['summary_text']
|
| 53 |
-
return summary
|
| 54 |
-
|
| 55 |
-
def recommend_learning_path(self, user_input, content_data):
|
| 56 |
-
# Generate embeddings
|
| 57 |
-
content_embeddings = self.generate_embeddings([c['description'] for c in content_data])
|
| 58 |
-
user_embedding = self.generate_embeddings([user_input])[0]
|
| 59 |
-
|
| 60 |
-
# Simple similarity scoring (cosine similarity) to match user input with content
|
| 61 |
-
import numpy as np
|
| 62 |
-
similarities = np.dot(content_embeddings, user_embedding) / (np.linalg.norm(content_embeddings, axis=1) * np.linalg.norm(user_embedding))
|
| 63 |
-
|
| 64 |
-
# Recommend top 3 most similar content items
|
| 65 |
-
top_indices = np.argsort(similarities)[-3:][::-1]
|
| 66 |
-
recommendations = [content_data[i] for i in top_indices]
|
| 67 |
-
return recommendations
|
|
|
|
| 1 |
+
# learning_path_model.py
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from langchain.chains import PromptTemplate, LLMChain, SequentialChain
|
| 5 |
+
from langchain_groq import ChatGroq
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
api_key = os.getenv('GROQ_API_KEY')
|
| 12 |
+
# print(api_key)
|
| 13 |
+
|
| 14 |
+
class LearningPathModel:
|
| 15 |
+
def __init__(self):
|
| 16 |
+
# Initialize embedding and NLP models
|
| 17 |
+
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 18 |
+
self.qa_pipeline = pipeline('question-answering', model='distilbert-base-cased-distilled-squad')
|
| 19 |
+
self.summarizer = pipeline('summarization', model='facebook/bart-large-cnn')
|
| 20 |
+
|
| 21 |
+
# Define LangChain elements
|
| 22 |
+
self.embedding_chain = LLMChain(
|
| 23 |
+
llm=ChatGroq(model_name="llama-3.1-70b-versatile"), # Example: replace with Groq LLM chain if needed
|
| 24 |
+
prompt=PromptTemplate(template="Generate an embedding for the following text: {text}", input_variables=["text"])
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
self.qa_chain = LLMChain(
|
| 28 |
+
llm=ChatGroq(model_name="llama-3.1-70b-versatile"),
|
| 29 |
+
prompt=PromptTemplate(template="Based on the context provided, answer the question: {question}. Context: {context}", input_variables=["question", "context"])
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
self.summarization_chain = LLMChain(
|
| 33 |
+
llm=ChatGroq(model_name="llama-3.1-70b-versatile"),
|
| 34 |
+
prompt=PromptTemplate(template="Summarize the following text: {text}", input_variables=["text"])
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Combine chains into a sequential chain
|
| 38 |
+
self.sequential_chain = SequentialChain(chains=[self.embedding_chain, self.qa_chain, self.summarization_chain], input_variables=['text', 'question', 'context'])
|
| 39 |
+
|
| 40 |
+
def generate_embeddings(self, content_list):
|
| 41 |
+
# Generate embeddings for a list of content items
|
| 42 |
+
embeddings = [self.embedding_model.encode(content) for content in content_list]
|
| 43 |
+
return embeddings
|
| 44 |
+
|
| 45 |
+
def assess_knowledge(self, question, context):
|
| 46 |
+
# Use the QA pipeline to assess knowledge
|
| 47 |
+
response = self.qa_pipeline(question=question, context=context)
|
| 48 |
+
return response['answer'], response['score']
|
| 49 |
+
|
| 50 |
+
def summarize_content(self, content):
|
| 51 |
+
# Summarize content using the summarizer chain
|
| 52 |
+
summary = self.summarizer(content, max_length=60, min_length=30, do_sample=False)[0]['summary_text']
|
| 53 |
+
return summary
|
| 54 |
+
|
| 55 |
+
def recommend_learning_path(self, user_input, content_data):
|
| 56 |
+
# Generate embeddings
|
| 57 |
+
content_embeddings = self.generate_embeddings([c['description'] for c in content_data])
|
| 58 |
+
user_embedding = self.generate_embeddings([user_input])[0]
|
| 59 |
+
|
| 60 |
+
# Simple similarity scoring (cosine similarity) to match user input with content
|
| 61 |
+
import numpy as np
|
| 62 |
+
similarities = np.dot(content_embeddings, user_embedding) / (np.linalg.norm(content_embeddings, axis=1) * np.linalg.norm(user_embedding))
|
| 63 |
+
|
| 64 |
+
# Recommend top 3 most similar content items
|
| 65 |
+
top_indices = np.argsort(similarities)[-3:][::-1]
|
| 66 |
+
recommendations = [content_data[i] for i in top_indices]
|
| 67 |
+
return recommendations
|