Spaces:
Sleeping
Sleeping
fixing sourcereturn and upping sources to 5
Browse files
app.py
CHANGED
|
@@ -25,7 +25,7 @@ client = OpenAIOG()
|
|
| 25 |
# Load index for retrieval
|
| 26 |
storage_context = StorageContext.from_defaults(persist_dir="arv_metadata")
|
| 27 |
index = load_index_from_storage(storage_context)
|
| 28 |
-
retriever = index.as_retriever(similarity_top_k=
|
| 29 |
|
| 30 |
# Define keyword lists
|
| 31 |
acknowledgment_keywords_sw = ["sawa", "ndiyo", "naam", "hakika", "asante", "nimeelewa", "nimekupata", "ni kweli", "kwa hakika", "nimesikia", "ahsante"]
|
|
@@ -96,10 +96,12 @@ def nishauri(question, conversation_history: list[str]):
|
|
| 96 |
)
|
| 97 |
reply_to_user = completion.choices[0].message.content
|
| 98 |
conversation_history.append({"user": question, "chatbot": reply_to_user})
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Detect language and translate if needed
|
| 105 |
lang_question = detect_language(question)
|
|
@@ -108,7 +110,7 @@ def nishauri(question, conversation_history: list[str]):
|
|
| 108 |
|
| 109 |
# Retrieve relevant sources
|
| 110 |
sources = retriever.retrieve(question)
|
| 111 |
-
retrieved_text = "\n\n".join([f"Source {i+1}: {source.text}" for i, source in enumerate(sources[:
|
| 112 |
|
| 113 |
source1return = ("File Name: " +
|
| 114 |
sources[0].metadata["file_name"] +
|
|
@@ -131,6 +133,20 @@ def nishauri(question, conversation_history: list[str]):
|
|
| 131 |
"\n Source Text: " +
|
| 132 |
sources[2].text)
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# Combine into new user question - conversation history, new question, retrieved sources
|
| 135 |
question_final = (
|
| 136 |
f"The user asked the following question: \"{question}\"\n\n"
|
|
@@ -189,7 +205,7 @@ def nishauri(question, conversation_history: list[str]):
|
|
| 189 |
reply_to_user = GoogleTranslator(source='auto', target='sw').translate(reply_to_user)
|
| 190 |
|
| 191 |
# return system_prompt, conversation_history
|
| 192 |
-
return reply_to_user, source1return, source2return, source3return, conversation_history
|
| 193 |
|
| 194 |
#%%
|
| 195 |
demo = gr.Interface(
|
|
@@ -202,6 +218,8 @@ demo = gr.Interface(
|
|
| 202 |
gr.Textbox(label = "Source 1", max_lines = 10, autoscroll = False, type = "text"),
|
| 203 |
gr.Textbox(label = "Source 2", max_lines = 10, autoscroll = False, type = "text"),
|
| 204 |
gr.Textbox(label = "Source 3", max_lines = 10, autoscroll = False, type = "text"),
|
|
|
|
|
|
|
| 205 |
gr.State()
|
| 206 |
],
|
| 207 |
)
|
|
|
|
| 25 |
# Load index for retrieval
|
| 26 |
storage_context = StorageContext.from_defaults(persist_dir="arv_metadata")
|
| 27 |
index = load_index_from_storage(storage_context)
|
| 28 |
+
retriever = index.as_retriever(similarity_top_k=5)
|
| 29 |
|
| 30 |
# Define keyword lists
|
| 31 |
acknowledgment_keywords_sw = ["sawa", "ndiyo", "naam", "hakika", "asante", "nimeelewa", "nimekupata", "ni kweli", "kwa hakika", "nimesikia", "ahsante"]
|
|
|
|
| 96 |
)
|
| 97 |
reply_to_user = completion.choices[0].message.content
|
| 98 |
conversation_history.append({"user": question, "chatbot": reply_to_user})
|
| 99 |
+
source1Return = ""
|
| 100 |
+
source2Return = ""
|
| 101 |
+
source3Return = ""
|
| 102 |
+
source4Return = ""
|
| 103 |
+
source5Return = ""
|
| 104 |
+
return reply_to_user, source1return, source2return, source3return, source4return, source5return, conversation_history
|
| 105 |
|
| 106 |
# Detect language and translate if needed
|
| 107 |
lang_question = detect_language(question)
|
|
|
|
| 110 |
|
| 111 |
# Retrieve relevant sources
|
| 112 |
sources = retriever.retrieve(question)
|
| 113 |
+
retrieved_text = "\n\n".join([f"Source {i+1}: {source.text}" for i, source in enumerate(sources[:5])])
|
| 114 |
|
| 115 |
source1return = ("File Name: " +
|
| 116 |
sources[0].metadata["file_name"] +
|
|
|
|
| 133 |
"\n Source Text: " +
|
| 134 |
sources[2].text)
|
| 135 |
|
| 136 |
+
source4return = ("File Name: " +
|
| 137 |
+
sources[3].metadata["file_name"] +
|
| 138 |
+
"\nPage Number: " +
|
| 139 |
+
sources[3].metadata["page_label"] +
|
| 140 |
+
"\n Source Text: " +
|
| 141 |
+
sources[3].text)
|
| 142 |
+
|
| 143 |
+
source5return = ("File Name: " +
|
| 144 |
+
sources[4].metadata["file_name"] +
|
| 145 |
+
"\nPage Number: " +
|
| 146 |
+
sources[4].metadata["page_label"] +
|
| 147 |
+
"\n Source Text: " +
|
| 148 |
+
sources[4].text)
|
| 149 |
+
|
| 150 |
# Combine into new user question - conversation history, new question, retrieved sources
|
| 151 |
question_final = (
|
| 152 |
f"The user asked the following question: \"{question}\"\n\n"
|
|
|
|
| 205 |
reply_to_user = GoogleTranslator(source='auto', target='sw').translate(reply_to_user)
|
| 206 |
|
| 207 |
# return system_prompt, conversation_history
|
| 208 |
+
return reply_to_user, source1return, source2return, source3return, source4return, source5return, conversation_history
|
| 209 |
|
| 210 |
#%%
|
| 211 |
demo = gr.Interface(
|
|
|
|
| 218 |
gr.Textbox(label = "Source 1", max_lines = 10, autoscroll = False, type = "text"),
|
| 219 |
gr.Textbox(label = "Source 2", max_lines = 10, autoscroll = False, type = "text"),
|
| 220 |
gr.Textbox(label = "Source 3", max_lines = 10, autoscroll = False, type = "text"),
|
| 221 |
+
gr.Textbox(label = "Source 4", max_lines = 10, autoscroll = False, type = "text"),
|
| 222 |
+
gr.Textbox(label = "Source 5", max_lines = 10, autoscroll = False, type = "text"),
|
| 223 |
gr.State()
|
| 224 |
],
|
| 225 |
)
|