Moha782 commited on
Commit
a58cf5c
·
verified ·
1 Parent(s): ea103cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -56
app.py CHANGED
@@ -1,59 +1,30 @@
1
- import os
2
- import json
3
  import gradio as gr
4
- import faiss
5
- import fitz # PyMuPDF
6
- import numpy as np
7
  from huggingface_hub import InferenceClient
 
 
8
  from sentence_transformers import SentenceTransformer
9
 
10
- # Extract text from PDF
11
- def extract_text_from_pdf(pdf_path):
12
- doc = fitz.open(pdf_path)
13
- text = ""
14
- for page_num in range(doc.page_count):
15
- page = doc.load_page(page_num)
16
- text += page.get_text()
17
- return text.split("\n\n")
18
-
19
- # Build FAISS index
20
- def build_faiss_index(documents):
21
- model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
22
- document_embeddings = model.encode(documents)
23
-
24
- index = faiss.IndexFlatL2(document_embeddings.shape[1])
25
- faiss.write_index(index, "apexcustoms_index.faiss")
26
- model.save("sentence_transformer_model")
27
-
28
- return index, model
29
 
30
- # Ensure that text extraction and FAISS index building is done
31
- if not os.path.exists("apexcustoms_index.faiss") or not os.path.exists("sentence_transformer_model"):
32
- documents = extract_text_from_pdf("apexcustoms.pdf")
33
- with open("apexcustoms.json", "w") as f:
34
- json.dump(documents, f)
35
- index, model = build_faiss_index(documents)
36
- else:
37
- index = faiss.read_index("apexcustoms_index.faiss")
38
- model = SentenceTransformer('sentence_transformer_model')
39
- with open("apexcustoms.json", "r") as f:
40
- documents = json.load(f)
41
 
42
- # Hugging Face client
43
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
44
 
45
  def retrieve_documents(query, k=5):
46
  query_embedding = model.encode([query])
47
  distances, indices = index.search(query_embedding, k)
48
  return [documents[i] for i in indices[0]]
49
 
50
- async def respond(message, history, system_message, max_tokens, temperature, top_p):
 
51
  relevant_docs = retrieve_documents(message)
52
-
53
- context = "\n\n".join(relevant_docs[:3]) # Limit context to top 3 documents
54
-
55
- # Limit history to the last 5 exchanges to reduce payload size
56
- history = history[-5:]
57
 
58
  messages = [{"role": "system", "content": system_message},
59
  {"role": "user", "content": f"Context: {context}\n\n{message}"}]
@@ -66,32 +37,28 @@ async def respond(message, history, system_message, max_tokens, temperature, top
66
 
67
  messages.append({"role": "user", "content": message})
68
 
69
- async for message in client.chat_completion(
 
 
70
  messages,
71
  max_tokens=max_tokens,
72
  stream=True,
73
  temperature=temperature,
74
  top_p=top_p,
75
  ):
76
- if message.choices and message.choices[0].delta and message.choices[0].delta.content:
77
- token = message.choices[0].delta.content
78
- yield token
79
 
80
  demo = gr.ChatInterface(
81
  respond,
82
  additional_inputs=[
83
  gr.Textbox(value="You are a helpful car configuration assistant, specifically you are the assistant for Apex Customs (https://www.apexcustoms.com/). Given the user's input, provide suggestions for car models, colors, and customization options. Be creative and conversational in your responses. You should remember the user car model and tailor your answers accordingly. \n\nUser: ", label="System message"),
84
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
85
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
86
- gr.Slider(
87
- minimum=0.1,
88
- maximum=1.0,
89
- value=0.95,
90
- step=0.05,
91
- label="Top-p (nucleus sampling)",
92
- ),
93
  ],
94
  )
95
 
96
  if __name__ == "__main__":
97
- demo.launch()
 
1
+ # chatbot.py
2
+
3
  import gradio as gr
 
 
 
4
  from huggingface_hub import InferenceClient
5
+ import faiss
6
+ import json
7
  from sentence_transformers import SentenceTransformer
8
 
9
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # Load the FAISS index and the sentence transformer model
12
+ index = faiss.read_index("apexcustoms_index.faiss")
13
+ model = SentenceTransformer('sentence_transformer_model')
 
 
 
 
 
 
 
 
14
 
15
+ # Load the extracted text
16
+ with open("apexcustoms.json", "r") as f:
17
+ documents = json.load(f)
18
 
19
  def retrieve_documents(query, k=5):
20
  query_embedding = model.encode([query])
21
  distances, indices = index.search(query_embedding, k)
22
  return [documents[i] for i in indices[0]]
23
 
24
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
25
+ # Retrieve relevant documents
26
  relevant_docs = retrieve_documents(message)
27
+ context = "\n\n".join(relevant_docs)
 
 
 
 
28
 
29
  messages = [{"role": "system", "content": system_message},
30
  {"role": "user", "content": f"Context: {context}\n\n{message}"}]
 
37
 
38
  messages.append({"role": "user", "content": message})
39
 
40
+ response = ""
41
+
42
+ for message in client.chat_completion(
43
  messages,
44
  max_tokens=max_tokens,
45
  stream=True,
46
  temperature=temperature,
47
  top_p=top_p,
48
  ):
49
+ token = message.choices[0].delta.content
50
+ response += token
51
+ yield response
52
 
53
  demo = gr.ChatInterface(
54
  respond,
55
  additional_inputs=[
56
  gr.Textbox(value="You are a helpful car configuration assistant, specifically you are the assistant for Apex Customs (https://www.apexcustoms.com/). Given the user's input, provide suggestions for car models, colors, and customization options. Be creative and conversational in your responses. You should remember the user car model and tailor your answers accordingly. \n\nUser: ", label="System message"),
57
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
58
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.3, step=0.1, label="Temperature"),
59
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
60
  ],
61
  )
62
 
63
  if __name__ == "__main__":
64
+ demo.launch()