IW2025 commited on
Commit
be64db7
·
verified ·
1 Parent(s): dbb7b33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -66,35 +66,44 @@ class CurriculumChatbot:
66
 
67
  def _setup_llm(self):
68
  try:
69
- # Use a smaller, faster model for Hugging Face Spaces
70
- model_name = "microsoft/DialoGPT-medium" # Smaller model for faster inference
71
  pipe = pipeline(
72
  "text-generation",
73
  model=model_name,
74
  max_new_tokens=200,
75
- temperature=0.7,
76
  do_sample=True,
77
- pad_token_id=50256
 
 
78
  )
79
  self.llm = HuggingFacePipeline(pipeline=pipe)
80
 
81
- # Create QA prompt template for DialoGPT
82
- qa_template = """You are a helpful programming tutor. Answer the following question based on the curriculum content provided.
83
 
84
- Curriculum Content:
85
- {filled_context}
 
86
 
87
  Question: {question}
88
 
89
- Provide a clear, educational answer explaining the concept:"""
 
 
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 DialoGPT
97
- slide_selection_template = """You are an AI that analyzes curriculum slides to find the best one for teaching a concept.
 
 
 
 
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 ONLY the filename and page number like this: "filename.pdf - Page X"
111
 
112
- Answer:"""
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 = """You are a helpful programming tutor. Answer the question based on the specific slide content provided.
 
 
 
 
121
 
122
  Slide Content:
123
  {slide_content}
124
 
125
  Question: {question}
126
 
127
- Provide a clear, educational answer based on this slide:"""
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-8B loaded successfully!")
135
  except Exception as e:
136
- print(f"Warning: Could not load Llama 3.1-8B: {e}")
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