Rahatara commited on
Commit
671a105
Β·
verified Β·
1 Parent(s): 60eae25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -16
app.py CHANGED
@@ -11,31 +11,23 @@ import os
11
  import re
12
  import openai
13
 
14
- openai.api_key = "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"
15
-
16
- # Load the saved PDF and prepare the chain
17
  class MyApp:
18
  def __init__(self) -> None:
19
- self.OPENAI_API_KEY: str = openai.api_key
20
  self.chain = None
21
  self.chat_history: list = []
22
  self.documents = None
23
  self.file_name = None
24
 
25
- def __call__(self, file: str) -> ConversationalRetrievalChain:
26
- if self.chain is None:
27
- self.chain = self.build_chain(file)
28
- return self.chain
29
 
30
  def process_file(self, file) -> Image.Image:
31
  loader = PyMuPDFLoader(file.name)
32
  self.documents = loader.load()
33
- pattern = r"/([^/]+)$"
34
- match = re.search(pattern, file.name)
35
- try:
36
- self.file_name = match.group(1)
37
- except:
38
- self.file_name = os.path.basename(file)
39
  doc = fitz.open(file.name)
40
  page = doc[0]
41
  pix = page.get_pixmap(dpi=150)
@@ -56,6 +48,14 @@ class MyApp:
56
  )
57
  return "Vector database built successfully!"
58
 
 
 
 
 
 
 
 
 
59
  def get_response(history, query, file):
60
  if not file:
61
  raise gr.Error(message="Upload a PDF")
@@ -76,6 +76,7 @@ def get_response(history, query, file):
76
  app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
77
  return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
78
 
 
79
  def render_file(file) -> Image.Image:
80
  doc = fitz.open(file.name)
81
  page = doc[0]
@@ -83,6 +84,7 @@ def render_file(file) -> Image.Image:
83
  image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
84
  return image
85
 
 
86
  def purge_chat_and_render_first(file) -> Tuple[Image.Image, list]:
87
  app.chat_history = []
88
  doc = fitz.open(file.name)
@@ -91,6 +93,7 @@ def purge_chat_and_render_first(file) -> Tuple[Image.Image, list]:
91
  image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
92
  return image, []
93
 
 
94
  def refresh_chat():
95
  app.chat_history = []
96
  return []
@@ -99,12 +102,29 @@ app = MyApp()
99
 
100
  # Pre-process the saved PDF file
101
  saved_file_path = "track_training.pdf"
102
- app.process_file(open(saved_file_path, 'rb'))
103
- app.build_chain(open(saved_file_path, 'rb'))
 
 
 
 
 
 
104
 
 
105
  with gr.Blocks() as demo:
106
  with gr.Tab("Inst RAG"):
107
  with gr.Column():
 
 
 
 
 
 
 
 
 
 
108
  with gr.Row():
109
  btn = gr.UploadButton("πŸ“ Upload a PDF", file_types=[".pdf"])
110
  show_img = gr.Image(label="Uploaded PDF")
 
11
  import re
12
  import openai
13
 
14
+ # MyApp class to handle the processes
 
 
15
  class MyApp:
16
  def __init__(self) -> None:
17
+ self.OPENAI_API_KEY: str = None # Initialize with None
18
  self.chain = None
19
  self.chat_history: list = []
20
  self.documents = None
21
  self.file_name = None
22
 
23
+ def set_api_key(self, api_key: str):
24
+ self.OPENAI_API_KEY = api_key
25
+ openai.api_key = api_key
 
26
 
27
  def process_file(self, file) -> Image.Image:
28
  loader = PyMuPDFLoader(file.name)
29
  self.documents = loader.load()
30
+ self.file_name = os.path.basename(file.name)
 
 
 
 
 
31
  doc = fitz.open(file.name)
32
  page = doc[0]
33
  pix = page.get_pixmap(dpi=150)
 
48
  )
49
  return "Vector database built successfully!"
50
 
51
+ # Function to add text to chat history
52
+ def add_text(history: List[Tuple[str, str]], text: str) -> List[Tuple[str, str]]:
53
+ if not text:
54
+ raise gr.Error("Enter text")
55
+ history.append((text, ""))
56
+ return history
57
+
58
+ # Function to get response from the model
59
  def get_response(history, query, file):
60
  if not file:
61
  raise gr.Error(message="Upload a PDF")
 
76
  app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
77
  return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
78
 
79
+ # Function to render file
80
  def render_file(file) -> Image.Image:
81
  doc = fitz.open(file.name)
82
  page = doc[0]
 
84
  image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
85
  return image
86
 
87
+ # Function to purge chat and render first page of PDF
88
  def purge_chat_and_render_first(file) -> Tuple[Image.Image, list]:
89
  app.chat_history = []
90
  doc = fitz.open(file.name)
 
93
  image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
94
  return image, []
95
 
96
+ # Function to refresh chat
97
  def refresh_chat():
98
  app.chat_history = []
99
  return []
 
102
 
103
  # Pre-process the saved PDF file
104
  saved_file_path = "track_training.pdf"
105
+ with open(saved_file_path, 'rb') as saved_file:
106
+ app.process_file(saved_file)
107
+ app.build_chain(saved_file)
108
+
109
+ # Function to set API key
110
+ def set_api_key(api_key):
111
+ app.set_api_key(api_key)
112
+ return f"API Key set to {api_key[:4]}...{api_key[-4:]}"
113
 
114
+ # Gradio interface
115
  with gr.Blocks() as demo:
116
  with gr.Tab("Inst RAG"):
117
  with gr.Column():
118
+ api_key_input = gr.Textbox(label="OpenAI API Key", type="password", placeholder="Enter your OpenAI API Key")
119
+ api_key_btn = gr.Button("Set API Key")
120
+ api_key_status = gr.Textbox(value="API Key status", interactive=False)
121
+
122
+ api_key_btn.click(
123
+ fn=set_api_key,
124
+ inputs=[api_key_input],
125
+ outputs=[api_key_status]
126
+ )
127
+
128
  with gr.Row():
129
  btn = gr.UploadButton("πŸ“ Upload a PDF", file_types=[".pdf"])
130
  show_img = gr.Image(label="Uploaded PDF")