SF001-123456 commited on
Commit
800669e
·
verified ·
1 Parent(s): 6c5c0b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -27,28 +27,28 @@ class ChatbotUI:
27
  # Retrieve relevant context from Pinecone
28
  retrieved_docs = self.retriever.retrieve_context(input_text, top_k=1)
29
 
30
- # Preset instruction for the legal assistant
31
  preset = "You are a HR legal assistant. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'. Avoid Yes or No."
32
-
33
- # Combine retrieved context with the user query to form the prompt
34
- prompt = f"{preset}\nContext: {retrieved_docs}\nUser: {input_text}\nResponse:"
35
- logging.info(f"Generated prompt for TinyLlama: {prompt}")
36
 
37
- # Generate response using the model
38
- inputs = self.trainer.tokenizer(prompt, return_tensors="pt").input_ids
39
- output = self.trainer.model.generate(inputs, max_length=2000, do_sample=True, top_p=0.95, temperature=0.1, repetition_penalty=1.2)
40
- response = self.trainer.tokenizer.decode(output[0], skip_special_tokens=True)
41
- logging.info("Response generated successfully.")
42
-
43
- # Ensure response does not repeat the prompt
44
- if response.startswith(prompt):
45
- response = response[len(prompt):].strip()
46
 
47
- # Append the new turn to the conversation history
48
- history.append((input_text, response))
49
- logging.info(f"Updated conversation history: {history}")
50
-
51
- return history, ""
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  def clear_conversation(self):
54
  """Clears the entire conversation history and resets the input box."""
 
27
  # Retrieve relevant context from Pinecone
28
  retrieved_docs = self.retriever.retrieve_context(input_text, top_k=1)
29
 
 
30
  preset = "You are a HR legal assistant. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'. Avoid Yes or No."
 
 
 
 
31
 
32
+ latest_prompt = f"{preset}\n\n### Context: {retrieved_docs.strip()}\n\n### User: {prompt.strip()}\n\n### Response:"
 
 
 
 
 
 
 
 
33
 
34
+ #latest_prompt = f"{preset}\n\n### User: {prompt.strip()}\n\n### Response:"
35
+
36
+ inputs = tokenizer(latest_prompt, return_tensors="pt").to(model.device)
37
+
38
+ outputs = model.generate(
39
+ **inputs,
40
+ max_new_tokens=2000, # Reduce token size to optimize speed
41
+ do_sample=True,
42
+ top_p=0.95,
43
+ temperature=0.1,
44
+ repetition_penalty=1.2,
45
+ )
46
+
47
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
48
+ if response.startswith(latest_prompt):
49
+ response = response[len(latest_prompt):].strip()
50
+
51
+ return response
52
 
53
  def clear_conversation(self):
54
  """Clears the entire conversation history and resets the input box."""