Moha782 commited on
Commit
95c09d8
1 Parent(s): cd79bad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -48
app.py CHANGED
@@ -1,56 +1,32 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- from pathlib import Path
4
- from transformers import RagTokenForGeneration, RagTokenizer
5
- import faiss
6
- from typing import List
7
- from pdfplumber import open as open_pdf
8
 
9
  """
10
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
11
  """
12
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
13
 
14
- # Load the PDF file
15
- pdf_path = Path("apexcustoms.pdf")
16
- with open_pdf(pdf_path) as pdf:
17
- text = "\n".join(page.extract_text() for page in pdf.pages)
18
-
19
- # Split the PDF text into chunks
20
- chunk_size = 1000 # Adjust this value based on your needs
21
- text_chunks: List[str] = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
22
-
23
- # Load the RAG model and tokenizer for retrieval
24
- rag_tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
25
- rag_model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq")
26
-
27
- # Create an in-memory index using the text chunks
28
- embeddings = rag_model.question_encoder(rag_tokenizer(text_chunks, padding=True, return_tensors="pt")["input_ids"])
29
- index = faiss.IndexFlatL2(embeddings.size(-1))
30
- index.add(embeddings.detach().numpy())
31
-
32
- # Custom retriever class
33
- class CustomRetriever:
34
- def __init__(self, documents, embeddings, index):
35
- self.documents = documents
36
- self.embeddings = embeddings
37
- self.index = index
38
-
39
- def get_relevant_docs(self, query_embeddings, top_k=4):
40
- scores, doc_indices = self.index.search(query_embeddings.detach().numpy(), top_k)
41
- return [(self.documents[doc_idx], score) for doc_idx, score in zip(doc_indices[0], scores[0])]
42
-
43
- # Create a custom retriever instance
44
- retriever = CustomRetriever(text_chunks, embeddings, index)
45
 
46
  def respond(
47
  message,
48
  history: list[tuple[str, str]],
49
  system_message,
50
  max_tokens,
 
 
51
  ):
52
  messages = [{"role": "system", "content": system_message}]
53
 
 
 
 
 
 
 
 
 
 
54
  for val in history:
55
  if val[0]:
56
  messages.append({"role": "user", "content": val[0]})
@@ -61,33 +37,41 @@ def respond(
61
 
62
  response = ""
63
 
64
- # Retrieve relevant chunks using the custom retriever
65
- rag_input_ids = rag_tokenizer(message, return_tensors="pt").input_ids
66
- query_embeddings = rag_model.question_encoder(rag_input_ids)
67
- relevant_docs = retriever.get_relevant_docs(query_embeddings)
68
- retrieved_text = "\n".join([doc for doc, _ in relevant_docs])
69
-
70
- # Generate the response using the zephyr model
71
  for message in client.chat_completion(
72
  messages,
73
  max_tokens=max_tokens,
74
  stream=True,
75
- files={"context": retrieved_text}, # Pass retrieved text as context
 
76
  ):
77
  token = message.choices[0].delta.content
 
78
  response += token
79
  yield response
80
 
81
  """
82
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
83
  """
 
 
 
84
  demo = gr.ChatInterface(
85
- respond,
 
86
  additional_inputs=[
87
- 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 conversational in your responses. You should remember the user car model and tailor your answers accordingly. You limit yourself to answering the given question and maybe propose a suggestion but not write the next question of the user. \n\nUser: ", label="System message"),
88
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
 
 
 
 
 
 
 
89
  ],
90
  )
91
 
 
92
  if __name__ == "__main__":
93
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
3
 
4
  """
5
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
7
+ client = InferenceClient("stabilityai/stablelm-zephyr-3b")
8
 
9
+ initial_message = "Hello! I'm the Apex Customs car configuration assistant. Welcome to Apex Customs, where we specialize in modifying and customizing some of the most popular sports cars and performance vehicles on the market. Our team of experts can help you take your ride to the next level with a wide range of customization options.\n\nWhen it comes to customization options, we offer everything from exterior modifications like body kits and spoilers to interior upgrades like seats and steering wheels. We can also help you enhance your car's performance with engine tuning, exhaust systems, and suspension upgrades. And if you're looking to upgrade your audio and entertainment system, we've got you covered there too.\n\nAs for paint colors, we offer a wide range of options to choose from. Some popular choices include matte black, Nardo gray, midnight purple, riviera blue, and fluorescent green.\n\nLet us know which car model you're interested in, and we can provide more detailed information about the customization options available for that specific vehicle. Thanks for considering Apex Customs for your next car modification project!\n\nHow can I help you today?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def respond(
12
  message,
13
  history: list[tuple[str, str]],
14
  system_message,
15
  max_tokens,
16
+ temperature,
17
+ top_p,
18
  ):
19
  messages = [{"role": "system", "content": system_message}]
20
 
21
+ # Add 3 shots prompting
22
+ messages.extend([
23
+ {"role": "user", "content": "I have a Toyota Supra and I want to customize it with a body kit and performance upgrades."},
24
+ {"role": "assistant", "content": "Sounds great! For the Toyota Supra, we offer a variety of body kits that can give your car an aggressive and aerodynamic look. Some popular options include the Rocket Bunny V3 widebody kit, Varis Arising II kit, and the Seibon Carbon Fiber kit. \n\nAs for performance upgrades, we can install a cold air intake, high-flow exhaust system, and tune the engine's ECU for increased power and throttle response. We can also upgrade the suspension with coilovers or air suspension for improved handling and stance. \n\nLet me know if you have any specific preferences or if you'd like more details on any of these modifications."},
25
+ {"role": "user", "content": "What about interior upgrades?"},
26
+ {"role": "assistant", "content": "Absolutely! For interior upgrades on the Toyota Supra, we offer a wide range of options. Some popular choices include:\n\n- Aftermarket racing seats (Bride, Recaro, Sparco, etc.) for better support and a sportier look.\n- Custom steering wheel (suede, carbon fiber, etc.) for a more personalized feel.\n- Shift knob and boot upgrades for a better shifting experience.\n- Carbon fiber or aluminum trim pieces to enhance the interior's appearance.\n- Upgraded audio system (speakers, amplifiers, subwoofers) for better sound quality.\n\nWe can also upholster the seats, door panels, and other surfaces in materials like Alcantara, leather, or even custom prints/patterns. Let me know if any of these interior upgrades interest you or if you have something else in mind!"},
27
+ {"role": "user", "content": "Hello, how can you help me?"},
28
+ ])
29
+
30
  for val in history:
31
  if val[0]:
32
  messages.append({"role": "user", "content": val[0]})
 
37
 
38
  response = ""
39
 
 
 
 
 
 
 
 
40
  for message in client.chat_completion(
41
  messages,
42
  max_tokens=max_tokens,
43
  stream=True,
44
+ temperature=temperature,
45
+ top_p=top_p,
46
  ):
47
  token = message.choices[0].delta.content
48
+
49
  response += token
50
  yield response
51
 
52
  """
53
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
54
  """
55
+
56
+ system_message = "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 conversational in your responses. You should remember the user's car model and tailor your answers accordingly, referring to the specific car model mentioned by the user. You limit yourself to answering the given question and maybe propose a suggestion but not write the next question of the user. Yhe user is aware that you are an AI assistant, you do not need to precise it in the conversation. If the user asks a question that has nothing to do with cars or Apex customs, say that you can only help him about Apex Customs."
57
+
58
  demo = gr.ChatInterface(
59
+ fn=respond,
60
+ chatbot=gr.Chatbot(value=[[None, initial_message]]),
61
  additional_inputs=[
62
+ gr.Textbox(value=system_message, label="System message"),
63
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
64
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.3, step=0.1, label="Temperature"),
65
+ gr.Slider(
66
+ minimum=0.1,
67
+ maximum=1.0,
68
+ value=0.95,
69
+ step=0.05,
70
+ label="Top-p (nucleus sampling)",
71
+ ),
72
  ],
73
  )
74
 
75
+
76
  if __name__ == "__main__":
77
+ demo.launch()