AlirezaHSZ commited on
Commit
2992c06
·
verified ·
1 Parent(s): 75effad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -8,12 +8,12 @@ from langchain.embeddings import HuggingFaceEmbeddings
8
  from transformers import AutoTokenizer, AutoModelForCausalLM
9
  import torch
10
 
11
- # ========== Configuration ==========
12
- EMBEDDING_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2" # Embedding model
13
- LLM_MODEL_NAME = "deepseek-ai/deepseek-llm-67b-chat" # Replace with a valid model name
14
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
15
 
16
- # ========== Text Splitting ==========
17
  def get_pdf_text(pdf_docs):
18
  text = ""
19
  for pdf in pdf_docs:
@@ -26,7 +26,7 @@ def get_text_chunks(text):
26
  splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
27
  return splitter.split_text(text)
28
 
29
- # ========== Vector Store ==========
30
  def get_vector_store(chunks):
31
  try:
32
  embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
@@ -35,15 +35,16 @@ def get_vector_store(chunks):
35
  except Exception as e:
36
  raise RuntimeError(f"Error processing embeddings: {e}")
37
 
38
- # ========== Response Generation ==========
39
  class DeepSeekChat:
40
  def __init__(self):
41
  try:
42
- self.tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL_NAME)
43
  self.model = AutoModelForCausalLM.from_pretrained(
44
  LLM_MODEL_NAME,
45
  torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
46
- low_cpu_mem_usage=True
 
47
  ).to(DEVICE)
48
  except Exception as e:
49
  raise RuntimeError(f"Error loading DeepSeek model: {e}")
@@ -76,14 +77,13 @@ class DeepSeekChat:
76
  except Exception as e:
77
  raise RuntimeError(f"Error generating response: {e}")
78
 
79
- # ========== Streamlit App ==========
80
  def clear_chat_history():
81
  st.session_state.messages = [{"role": "assistant", "content": "در خدمتیم"}]
82
 
83
  def main():
84
  st.set_page_config(page_title="Chatbot", layout="wide", initial_sidebar_state="expanded")
85
 
86
- # Custom CSS
87
  st.markdown("""
88
  <style>
89
  body { background-color: #000000; color: #ffffff; }
@@ -127,21 +127,18 @@ def main():
127
  if st.session_state.messages[-1]["role"] != "assistant":
128
  with st.spinner("در حال پاسخگویی..."):
129
  try:
130
- # Load embeddings and vector store
131
  embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
132
  db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
133
  docs = db.similarity_search(prompt, k=4)
134
- print("Retrieved Documents:", docs) # Debug: Print the retrieved documents
135
 
136
  context = "\n".join([d.page_content for d in docs])
137
- print("Context:", context) # Debug: Print the context passed to the model
138
 
139
- # Generate response
140
  chat_model = DeepSeekChat()
141
  response = chat_model.generate(context, prompt)
142
- print("Generated Response:", response) # Debug: Print the generated response
143
 
144
- # Display response
145
  with st.chat_message("assistant"):
146
  st.write(response)
147
  st.session_state.messages.append({"role": "assistant", "content": response})
@@ -149,4 +146,4 @@ def main():
149
  st.error(f"خطا: {str(e)}")
150
 
151
  if __name__ == "__main__":
152
- main()
 
8
  from transformers import AutoTokenizer, AutoModelForCausalLM
9
  import torch
10
 
11
+ # Configuration
12
+ EMBEDDING_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
13
+ LLM_MODEL_NAME = "deepseek-ai/DeepSeek-V2-Lite-Chat"
14
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
15
 
16
+ # Text Splitting
17
  def get_pdf_text(pdf_docs):
18
  text = ""
19
  for pdf in pdf_docs:
 
26
  splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
27
  return splitter.split_text(text)
28
 
29
+ # Vector Store
30
  def get_vector_store(chunks):
31
  try:
32
  embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
 
35
  except Exception as e:
36
  raise RuntimeError(f"Error processing embeddings: {e}")
37
 
38
+ # Response Generation
39
  class DeepSeekChat:
40
  def __init__(self):
41
  try:
42
+ self.tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL_NAME, cache_dir="./cache")
43
  self.model = AutoModelForCausalLM.from_pretrained(
44
  LLM_MODEL_NAME,
45
  torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
46
+ low_cpu_mem_usage=True,
47
+ cache_dir="./cache"
48
  ).to(DEVICE)
49
  except Exception as e:
50
  raise RuntimeError(f"Error loading DeepSeek model: {e}")
 
77
  except Exception as e:
78
  raise RuntimeError(f"Error generating response: {e}")
79
 
80
+ # Streamlit App
81
  def clear_chat_history():
82
  st.session_state.messages = [{"role": "assistant", "content": "در خدمتیم"}]
83
 
84
  def main():
85
  st.set_page_config(page_title="Chatbot", layout="wide", initial_sidebar_state="expanded")
86
 
 
87
  st.markdown("""
88
  <style>
89
  body { background-color: #000000; color: #ffffff; }
 
127
  if st.session_state.messages[-1]["role"] != "assistant":
128
  with st.spinner("در حال پاسخگویی..."):
129
  try:
 
130
  embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
131
  db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
132
  docs = db.similarity_search(prompt, k=4)
133
+ print("Retrieved Documents:", docs)
134
 
135
  context = "\n".join([d.page_content for d in docs])
136
+ print("Context:", context)
137
 
 
138
  chat_model = DeepSeekChat()
139
  response = chat_model.generate(context, prompt)
140
+ print("Generated Response:", response)
141
 
 
142
  with st.chat_message("assistant"):
143
  st.write(response)
144
  st.session_state.messages.append({"role": "assistant", "content": response})
 
146
  st.error(f"خطا: {str(e)}")
147
 
148
  if __name__ == "__main__":
149
+ main()