Spaces:
Build error
Build error
Update app.py
Browse files
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
|
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 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
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("
|
73 |
except Exception as e:
|
74 |
-
print(f"
|
75 |
raise
|
76 |
|
77 |
def generate_response(query):
|
78 |
try:
|
79 |
-
#
|
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
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
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 "
|
107 |
|
108 |
# Gradio interface
|
109 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
110 |
gr.Markdown("# Customer Support Chatbot")
|
111 |
-
chatbot = gr.Chatbot(
|
112 |
-
msg = gr.Textbox(label="
|
113 |
-
clear = gr.
|
114 |
|
115 |
def respond(message, history):
|
116 |
response = generate_response(message)
|
117 |
-
|
|
|
118 |
|
119 |
-
msg.submit(respond, [msg, chatbot], chatbot)
|
120 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
121 |
|
122 |
-
demo.launch(
|
|
|
|
|
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()
|