Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- simple_script.py +18 -6
simple_script.py
CHANGED
|
@@ -55,6 +55,7 @@ class Chatbot:
|
|
| 55 |
self.index = index
|
| 56 |
self.conversation_id = str(uuid.uuid4())
|
| 57 |
self.co = co
|
|
|
|
| 58 |
|
| 59 |
def generate_response(self, message: str):
|
| 60 |
"""
|
|
@@ -80,6 +81,8 @@ class Chatbot:
|
|
| 80 |
|
| 81 |
documents = self.retrieve_docs(response)
|
| 82 |
|
|
|
|
|
|
|
| 83 |
response = self.co.chat(
|
| 84 |
message=message,
|
| 85 |
documents=documents,
|
|
@@ -134,13 +137,15 @@ with gr.Blocks() as demo:
|
|
| 134 |
return "", history + [[user_message, None]]
|
| 135 |
|
| 136 |
def chat_function(history, cohere_chatbot):
|
| 137 |
-
flag = False
|
| 138 |
if cohere_chatbot is None:
|
| 139 |
cohere_chatbot = Chatbot(co, index)
|
| 140 |
|
| 141 |
message = history[-1][0]
|
| 142 |
history[-1][1] = ""
|
| 143 |
|
|
|
|
|
|
|
|
|
|
| 144 |
for event in cohere_chatbot.generate_response(message):
|
| 145 |
if event.event_type == "text-generation":
|
| 146 |
history[-1][1] += str(event.text)
|
|
@@ -148,12 +153,19 @@ with gr.Blocks() as demo:
|
|
| 148 |
|
| 149 |
# Citations
|
| 150 |
if event.event_type == "citation-generation":
|
| 151 |
-
if
|
| 152 |
-
history[-1][1] += "\n\
|
| 153 |
yield history, cohere_chatbot
|
| 154 |
-
flag =
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 159 |
chat_function, [chatbot, cohere_chatbot_var], [chatbot, cohere_chatbot_var]
|
|
|
|
| 55 |
self.index = index
|
| 56 |
self.conversation_id = str(uuid.uuid4())
|
| 57 |
self.co = co
|
| 58 |
+
self.docs = None
|
| 59 |
|
| 60 |
def generate_response(self, message: str):
|
| 61 |
"""
|
|
|
|
| 81 |
|
| 82 |
documents = self.retrieve_docs(response)
|
| 83 |
|
| 84 |
+
self.docs = {f"doc_{i}": document for i, document in enumerate(documents)}
|
| 85 |
+
|
| 86 |
response = self.co.chat(
|
| 87 |
message=message,
|
| 88 |
documents=documents,
|
|
|
|
| 137 |
return "", history + [[user_message, None]]
|
| 138 |
|
| 139 |
def chat_function(history, cohere_chatbot):
|
|
|
|
| 140 |
if cohere_chatbot is None:
|
| 141 |
cohere_chatbot = Chatbot(co, index)
|
| 142 |
|
| 143 |
message = history[-1][0]
|
| 144 |
history[-1][1] = ""
|
| 145 |
|
| 146 |
+
documents_used = set()
|
| 147 |
+
flag = True
|
| 148 |
+
|
| 149 |
for event in cohere_chatbot.generate_response(message):
|
| 150 |
if event.event_type == "text-generation":
|
| 151 |
history[-1][1] += str(event.text)
|
|
|
|
| 153 |
|
| 154 |
# Citations
|
| 155 |
if event.event_type == "citation-generation":
|
| 156 |
+
if flag:
|
| 157 |
+
history[-1][1] += "\n\n**DOCUMENTS CONSULTED:**\n\n"
|
| 158 |
yield history, cohere_chatbot
|
| 159 |
+
flag = False
|
| 160 |
+
|
| 161 |
+
for citation in event.citations:
|
| 162 |
+
documents_used.update(citation["document_ids"])
|
| 163 |
+
|
| 164 |
+
urls_used = set(cohere_chatbot.docs[doc_id]["url"] for doc_id in documents_used)
|
| 165 |
+
|
| 166 |
+
for url in sorted(urls_used):
|
| 167 |
+
history[-1][1] += f"* {url}\n"
|
| 168 |
+
yield history, cohere_chatbot
|
| 169 |
|
| 170 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 171 |
chat_function, [chatbot, cohere_chatbot_var], [chatbot, cohere_chatbot_var]
|