Spaces:
Build error
Build error
alen
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from langchain_community.chat_models import ChatOllama
|
| 3 |
-
from langchain_community.embeddings import GPT4AllEmbeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
from langchain.prompts import ChatPromptTemplate
|
| 6 |
-
from langchain.schema.runnable import RunnablePassthrough
|
| 7 |
-
from langchain_community.vectorstores import FAISS
|
| 8 |
# import os
|
| 9 |
# os.system("ollama pull alen_ox/llama_3_fin")
|
| 10 |
|
| 11 |
vector_db_path = "vectorstores/db_faiss"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# embeddings = OllamaEmbeddings(model="nomic-embed-text", show_progress=False)
|
| 14 |
-
embeddings = GPT4AllEmbeddings(model_name = "all-MiniLM-L6-v2.gguf2.f16.gguf", gpt4all_kwargs = {'allow_download': 'True'})
|
| 15 |
|
| 16 |
-
db = FAISS.load_local(vector_db_path, embeddings, allow_dangerous_deserialization=True)
|
| 17 |
|
| 18 |
-
# # Create retriever
|
| 19 |
-
retriever = db.as_retriever(
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
)
|
| 23 |
-
local_llm = 'llama3.1'
|
| 24 |
|
| 25 |
-
llm = ChatOllama(model=local_llm,
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
# Create prompt template
|
| 31 |
|
| 32 |
def respond(message, history, system_message, path_document):
|
| 33 |
-
print(message, history, system_message, path_document)
|
| 34 |
-
respon = ''
|
| 35 |
-
print("Answer:\n\n", end=" ", flush=True)
|
| 36 |
-
template = """Bạn là trợ lý ảo vì vậy bạn hãy sử dụng dữ liệu dưới đây để trả lời câu hỏi,
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
prompt = ChatPromptTemplate.from_template(template)
|
| 44 |
|
| 45 |
-
rag_chain = (
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
)
|
| 50 |
-
for chunk in rag_chain.stream(message):
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
print(chunk.content, end="", flush=True)
|
| 53 |
yield respon
|
| 54 |
-
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
respond,
|
| 57 |
additional_inputs=[
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
# from langchain_community.chat_models import ChatOllama
|
| 3 |
+
# from langchain_community.embeddings import GPT4AllEmbeddings
|
| 4 |
+
|
| 5 |
+
# from langchain.prompts import ChatPromptTemplate
|
| 6 |
+
# from langchain.schema.runnable import RunnablePassthrough
|
| 7 |
+
# from langchain_community.vectorstores import FAISS
|
| 8 |
+
from langchain_community.llms import LlamaCpp
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# import os
|
| 11 |
# os.system("ollama pull alen_ox/llama_3_fin")
|
| 12 |
|
| 13 |
vector_db_path = "vectorstores/db_faiss"
|
| 14 |
|
| 15 |
+
llm = LlamaCpp(
|
| 16 |
+
model_path="Qwen2-7B-Instruct.Q5_K_M.gguf",
|
| 17 |
+
temperature=0.75,
|
| 18 |
+
max_tokens=2000,
|
| 19 |
+
top_p=1,
|
| 20 |
+
# callback_manager=callback_manager,
|
| 21 |
+
verbose=True, # Verbose is required to pass to the callback manager
|
| 22 |
+
)
|
| 23 |
# embeddings = OllamaEmbeddings(model="nomic-embed-text", show_progress=False)
|
| 24 |
+
# embeddings = GPT4AllEmbeddings(model_name = "all-MiniLM-L6-v2.gguf2.f16.gguf", gpt4all_kwargs = {'allow_download': 'True'})
|
| 25 |
|
| 26 |
+
# db = FAISS.load_local(vector_db_path, embeddings, allow_dangerous_deserialization=True)
|
| 27 |
|
| 28 |
+
# # # Create retriever
|
| 29 |
+
# retriever = db.as_retriever(
|
| 30 |
+
# search_type="similarity",
|
| 31 |
+
# search_kwargs= {"k": 3}
|
| 32 |
+
# )
|
| 33 |
+
# local_llm = 'llama3.1'
|
| 34 |
|
| 35 |
+
# llm = ChatOllama(model=local_llm,
|
| 36 |
+
# keep_alive="3h",
|
| 37 |
+
# max_tokens=512,
|
| 38 |
+
# temperature=0)
|
| 39 |
|
| 40 |
# Create prompt template
|
| 41 |
|
| 42 |
def respond(message, history, system_message, path_document):
|
| 43 |
+
# print(message, history, system_message, path_document)
|
| 44 |
+
# respon = ''
|
| 45 |
+
# print("Answer:\n\n", end=" ", flush=True)
|
| 46 |
+
# template = """Bạn là trợ lý ảo vì vậy bạn hãy sử dụng dữ liệu dưới đây để trả lời câu hỏi,
|
| 47 |
+
# nếu không có thông tin hãy đưa ra câu trả lời sát nhất với câu hỏi từ các thông tin tìm được
|
| 48 |
+
# Content: {content}
|
| 49 |
+
# Question: {question}
|
| 50 |
+
# Chỉ đưa ra các câu trả lời hữu ích.
|
| 51 |
+
# Helpful answer:
|
| 52 |
+
# """
|
| 53 |
+
# prompt = ChatPromptTemplate.from_template(template)
|
| 54 |
|
| 55 |
+
# rag_chain = (
|
| 56 |
+
# {"content": retriever, "question": RunnablePassthrough()}
|
| 57 |
+
# | prompt
|
| 58 |
+
# | llm
|
| 59 |
+
# )
|
| 60 |
+
# for chunk in rag_chain.stream(message):
|
| 61 |
+
# respon += chunk.content
|
| 62 |
+
# print(chunk.content, end="", flush=True)
|
| 63 |
+
# yield respon
|
| 64 |
+
|
| 65 |
+
for chunk in llm.stream(message):
|
| 66 |
+
respon += chunk
|
| 67 |
print(chunk.content, end="", flush=True)
|
| 68 |
yield respon
|
|
|
|
| 69 |
demo = gr.ChatInterface(
|
| 70 |
respond,
|
| 71 |
additional_inputs=[
|