Moha782 commited on
Commit
76e0d03
1 Parent(s): 1dd768c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -1
app.py CHANGED
@@ -1,11 +1,22 @@
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("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
9
 
10
  def respond(
11
  message,
@@ -27,12 +38,16 @@ def respond(
27
 
28
  response = ""
29
 
 
 
 
30
  for message in client.chat_completion(
31
  messages,
32
  max_tokens=max_tokens,
33
  stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
 
36
  ):
37
  token = message.choices[0].delta.content
38
 
@@ -58,6 +73,5 @@ demo = gr.ChatInterface(
58
  ],
59
  )
60
 
61
-
62
  if __name__ == "__main__":
63
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from pathlib import Path
4
+ from typing import List
5
+ from pdfplumber import open as open_pdf
6
 
7
  """
8
  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
9
  """
10
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
11
 
12
+ # Load the PDF file
13
+ pdf_path = Path("path/to/your/pdf/file.pdf")
14
+ with open_pdf(pdf_path) as pdf:
15
+ text = "\n".join(page.extract_text() for page in pdf.pages)
16
+
17
+ # Split the PDF text into chunks
18
+ chunk_size = 1000 # Adjust this value based on your needs
19
+ text_chunks: List[str] = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
20
 
21
  def respond(
22
  message,
 
38
 
39
  response = ""
40
 
41
+ # Pass relevant chunks as context
42
+ relevant_chunks = [chunk for chunk in text_chunks if message.lower() in chunk.lower()]
43
+
44
  for message in client.chat_completion(
45
  messages,
46
  max_tokens=max_tokens,
47
  stream=True,
48
  temperature=temperature,
49
  top_p=top_p,
50
+ files={"context": "\n".join(relevant_chunks)}, # Pass relevant chunks as context
51
  ):
52
  token = message.choices[0].delta.content
53
 
 
73
  ],
74
  )
75
 
 
76
  if __name__ == "__main__":
77
  demo.launch()