phucndh commited on
Commit
04fffa4
·
1 Parent(s): ae39eb5

Start test v10

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -121,17 +121,35 @@ def answer_query(question):
121
  if not question.strip():
122
  return "Vui lòng nhập câu hỏi."
123
 
124
- # Tạo prompt kết hợp tài liệu câu hỏi
125
- prompt = f"Dựa trên tài liệu sau đây:\n{document_content}\nHỏi: {question}\nTrả lời:"
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  input_ids = tokenizer.encode(prompt, return_tensors="pt", max_length=512, truncation=True)
128
- outputs = model.generate(input_ids, max_length=150)
 
 
 
 
 
 
 
 
129
  answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
130
  if len(answer.split()) < 3:
131
- return "Nội dung nằm ngoài tài liệu."
132
  return answer
133
 
134
-
135
  chat_interface = gr.Interface(
136
  fn=answer_query,
137
  inputs=gr.Textbox(lines=2, placeholder="Nhập câu hỏi của bạn..."),
 
121
  if not question.strip():
122
  return "Vui lòng nhập câu hỏi."
123
 
124
+ # Nếu nội dung tài liệu quá dài, giới hạn lại (ví dụ: chỉ lấy phần đầu)
125
+ if len(document_content) > 1000:
126
+ document_content = document_content[:1000] + "\n... (đã rút gọn)"
127
+
128
+ # Tạo prompt với chỉ dẫn rõ ràng
129
+ prompt = (
130
+ "Bạn là một chuyên gia pháp luật. Hãy trả lời câu hỏi dưới đây dựa trên nội dung tài liệu.\n\n"
131
+ "Nội dung tài liệu:\n"
132
+ f"{document_content}\n\n"
133
+ "Câu hỏi: {question}\n\n"
134
+ "Trả lời chi tiết:"
135
+ )
136
+ prompt = prompt.format(question=question)
137
 
138
  input_ids = tokenizer.encode(prompt, return_tensors="pt", max_length=512, truncation=True)
139
+
140
+ # Sử dụng beam search để tạo ra câu trả lời mạch lạc hơn
141
+ outputs = model.generate(
142
+ input_ids,
143
+ max_length=200,
144
+ num_beams=5,
145
+ early_stopping=True
146
+ )
147
+
148
  answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
149
  if len(answer.split()) < 3:
150
+ return "Nội dung nằm ngoài tài liệu hoặc mô hình chưa được huấn luyện tốt."
151
  return answer
152
 
 
153
  chat_interface = gr.Interface(
154
  fn=answer_query,
155
  inputs=gr.Textbox(lines=2, placeholder="Nhập câu hỏi của bạn..."),