Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -66,35 +66,44 @@ class CurriculumChatbot:
|
|
| 66 |
|
| 67 |
def _setup_llm(self):
|
| 68 |
try:
|
| 69 |
-
# Use
|
| 70 |
-
model_name = "
|
| 71 |
pipe = pipeline(
|
| 72 |
"text-generation",
|
| 73 |
model=model_name,
|
| 74 |
max_new_tokens=200,
|
| 75 |
-
temperature=0.
|
| 76 |
do_sample=True,
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
)
|
| 79 |
self.llm = HuggingFacePipeline(pipeline=pipe)
|
| 80 |
|
| 81 |
-
# Create QA prompt template for
|
| 82 |
-
qa_template = """
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
Question: {question}
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
self.qa_chain = LLMChain(llm=self.llm, prompt=PromptTemplate(
|
| 92 |
input_variables=["question", "filled_context"],
|
| 93 |
template=qa_template
|
| 94 |
))
|
| 95 |
|
| 96 |
-
# Create slide selection prompt template for
|
| 97 |
-
slide_selection_template = """
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
Question: {question}
|
| 100 |
|
|
@@ -107,33 +116,37 @@ Which slide is the BEST for teaching this concept to a student? Consider:
|
|
| 107 |
- Which slide explains the concept most clearly?
|
| 108 |
- Which slide would be most helpful for learning?
|
| 109 |
|
| 110 |
-
Return
|
| 111 |
|
| 112 |
-
|
| 113 |
|
| 114 |
self.slide_selection_chain = LLMChain(llm=self.llm, prompt=PromptTemplate(
|
| 115 |
input_variables=["question", "slide_contents"],
|
| 116 |
template=slide_selection_template
|
| 117 |
))
|
| 118 |
|
| 119 |
-
# Create focused answer prompt template
|
| 120 |
-
focused_qa_template = """
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
Slide Content:
|
| 123 |
{slide_content}
|
| 124 |
|
| 125 |
Question: {question}
|
| 126 |
|
| 127 |
-
|
| 128 |
|
| 129 |
self.focused_qa_chain = LLMChain(llm=self.llm, prompt=PromptTemplate(
|
| 130 |
input_variables=["question", "slide_content"],
|
| 131 |
template=focused_qa_template
|
| 132 |
))
|
| 133 |
|
| 134 |
-
print("✅ Llama 3.1
|
| 135 |
except Exception as e:
|
| 136 |
-
print(f"Warning: Could not load Llama 3.1
|
| 137 |
print("Falling back to basic search mode...")
|
| 138 |
self.llm = None
|
| 139 |
self.qa_chain = None
|
|
|
|
| 66 |
|
| 67 |
def _setup_llm(self):
|
| 68 |
try:
|
| 69 |
+
# Use Llama 3.1 8B for better quality answers
|
| 70 |
+
model_name = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
| 71 |
pipe = pipeline(
|
| 72 |
"text-generation",
|
| 73 |
model=model_name,
|
| 74 |
max_new_tokens=200,
|
| 75 |
+
temperature=0.3,
|
| 76 |
do_sample=True,
|
| 77 |
+
top_p=0.9,
|
| 78 |
+
repetition_penalty=1.1,
|
| 79 |
+
device_map="auto" if torch.cuda.is_available() else None
|
| 80 |
)
|
| 81 |
self.llm = HuggingFacePipeline(pipeline=pipe)
|
| 82 |
|
| 83 |
+
# Create QA prompt template for Llama 3.1
|
| 84 |
+
qa_template = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
| 85 |
|
| 86 |
+
You are a helpful AI programming tutor. Answer questions about programming concepts clearly and educationally. If the question is about curriculum content, use the provided context. If not, provide a general programming answer.
|
| 87 |
+
|
| 88 |
+
<|eot_id|><|start_header_id|>user<|end_header_id|>
|
| 89 |
|
| 90 |
Question: {question}
|
| 91 |
|
| 92 |
+
{filled_context}
|
| 93 |
+
|
| 94 |
+
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
|
| 95 |
|
| 96 |
self.qa_chain = LLMChain(llm=self.llm, prompt=PromptTemplate(
|
| 97 |
input_variables=["question", "filled_context"],
|
| 98 |
template=qa_template
|
| 99 |
))
|
| 100 |
|
| 101 |
+
# Create slide selection prompt template for Llama 3.1
|
| 102 |
+
slide_selection_template = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
| 103 |
+
|
| 104 |
+
You are an AI that analyzes curriculum slides to find the best one for teaching a concept. Return ONLY the filename and page number.
|
| 105 |
+
|
| 106 |
+
<|eot_id|><|start_header_id|>user<|end_header_id|>
|
| 107 |
|
| 108 |
Question: {question}
|
| 109 |
|
|
|
|
| 116 |
- Which slide explains the concept most clearly?
|
| 117 |
- Which slide would be most helpful for learning?
|
| 118 |
|
| 119 |
+
Return only: "filename.pdf - Page X"
|
| 120 |
|
| 121 |
+
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
|
| 122 |
|
| 123 |
self.slide_selection_chain = LLMChain(llm=self.llm, prompt=PromptTemplate(
|
| 124 |
input_variables=["question", "slide_contents"],
|
| 125 |
template=slide_selection_template
|
| 126 |
))
|
| 127 |
|
| 128 |
+
# Create focused answer prompt template for Llama 3.1
|
| 129 |
+
focused_qa_template = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
| 130 |
+
|
| 131 |
+
You are a helpful AI programming tutor. Answer questions about programming concepts clearly and educationally based on the provided slide content.
|
| 132 |
+
|
| 133 |
+
<|eot_id|><|start_header_id|>user<|end_header_id|>
|
| 134 |
|
| 135 |
Slide Content:
|
| 136 |
{slide_content}
|
| 137 |
|
| 138 |
Question: {question}
|
| 139 |
|
| 140 |
+
<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
|
| 141 |
|
| 142 |
self.focused_qa_chain = LLMChain(llm=self.llm, prompt=PromptTemplate(
|
| 143 |
input_variables=["question", "slide_content"],
|
| 144 |
template=focused_qa_template
|
| 145 |
))
|
| 146 |
|
| 147 |
+
print("✅ Llama 3.1 8B loaded successfully!")
|
| 148 |
except Exception as e:
|
| 149 |
+
print(f"Warning: Could not load Llama 3.1 8B: {e}")
|
| 150 |
print("Falling back to basic search mode...")
|
| 151 |
self.llm = None
|
| 152 |
self.qa_chain = None
|