Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,93 @@
|
|
1 |
import gradio as gr
|
2 |
-
import spaces
|
3 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
@spaces.GPU
|
9 |
-
def
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
import spaces
|
4 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
5 |
+
from langchain_community.document_loaders import TextLoader
|
6 |
+
from langchain_community.vectorstores import FAISS
|
7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
+
from langchain.chains import RetrievalQA
|
9 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
10 |
+
from langchain_huggingface import HuggingFacePipeline
|
11 |
+
|
12 |
+
# Load and process the document
|
13 |
+
doc_loader = TextLoader("dataset.txt")
|
14 |
+
docs = doc_loader.load()
|
15 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
16 |
+
split_docs = text_splitter.split_documents(docs)
|
17 |
+
|
18 |
+
# Create embeddings and vector store
|
19 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
20 |
+
vectordb = FAISS.from_documents(split_docs, embeddings)
|
21 |
|
22 |
+
# Load model and tokenizer
|
23 |
+
model_name = "01-ai/Yi-Coder-9B-Chat"
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
25 |
|
26 |
@spaces.GPU
|
27 |
+
def setup_model():
|
28 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
29 |
+
model = AutoModelForCausalLM.from_pretrained(
|
30 |
+
model_name,
|
31 |
+
device_map="auto",
|
32 |
+
torch_dtype=torch.float16 if device.type == "cuda" else torch.float32
|
33 |
+
)
|
34 |
+
return model, device
|
35 |
+
|
36 |
+
model, device = setup_model()
|
37 |
+
|
38 |
+
# Set up the QA pipeline
|
39 |
+
qa_pipeline = pipeline(
|
40 |
+
"text-generation",
|
41 |
+
model=model,
|
42 |
+
tokenizer=tokenizer,
|
43 |
+
max_new_tokens=750,
|
44 |
+
pad_token_id=tokenizer.eos_token_id,
|
45 |
+
device=device
|
46 |
+
)
|
47 |
+
|
48 |
+
llm = HuggingFacePipeline(pipeline=qa_pipeline)
|
49 |
+
|
50 |
+
# Set up the retriever and QA chain
|
51 |
+
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
52 |
+
qa_chain = RetrievalQA.from_chain_type(
|
53 |
+
retriever=retriever,
|
54 |
+
chain_type="stuff",
|
55 |
+
llm=llm,
|
56 |
+
return_source_documents=False
|
57 |
+
)
|
58 |
+
|
59 |
+
def preprocess_query(query):
|
60 |
+
if "script" in query or "code" in query.lower():
|
61 |
+
return f"Write a CPSL script: {query}"
|
62 |
+
return query
|
63 |
+
|
64 |
+
def clean_response(response):
|
65 |
+
result = response.get("result", "")
|
66 |
+
if "Answer:" in result:
|
67 |
+
return result.split("Answer:")[1].strip()
|
68 |
+
return result.strip()
|
69 |
+
|
70 |
+
@spaces.GPU
|
71 |
+
def chatbot_response(user_input):
|
72 |
+
processed_query = preprocess_query(user_input)
|
73 |
+
raw_response = qa_chain.invoke({"query": processed_query})
|
74 |
+
return clean_response(raw_response)
|
75 |
+
|
76 |
+
# Gradio interface
|
77 |
+
with gr.Blocks() as chat_interface:
|
78 |
+
gr.Markdown("# CPSL Chatbot")
|
79 |
+
chat_history = gr.Chatbot(type='messages')
|
80 |
+
user_input = gr.Textbox(label="Your Message:")
|
81 |
+
send_button = gr.Button("Send")
|
82 |
+
|
83 |
+
def interact(user_message, history):
|
84 |
+
bot_reply = chatbot_response(user_message)
|
85 |
+
history.append({"role": "user", "content": user_message})
|
86 |
+
history.append({"role": "assistant", "content": bot_reply})
|
87 |
+
return history, history
|
88 |
+
|
89 |
+
send_button.click(interact, inputs=[user_input, chat_history], outputs=[chat_history, chat_history])
|
90 |
|
91 |
+
# Launch the interface
|
92 |
+
if __name__ == "__main__":
|
93 |
+
chat_interface.launch()
|