Update app.py
Browse files
app.py
CHANGED
|
@@ -69,21 +69,35 @@ def truncate_text(text, max_tokens=1000):
|
|
| 69 |
text = tokenizer.decode(tokens)
|
| 70 |
return text
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def chat_with_context(question: str) -> str:
|
| 73 |
if not indexed_texts:
|
| 74 |
return "Please index some text first."
|
| 75 |
|
| 76 |
context = find_most_similar(question)[0]
|
| 77 |
-
|
| 78 |
-
question = truncate_text(question, max_tokens=
|
| 79 |
-
prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Translation function
|
| 89 |
def translate_text(text, target_lang_code):
|
|
|
|
| 69 |
text = tokenizer.decode(tokens)
|
| 70 |
return text
|
| 71 |
|
| 72 |
+
def sliding_window(text, window_size=512, stride=256):
|
| 73 |
+
tokens = tokenizer.encode(text)
|
| 74 |
+
windows = []
|
| 75 |
+
for i in range(0, len(tokens), stride):
|
| 76 |
+
window = tokens[i:i+window_size]
|
| 77 |
+
windows.append(tokenizer.decode(window))
|
| 78 |
+
return windows
|
| 79 |
+
|
| 80 |
def chat_with_context(question: str) -> str:
|
| 81 |
if not indexed_texts:
|
| 82 |
return "Please index some text first."
|
| 83 |
|
| 84 |
context = find_most_similar(question)[0]
|
| 85 |
+
windows = sliding_window(context, window_size=512, stride=256)
|
| 86 |
+
question = truncate_text(question, max_tokens=100) # Further reduce question length
|
|
|
|
| 87 |
|
| 88 |
+
responses = []
|
| 89 |
+
for window in windows:
|
| 90 |
+
prompt = f"Context: {window}\n\nQuestion: {question}\n\nAnswer:"
|
| 91 |
+
try:
|
| 92 |
+
response = chat_model(prompt, max_length=1024, num_return_sequences=1)
|
| 93 |
+
answer = response[0]['generated_text'].split("Answer:")[-1].strip()
|
| 94 |
+
responses.append(answer)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
logging.error(f"Error in chat: {str(e)}")
|
| 97 |
+
|
| 98 |
+
# Combine and truncate responses
|
| 99 |
+
combined_response = " ".join(responses)
|
| 100 |
+
return truncate_text(combined_response, max_tokens=500)
|
| 101 |
|
| 102 |
# Translation function
|
| 103 |
def translate_text(text, target_lang_code):
|