Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,27 +9,23 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
9 |
from langchain_community.llms import HuggingFacePipeline
|
10 |
from langchain.memory import ConversationBufferMemory
|
11 |
|
12 |
-
from pathlib import Path
|
13 |
-
import chromadb
|
14 |
-
from unidecode import unidecode
|
15 |
-
|
16 |
from transformers import AutoTokenizer, pipeline
|
17 |
-
import transformers
|
18 |
import torch
|
19 |
-
|
|
|
20 |
|
21 |
# Lista de modelos 100% abertos e gratuitos
|
22 |
list_llm = [
|
23 |
-
"google/flan-t5-xxl",
|
24 |
-
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
25 |
-
"microsoft/phi-2",
|
26 |
-
"facebook/opt-1.3b",
|
27 |
-
"EleutherAI/gpt-neo-1.3B",
|
28 |
-
"bigscience/bloom-1b7",
|
29 |
-
"RWKV/rwkv-4-169m-pile",
|
30 |
-
"gpt2-medium",
|
31 |
-
"databricks/dolly-v2-3b",
|
32 |
-
"mosaicml/mpt-7b-instruct"
|
33 |
]
|
34 |
|
35 |
list_llm_simple = [os.path.basename(llm) for llm in list_llm]
|
@@ -52,7 +48,7 @@ def create_db(splits, collection_name):
|
|
52 |
return Chroma.from_documents(
|
53 |
documents=splits,
|
54 |
embedding=embedding,
|
55 |
-
client=chromadb.
|
56 |
collection_name=collection_name
|
57 |
)
|
58 |
|
@@ -88,7 +84,6 @@ def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, pr
|
|
88 |
llm=llm,
|
89 |
retriever=vector_db.as_retriever(),
|
90 |
memory=memory,
|
91 |
-
chain_type="stuff",
|
92 |
return_source_documents=True
|
93 |
)
|
94 |
|
@@ -118,7 +113,7 @@ def demo():
|
|
118 |
with gr.Tab("💬 Chat"):
|
119 |
chatbot = gr.Chatbot(height=400)
|
120 |
msg = gr.Textbox(label="Sua mensagem")
|
121 |
-
clear_btn = gr.
|
122 |
|
123 |
# Eventos
|
124 |
process_btn.click(
|
@@ -136,16 +131,20 @@ def demo():
|
|
136 |
def respond(message, chat_history):
|
137 |
if qa_chain.value is None:
|
138 |
return "Por favor, carregue um modelo primeiro.", chat_history
|
|
|
139 |
result = qa_chain.value({"question": message, "chat_history": chat_history})
|
140 |
response = result["answer"]
|
|
|
141 |
sources = "\n".join([f"📄 Página {doc.metadata['page']+1}: {doc.page_content[:50]}..."
|
142 |
-
|
|
|
143 |
chat_history.append((message, f"{response}\n\n🔍 Fontes:\n{sources}"))
|
144 |
return "", chat_history
|
145 |
|
146 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
|
147 |
|
148 |
demo.launch()
|
149 |
|
150 |
if __name__ == "__main__":
|
151 |
-
demo()
|
|
|
9 |
from langchain_community.llms import HuggingFacePipeline
|
10 |
from langchain.memory import ConversationBufferMemory
|
11 |
|
|
|
|
|
|
|
|
|
12 |
from transformers import AutoTokenizer, pipeline
|
|
|
13 |
import torch
|
14 |
+
|
15 |
+
import chromadb
|
16 |
|
17 |
# Lista de modelos 100% abertos e gratuitos
|
18 |
list_llm = [
|
19 |
+
"google/flan-t5-xxl",
|
20 |
+
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
21 |
+
"microsoft/phi-2",
|
22 |
+
"facebook/opt-1.3b",
|
23 |
+
"EleutherAI/gpt-neo-1.3B",
|
24 |
+
"bigscience/bloom-1b7",
|
25 |
+
"RWKV/rwkv-4-169m-pile",
|
26 |
+
"gpt2-medium",
|
27 |
+
"databricks/dolly-v2-3b",
|
28 |
+
"mosaicml/mpt-7b-instruct"
|
29 |
]
|
30 |
|
31 |
list_llm_simple = [os.path.basename(llm) for llm in list_llm]
|
|
|
48 |
return Chroma.from_documents(
|
49 |
documents=splits,
|
50 |
embedding=embedding,
|
51 |
+
client=chromadb.PersistentClient(),
|
52 |
collection_name=collection_name
|
53 |
)
|
54 |
|
|
|
84 |
llm=llm,
|
85 |
retriever=vector_db.as_retriever(),
|
86 |
memory=memory,
|
|
|
87 |
return_source_documents=True
|
88 |
)
|
89 |
|
|
|
113 |
with gr.Tab("💬 Chat"):
|
114 |
chatbot = gr.Chatbot(height=400)
|
115 |
msg = gr.Textbox(label="Sua mensagem")
|
116 |
+
clear_btn = gr.Button("Limpar Chat")
|
117 |
|
118 |
# Eventos
|
119 |
process_btn.click(
|
|
|
131 |
def respond(message, chat_history):
|
132 |
if qa_chain.value is None:
|
133 |
return "Por favor, carregue um modelo primeiro.", chat_history
|
134 |
+
|
135 |
result = qa_chain.value({"question": message, "chat_history": chat_history})
|
136 |
response = result["answer"]
|
137 |
+
|
138 |
sources = "\n".join([f"📄 Página {doc.metadata['page']+1}: {doc.page_content[:50]}..."
|
139 |
+
for doc in result.get("source_documents", [])[:2]])
|
140 |
+
|
141 |
chat_history.append((message, f"{response}\n\n🔍 Fontes:\n{sources}"))
|
142 |
return "", chat_history
|
143 |
|
144 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
145 |
+
clear_btn.click(lambda: [], outputs=[chatbot])
|
146 |
|
147 |
demo.launch()
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
+
demo()
|