Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
4 |
-
from langchain_community.document_loaders import TextLoader
|
5 |
-
from langchain_community.vectorstores import FAISS
|
6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
|
|
|
7 |
from langchain.chains import RetrievalQA
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
9 |
-
from langchain_huggingface import HuggingFacePipeline
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
# Load and process the document
|
14 |
doc_loader = TextLoader("dataset.txt")
|
15 |
docs = doc_loader.load()
|
16 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
17 |
split_docs = text_splitter.split_documents(docs)
|
18 |
|
19 |
-
# Create
|
20 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
21 |
vectordb = FAISS.from_documents(split_docs, embeddings)
|
22 |
|
23 |
-
# Load model and
|
24 |
model_name = "01-ai/Yi-Coder-9B-Chat"
|
25 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
26 |
-
model = AutoModelForCausalLM.from_pretrained(
|
27 |
-
model_name,
|
28 |
-
device_map="auto",
|
29 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
30 |
-
)
|
31 |
-
|
32 |
-
# Set up the QA pipeline
|
33 |
qa_pipeline = pipeline(
|
34 |
"text-generation",
|
35 |
model=model,
|
36 |
tokenizer=tokenizer,
|
37 |
-
max_new_tokens=
|
38 |
pad_token_id=tokenizer.eos_token_id
|
39 |
)
|
40 |
|
|
|
41 |
llm = HuggingFacePipeline(pipeline=qa_pipeline)
|
42 |
-
|
43 |
-
# Set up the retriever and QA chain
|
44 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
45 |
qa_chain = RetrievalQA.from_chain_type(
|
46 |
retriever=retriever,
|
@@ -66,20 +56,17 @@ def chatbot_response(user_input):
|
|
66 |
return clean_response(raw_response)
|
67 |
|
68 |
# Gradio interface
|
69 |
-
with gr.Blocks() as
|
70 |
gr.Markdown("# CPSL Chatbot")
|
71 |
-
chat_history = gr.Chatbot(
|
72 |
user_input = gr.Textbox(label="Your Message:")
|
73 |
send_button = gr.Button("Send")
|
74 |
|
75 |
def interact(user_message, history):
|
76 |
bot_reply = chatbot_response(user_message)
|
77 |
-
history.append(
|
78 |
-
history.append({"role": "assistant", "content": bot_reply})
|
79 |
return history, history
|
80 |
|
81 |
send_button.click(interact, inputs=[user_input, chat_history], outputs=[chat_history, chat_history])
|
82 |
|
83 |
-
#
|
84 |
-
if __name__ == "__main__":
|
85 |
-
chat_interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain.document_loaders import TextLoader
|
|
|
|
|
|
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain.vectorstores import FAISS
|
6 |
+
from langchain.llms import HuggingFacePipeline
|
7 |
from langchain.chains import RetrievalQA
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
9 |
|
10 |
+
# Load and process documents
|
|
|
|
|
11 |
doc_loader = TextLoader("dataset.txt")
|
12 |
docs = doc_loader.load()
|
13 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
14 |
split_docs = text_splitter.split_documents(docs)
|
15 |
|
16 |
+
# Create vector database
|
17 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
18 |
vectordb = FAISS.from_documents(split_docs, embeddings)
|
19 |
|
20 |
+
# Load model and create pipeline
|
21 |
model_name = "01-ai/Yi-Coder-9B-Chat"
|
22 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
23 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto")
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
qa_pipeline = pipeline(
|
25 |
"text-generation",
|
26 |
model=model,
|
27 |
tokenizer=tokenizer,
|
28 |
+
max_new_tokens=500,
|
29 |
pad_token_id=tokenizer.eos_token_id
|
30 |
)
|
31 |
|
32 |
+
# Set up LangChain
|
33 |
llm = HuggingFacePipeline(pipeline=qa_pipeline)
|
|
|
|
|
34 |
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
35 |
qa_chain = RetrievalQA.from_chain_type(
|
36 |
retriever=retriever,
|
|
|
56 |
return clean_response(raw_response)
|
57 |
|
58 |
# Gradio interface
|
59 |
+
with gr.Blocks() as demo:
|
60 |
gr.Markdown("# CPSL Chatbot")
|
61 |
+
chat_history = gr.Chatbot()
|
62 |
user_input = gr.Textbox(label="Your Message:")
|
63 |
send_button = gr.Button("Send")
|
64 |
|
65 |
def interact(user_message, history):
|
66 |
bot_reply = chatbot_response(user_message)
|
67 |
+
history.append((user_message, bot_reply))
|
|
|
68 |
return history, history
|
69 |
|
70 |
send_button.click(interact, inputs=[user_input, chat_history], outputs=[chat_history, chat_history])
|
71 |
|
72 |
+
# Note: No launch() call here. Hugging Face will handle this.
|
|
|
|