random2222 commited on
Commit
8550dc5
·
verified ·
1 Parent(s): bd75b14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -71
app.py CHANGED
@@ -1,7 +1,6 @@
1
- # Updated app.py with torch import and error handling
2
  import gradio as gr
3
  import os
4
- import torch # Missing import added here
5
  from langchain_community.document_loaders import PyPDFLoader
6
  from langchain.text_splitter import RecursiveCharacterTextSplitter
7
  from langchain_community.embeddings import HuggingFaceEmbeddings
@@ -14,78 +13,75 @@ EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
14
  MODEL_NAME = "microsoft/phi-2"
15
 
16
  def initialize_system():
17
- try:
18
- # Verify documents
19
- if not os.path.exists(DOCS_DIR):
20
- raise FileNotFoundError(f"Missing {DOCS_DIR} folder")
21
-
22
- pdf_files = [os.path.join(DOCS_DIR, f)
23
- for f in os.listdir(DOCS_DIR)
24
- if f.endswith(".pdf")]
25
- if not pdf_files:
26
- raise ValueError(f"No PDFs found in {DOCS_DIR}")
27
-
28
- # Process documents
29
- text_splitter = RecursiveCharacterTextSplitter(
30
- chunk_size=800,
31
- chunk_overlap=100
32
- )
33
-
34
- texts = []
35
- for pdf in pdf_files:
36
- loader = PyPDFLoader(pdf)
37
- pages = loader.load_and_split(text_splitter)
38
- texts.extend(pages)
39
-
40
- # Create embeddings
41
- embeddings = HuggingFaceEmbeddings(
42
- model_name=EMBEDDING_MODEL,
43
- model_kwargs={'device': 'cpu'},
44
- encode_kwargs={'normalize_embeddings': False}
45
- )
46
-
47
- # Create vector store
48
- vector_store = FAISS.from_documents(texts, embeddings)
49
-
50
- # Load Phi-2 model
51
- tokenizer = AutoTokenizer.from_pretrained(
52
- MODEL_NAME,
53
- trust_remote_code=True,
54
- padding_side="left"
55
- )
56
-
57
- model = AutoModelForCausalLM.from_pretrained(
58
- MODEL_NAME,
59
- trust_remote_code=True,
60
- device_map="auto",
61
- load_in_4bit=True,
62
- torch_dtype=torch.float16
63
- )
64
-
65
- return vector_store, model, tokenizer
66
-
67
- except Exception as e:
68
- raise RuntimeError(f"Initialization failed: {str(e)}")
69
 
70
  try:
71
  vector_store, model, tokenizer = initialize_system()
72
- print("System initialized successfully")
73
  except Exception as e:
74
- print(f"Initialization error: {str(e)}")
75
  raise
76
 
77
  def generate_response(query):
78
  try:
79
- # Retrieve context
80
  docs = vector_store.similarity_search(query, k=2)
81
  context = "\n".join([d.page_content for d in docs])
82
 
83
  # Phi-2 optimized prompt
84
  prompt = f"""<|system|>
85
- You are a customer service assistant. Answer ONLY using the context below.
86
- Keep responses under 3 sentences. If unsure, say "I'll check with the team".
87
-
88
- Context: {context}</s>
 
89
  <|user|>
90
  {query}</s>
91
  <|assistant|>"""
@@ -95,28 +91,27 @@ def generate_response(query):
95
  **inputs,
96
  max_new_tokens=200,
97
  temperature=0.1,
98
- do_sample=True,
99
  pad_token_id=tokenizer.eos_token_id
100
  )
101
 
102
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
103
  return response.split("<|assistant|>")[-1].strip()
104
-
105
  except Exception as e:
106
- return "I'm having trouble answering that. Please try again later."
107
 
108
  # Gradio interface
109
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
110
  gr.Markdown("# Customer Support Chatbot")
111
- chatbot = gr.Chatbot(height=400)
112
- msg = gr.Textbox(label="Your question", placeholder="Type here...")
113
- clear = gr.Button("Clear History")
114
 
115
  def respond(message, history):
116
  response = generate_response(message)
117
- return response
 
118
 
119
- msg.submit(respond, [msg, chatbot], chatbot)
120
- clear.click(lambda: None, None, chatbot, queue=False)
121
 
122
- demo.launch(server_port=7860)
 
 
1
  import gradio as gr
2
  import os
3
+ import torch
4
  from langchain_community.document_loaders import PyPDFLoader
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
6
  from langchain_community.embeddings import HuggingFaceEmbeddings
 
13
  MODEL_NAME = "microsoft/phi-2"
14
 
15
  def initialize_system():
16
+ # Document verification
17
+ if not os.path.exists(DOCS_DIR):
18
+ raise FileNotFoundError(f"Missing {DOCS_DIR} folder")
19
+
20
+ pdf_files = [os.path.join(DOCS_DIR, f)
21
+ for f in os.listdir(DOCS_DIR)
22
+ if f.endswith(".pdf")]
23
+
24
+ if not pdf_files:
25
+ raise ValueError(f"No PDFs found in {DOCS_DIR}")
26
+
27
+ # Document processing
28
+ text_splitter = RecursiveCharacterTextSplitter(
29
+ chunk_size=800,
30
+ chunk_overlap=100
31
+ )
32
+
33
+ texts = []
34
+ for pdf in pdf_files:
35
+ loader = PyPDFLoader(pdf)
36
+ pages = loader.load_and_split(text_splitter)
37
+ texts.extend(pages)
38
+
39
+ # Create embeddings
40
+ embeddings = HuggingFaceEmbeddings(
41
+ model_name=EMBEDDING_MODEL,
42
+ model_kwargs={'device': 'cpu'}
43
+ )
44
+
45
+ # Vector store
46
+ vector_store = FAISS.from_documents(texts, embeddings)
47
+
48
+ # Model loading
49
+ tokenizer = AutoTokenizer.from_pretrained(
50
+ MODEL_NAME,
51
+ trust_remote_code=True,
52
+ padding_side="left"
53
+ )
54
+
55
+ model = AutoModelForCausalLM.from_pretrained(
56
+ MODEL_NAME,
57
+ trust_remote_code=True,
58
+ device_map="auto",
59
+ load_in_4bit=True,
60
+ torch_dtype=torch.float16
61
+ )
62
+
63
+ return vector_store, model, tokenizer
 
 
 
 
64
 
65
  try:
66
  vector_store, model, tokenizer = initialize_system()
67
+ print("System initialized successfully")
68
  except Exception as e:
69
+ print(f"Initialization failed ❌: {str(e)}")
70
  raise
71
 
72
  def generate_response(query):
73
  try:
74
+ # Context retrieval
75
  docs = vector_store.similarity_search(query, k=2)
76
  context = "\n".join([d.page_content for d in docs])
77
 
78
  # Phi-2 optimized prompt
79
  prompt = f"""<|system|>
80
+ You are a customer service bot. Answer only using:
81
+ {context}
82
+ - Max 3 sentences
83
+ - If unsure: "I'll check with the team"
84
+ </s>
85
  <|user|>
86
  {query}</s>
87
  <|assistant|>"""
 
91
  **inputs,
92
  max_new_tokens=200,
93
  temperature=0.1,
 
94
  pad_token_id=tokenizer.eos_token_id
95
  )
96
 
97
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
98
  return response.split("<|assistant|>")[-1].strip()
99
+
100
  except Exception as e:
101
+ return "Please try again later."
102
 
103
  # Gradio interface
104
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
105
  gr.Markdown("# Customer Support Chatbot")
106
+ chatbot = gr.Chatbot()
107
+ msg = gr.Textbox(label="Ask about our services")
108
+ clear = gr.ClearButton([msg, chatbot])
109
 
110
  def respond(message, history):
111
  response = generate_response(message)
112
+ history.append((message, response))
113
+ return "", history
114
 
115
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
116
 
117
+ demo.launch()