random2222 commited on
Commit
1051bf9
·
verified ·
1 Parent(s): 2c2ba82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -53
app.py CHANGED
@@ -1,48 +1,56 @@
1
- import gradio as gr
2
  import os
3
- from langchain.document_loaders import PyPDFLoader
 
 
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
  from langchain_community.embeddings import HuggingFaceEmbeddings
6
- from langchain.vectorstores import FAISS
7
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
 
9
  # Configuration
10
- DOCS_DIR = "business_docs"
11
  EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
12
- MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.1"
13
 
14
- # Initialize components once at startup
15
  def initialize_system():
16
- # Load and process PDFs from business_docs folder
17
  if not os.path.exists(DOCS_DIR):
18
  raise FileNotFoundError(f"Business documents folder '{DOCS_DIR}' not found")
19
-
 
20
  pdf_files = [os.path.join(DOCS_DIR, f) for f in os.listdir(DOCS_DIR) if f.endswith(".pdf")]
21
  if not pdf_files:
22
  raise ValueError(f"No PDF files found in {DOCS_DIR} folder")
23
 
24
- # Process documents
25
  text_splitter = RecursiveCharacterTextSplitter(
26
  chunk_size=1000,
27
  chunk_overlap=200
28
  )
29
 
30
- texts = []
31
- for pdf in pdf_files:
32
- loader = PyPDFLoader(pdf)
33
- pages = loader.load_and_split(text_splitter)
34
- texts.extend(pages)
35
-
36
- # Create vector store
37
  embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
38
- vector_store = FAISS.from_documents(texts, embeddings)
 
 
 
 
 
 
 
39
 
40
- # Load model with quantization for faster inference
41
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
42
  model = AutoModelForCausalLM.from_pretrained(
43
  MODEL_NAME,
 
44
  device_map="auto",
45
- load_in_8bit=True
46
  )
47
 
48
  return vector_store, model, tokenizer
@@ -50,52 +58,66 @@ def initialize_system():
50
  # Initialize system components
51
  try:
52
  vector_store, model, tokenizer = initialize_system()
53
- print("System initialized successfully with business documents")
54
  except Exception as e:
55
- print(f"Initialization error: {str(e)}")
56
  raise
57
 
58
- # Response generation with context
59
  def generate_response(query):
60
- # Retrieve relevant context
61
- docs = vector_store.similarity_search(query, k=3)
62
- context = "\n".join([doc.page_content for doc in docs])
63
-
64
- # Create instruction prompt
65
- prompt = f"""<s>[INST] You are a customer support agent.
66
- Answer ONLY using information from the provided business documents.
67
- If unsure, say "I don't have information about that."
68
-
69
- Context: {context}
70
- Question: {query} [/INST]"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- # Generate response
73
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
74
- outputs = model.generate(
75
- **inputs,
76
- max_new_tokens=500,
77
- temperature=0.3,
78
- do_sample=True
79
- )
80
- return tokenizer.decode(outputs[0], skip_special_tokens=True).split("[/INST]")[-1].strip()
81
 
82
- # Chat interface
83
- with gr.Blocks() as demo:
84
- gr.Markdown("## Business Support Chatbot\nAsk questions about our services!")
 
 
 
 
 
85
 
86
- chatbot = gr.Chatbot(label="Conversation")
87
- msg = gr.Textbox(label="Type your question")
88
  clear = gr.Button("Clear History")
89
 
90
  def respond(message, chat_history):
91
- try:
92
- response = generate_response(message)
93
- except Exception as e:
94
- response = "Sorry, I'm having trouble answering right now. Please try again later."
95
  chat_history.append((message, response))
96
  return "", chat_history
97
 
98
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
99
  clear.click(lambda: None, None, chatbot, queue=False)
100
 
101
- demo.launch()
 
 
 
1
  import os
2
+ import gradio as gr
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
7
+ from langchain_community.vectorstores import FAISS
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
9
 
10
  # Configuration
11
+ DOCS_DIR = ".business_docs"
12
  EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
13
+ MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
14
 
15
+ # System Initialization
16
  def initialize_system():
17
+ # Validate documents folder
18
  if not os.path.exists(DOCS_DIR):
19
  raise FileNotFoundError(f"Business documents folder '{DOCS_DIR}' not found")
20
+
21
+ # Load and process PDFs
22
  pdf_files = [os.path.join(DOCS_DIR, f) for f in os.listdir(DOCS_DIR) if f.endswith(".pdf")]
23
  if not pdf_files:
24
  raise ValueError(f"No PDF files found in {DOCS_DIR} folder")
25
 
 
26
  text_splitter = RecursiveCharacterTextSplitter(
27
  chunk_size=1000,
28
  chunk_overlap=200
29
  )
30
 
31
+ documents = []
32
+ for pdf_path in pdf_files:
33
+ loader = PyPDFLoader(pdf_path)
34
+ documents.extend(loader.load_and_split(text_splitter))
35
+
36
+ # Create embeddings
 
37
  embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
38
+ vector_store = FAISS.from_documents(documents, embeddings)
39
+
40
+ # Quantization config
41
+ bnb_config = BitsAndBytesConfig(
42
+ load_in_4bit=True,
43
+ bnb_4bit_quant_type="nf4",
44
+ bnb_4bit_compute_dtype=torch.float16,
45
+ )
46
 
47
+ # Load model and tokenizer
48
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
49
  model = AutoModelForCausalLM.from_pretrained(
50
  MODEL_NAME,
51
+ quantization_config=bnb_config,
52
  device_map="auto",
53
+ trust_remote_code=True
54
  )
55
 
56
  return vector_store, model, tokenizer
 
58
  # Initialize system components
59
  try:
60
  vector_store, model, tokenizer = initialize_system()
61
+ print("System initialized with business documents")
62
  except Exception as e:
63
+ print(f"Initialization failed: {str(e)}")
64
  raise
65
 
66
+ # Response Generation
67
  def generate_response(query):
68
+ try:
69
+ # Retrieve relevant context
70
+ docs = vector_store.similarity_search(query, k=3)
71
+ context = "\n".join([doc.page_content for doc in docs])
72
+
73
+ # Create formatted prompt
74
+ prompt = f"""<|system|>
75
+ You are a customer support assistant. Answer ONLY using the provided business documents.
76
+ If the answer isn't in the documents, respond: "I don't have that information."
77
+
78
+ Context: {context}</s>
79
+ <|user|>
80
+ {query}</s>
81
+ <|assistant|>
82
+ """
83
+
84
+ # Generate response
85
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
86
+ outputs = model.generate(
87
+ inputs.input_ids,
88
+ max_new_tokens=512,
89
+ temperature=0.3,
90
+ do_sample=True,
91
+ pad_token_id=tokenizer.eos_token_id
92
+ )
93
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
94
+
95
+ # Extract only the assistant's response
96
+ return response.split("<|assistant|>")[-1].strip()
97
 
98
+ except Exception as e:
99
+ return f"⚠️ Error: {str(e)}"
 
 
 
 
 
 
 
100
 
101
+ # Chat Interface
102
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
103
+ gr.Markdown("# 📚 Business Document Assistant")
104
+
105
+ with gr.Row():
106
+ gr.Image("https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.png",
107
+ width=100)
108
+ gr.Markdown("Ask questions about our policies, products, and services!")
109
 
110
+ chatbot = gr.Chatbot(height=400)
111
+ msg = gr.Textbox(label="Your Question", placeholder="Type your question here...")
112
  clear = gr.Button("Clear History")
113
 
114
  def respond(message, chat_history):
115
+ response = generate_response(message)
 
 
 
116
  chat_history.append((message, response))
117
  return "", chat_history
118
 
119
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
120
  clear.click(lambda: None, None, chatbot, queue=False)
121
 
122
+ if __name__ == "__main__":
123
+ demo.launch(server_name="0.0.0.0", server_port=7860)