Update app.py
Browse files
app.py
CHANGED
|
@@ -48,14 +48,26 @@ audio_language_dict = {
|
|
| 48 |
def index_text(text: str) -> str:
|
| 49 |
global indexed_texts, indexed_embeddings
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
return f"Error indexing text: {str(e)}"
|
| 57 |
|
| 58 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
query_embedding = sentence_model.encode([query])[0]
|
| 60 |
similarities = [np.dot(query_embedding, doc_embedding) for doc_embedding in indexed_embeddings]
|
| 61 |
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
|
@@ -65,7 +77,8 @@ def chat_with_context(question: str, model: str) -> str:
|
|
| 65 |
if not indexed_texts:
|
| 66 |
return "Please index some text first."
|
| 67 |
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
try:
|
| 71 |
prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
|
|
@@ -116,6 +129,7 @@ with gr.Blocks() as iface:
|
|
| 116 |
|
| 117 |
with gr.Row():
|
| 118 |
index_button = gr.Button("Index")
|
|
|
|
| 119 |
|
| 120 |
index_status = gr.Textbox(label="Indexing Status")
|
| 121 |
|
|
@@ -159,6 +173,7 @@ with gr.Blocks() as iface:
|
|
| 159 |
|
| 160 |
convert_button.click(convert_text, inputs=[text_input, translation_lang_dropdown], outputs=translated_text)
|
| 161 |
index_button.click(index_text, inputs=[translated_text], outputs=[index_status])
|
|
|
|
| 162 |
use_chat.change(update_chat_visibility, inputs=[use_chat], outputs=[chat_group])
|
| 163 |
chat_button.click(chat_with_context, inputs=[chat_input, chat_model], outputs=[chat_output])
|
| 164 |
|
|
|
|
| 48 |
def index_text(text: str) -> str:
|
| 49 |
global indexed_texts, indexed_embeddings
|
| 50 |
try:
|
| 51 |
+
# Split the text into sentences or smaller chunks
|
| 52 |
+
chunks = text.split('. ')
|
| 53 |
+
for chunk in chunks:
|
| 54 |
+
if chunk:
|
| 55 |
+
embedding = sentence_model.encode([chunk])[0]
|
| 56 |
+
indexed_texts.append(chunk)
|
| 57 |
+
indexed_embeddings.append(embedding)
|
| 58 |
+
return f"Text indexed successfully. Total indexed chunks: {len(indexed_texts)}"
|
| 59 |
except Exception as e:
|
| 60 |
return f"Error indexing text: {str(e)}"
|
| 61 |
|
| 62 |
+
def clear_index() -> str:
|
| 63 |
+
global indexed_texts, indexed_embeddings
|
| 64 |
+
indexed_texts.clear()
|
| 65 |
+
indexed_embeddings.clear()
|
| 66 |
+
return "Index cleared successfully. Ready for new indexing."
|
| 67 |
+
|
| 68 |
+
def find_most_similar(query: str, top_k: int = 3) -> list:
|
| 69 |
+
if not indexed_texts:
|
| 70 |
+
return ["No indexed text available."]
|
| 71 |
query_embedding = sentence_model.encode([query])[0]
|
| 72 |
similarities = [np.dot(query_embedding, doc_embedding) for doc_embedding in indexed_embeddings]
|
| 73 |
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
|
|
|
| 77 |
if not indexed_texts:
|
| 78 |
return "Please index some text first."
|
| 79 |
|
| 80 |
+
relevant_contexts = find_most_similar(question, top_k=3)
|
| 81 |
+
context = " ".join(relevant_contexts)
|
| 82 |
|
| 83 |
try:
|
| 84 |
prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
|
|
|
|
| 129 |
|
| 130 |
with gr.Row():
|
| 131 |
index_button = gr.Button("Index")
|
| 132 |
+
clear_index_button = gr.Button("Clear Index")
|
| 133 |
|
| 134 |
index_status = gr.Textbox(label="Indexing Status")
|
| 135 |
|
|
|
|
| 173 |
|
| 174 |
convert_button.click(convert_text, inputs=[text_input, translation_lang_dropdown], outputs=translated_text)
|
| 175 |
index_button.click(index_text, inputs=[translated_text], outputs=[index_status])
|
| 176 |
+
clear_index_button.click(clear_index, outputs=[index_status])
|
| 177 |
use_chat.change(update_chat_visibility, inputs=[use_chat], outputs=[chat_group])
|
| 178 |
chat_button.click(chat_with_context, inputs=[chat_input, chat_model], outputs=[chat_output])
|
| 179 |
|