Update app.py
Browse files
app.py
CHANGED
|
@@ -18,12 +18,27 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 18 |
model = model.to(device)
|
| 19 |
model.eval()
|
| 20 |
|
| 21 |
-
def generate_answer(prompt):
|
| 22 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 23 |
with torch.no_grad():
|
| 24 |
outputs = model.generate(**inputs, max_new_tokens=512)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
|
|
|
|
| 27 |
# Sentence embeddings
|
| 28 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 29 |
|
|
@@ -89,7 +104,17 @@ def ask_question(message, history, selected_titles):
|
|
| 89 |
f"(Page {page_numbers[i]}): {chunks[i]}" for i in I[0]
|
| 90 |
])
|
| 91 |
|
| 92 |
-
prompt = f"""Answer the question using only the context below.\n\nContext:\n{context}\n\nQuestion: {message}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
response = generate_answer(prompt)
|
| 94 |
|
| 95 |
combined_answer += f"**{title}**:\n{response.strip()}\n\n"
|
|
|
|
| 18 |
model = model.to(device)
|
| 19 |
model.eval()
|
| 20 |
|
| 21 |
+
"""def generate_answer(prompt):
|
| 22 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 23 |
with torch.no_grad():
|
| 24 |
outputs = model.generate(**inputs, max_new_tokens=512)
|
| 25 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)"""
|
| 26 |
+
|
| 27 |
+
def generate_answer(prompt):
|
| 28 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
outputs = model.generate(
|
| 31 |
+
**inputs,
|
| 32 |
+
max_new_tokens=512,
|
| 33 |
+
temperature=0.9, # Higher = more creative
|
| 34 |
+
repetition_penalty=1.1, # Penalize repeating the same phrases
|
| 35 |
+
do_sample=True, # Needed for temperature to work
|
| 36 |
+
top_k=50, # Sample from top 50 tokens
|
| 37 |
+
top_p=0.95 # Nucleus sampling
|
| 38 |
+
)
|
| 39 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
|
| 41 |
+
|
| 42 |
# Sentence embeddings
|
| 43 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 44 |
|
|
|
|
| 104 |
f"(Page {page_numbers[i]}): {chunks[i]}" for i in I[0]
|
| 105 |
])
|
| 106 |
|
| 107 |
+
#prompt = f"""Answer the question using only the context below.\n\nContext:\n{context}\n\nQuestion: {message}"""
|
| 108 |
+
prompt = f"""You are a helpful assistant. Provide a thorough and detailed answer to the following question using only the context.
|
| 109 |
+
|
| 110 |
+
Context:
|
| 111 |
+
{context}
|
| 112 |
+
|
| 113 |
+
Question: {message}
|
| 114 |
+
|
| 115 |
+
Answer in detail:
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
response = generate_answer(prompt)
|
| 119 |
|
| 120 |
combined_answer += f"**{title}**:\n{response.strip()}\n\n"
|