Aranwer commited on
Commit
b6df14b
·
verified ·
1 Parent(s): c7e4cf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -1
app.py CHANGED
@@ -46,12 +46,23 @@ def legal_assistant_query(query):
46
  query_embedding = embedder.encode([query])
47
  D, I = index.search(np.array(query_embedding), k=5)
48
 
 
49
  retrieved_docs = [corpus[i] for i in I[0]]
50
- context_combined = "\n\n".join(retrieved_docs)
 
 
 
 
 
 
51
 
 
52
  prompt = f"Given the following legal references, answer the question:\n\n{context_combined}\n\nQuestion: {query}\nAnswer:"
 
 
53
  result = generator(prompt, max_new_tokens=200, do_sample=True)[0]['generated_text']
54
 
 
55
  return result.split("Answer:")[-1].strip()
56
 
57
  # Gradio Interface
 
46
  query_embedding = embedder.encode([query])
47
  D, I = index.search(np.array(query_embedding), k=5)
48
 
49
+ # Limit the number of retrieved documents or trim context
50
  retrieved_docs = [corpus[i] for i in I[0]]
51
+
52
+ # Combine retrieved documents into a single context and ensure it doesn't exceed token limit
53
+ context_combined = "\n\n".join(retrieved_docs[:3]) # Limit to 3 docs to avoid overflow
54
+ max_length = 1024 # Set appropriate limit based on GPT-2's token length (around 1024 tokens)
55
+
56
+ # Ensure the context combined does not exceed max length
57
+ context_combined = context_combined[:max_length]
58
 
59
+ # Prepare the prompt for GPT-2
60
  prompt = f"Given the following legal references, answer the question:\n\n{context_combined}\n\nQuestion: {query}\nAnswer:"
61
+
62
+ # Generate the response
63
  result = generator(prompt, max_new_tokens=200, do_sample=True)[0]['generated_text']
64
 
65
+ # Extract the answer from the generated text
66
  return result.split("Answer:")[-1].strip()
67
 
68
  # Gradio Interface