lubani commited on
Commit
1e9fcf2
·
1 Parent(s): e88497b
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -8,7 +8,6 @@ import gradio as gr
8
  # import shutil
9
  import tempfile
10
 
11
- # default model
12
  repo_id = "mistralai/Mistral-7B-Instruct-v0.1"
13
  model_type = 'text-generation'
14
 
@@ -25,15 +24,15 @@ def add_text(history, text):
25
 
26
 
27
  def hasFile(history):
 
28
  for user_prompt, bot_response in history:
29
  if '.pdf' in user_prompt.lower():
30
- return True
31
 
32
- return False
33
 
34
  def modelChanged(history, drop):
35
 
36
-
37
  history = history + [(f'===> {drop}', None)]
38
  return history, drop
39
 
@@ -76,6 +75,11 @@ def copy_pdf(source_path, destination_path):
76
 
77
  def add_file(history, file):
78
 
 
 
 
 
 
79
  file_path = os.path.join(temp_dir.name, os.path.basename(file))
80
  # shutil.copyfile(file.name, file_path) # <---Asynchronous
81
  copy_pdf(file.name, file_path)
@@ -84,6 +88,9 @@ def add_file(history, file):
84
  history = history + [(os.path.basename(file), None)]
85
  return history
86
 
 
 
 
87
 
88
  def format_prompt(message, history, model):
89
  if model is None or 'mistral' in model.lower():
@@ -111,13 +118,17 @@ def bot(history, model=None):
111
  if model:
112
  local_llm = CustomLLM(repo_id=model, model_type=model_type, api_token=API_TOKEN)
113
 
114
- if len(history) > 0 and len(history[-1]) > 0 and '.pdf' in history[-1][0]:
115
- response = "You uploaded a PDF file. You can ask questions from the file."
116
-
117
- elif len(history) > 0 and len(history[-1]) > 0 and '===>' in history[-1][0]:
118
  new_model = history[-1][0].replace("===>", "")
119
  response = f"You have changed the model to {new_model}"
120
 
 
 
 
 
 
 
121
  else:
122
  prompt = history[-1][0]
123
 
@@ -182,6 +193,7 @@ with gr.Blocks() as demo:
182
  container=False,
183
  )
184
  btn = gr.UploadButton("📁", file_types=[".pdf"])
 
185
 
186
  drop.change(modelChanged, [chatbot, drop], [chatbot, drop], queue=False).then(
187
  bot, [chatbot, drop], chatbot
@@ -196,6 +208,8 @@ with gr.Blocks() as demo:
196
  bot, [chatbot, drop], chatbot
197
  )
198
 
 
 
199
 
200
 
201
  demo.queue()
 
8
  # import shutil
9
  import tempfile
10
 
 
11
  repo_id = "mistralai/Mistral-7B-Instruct-v0.1"
12
  model_type = 'text-generation'
13
 
 
24
 
25
 
26
  def hasFile(history):
27
+ pdf_files = 0
28
  for user_prompt, bot_response in history:
29
  if '.pdf' in user_prompt.lower():
30
+ pdf_files += 1
31
 
32
+ return pdf_files
33
 
34
  def modelChanged(history, drop):
35
 
 
36
  history = history + [(f'===> {drop}', None)]
37
  return history, drop
38
 
 
75
 
76
  def add_file(history, file):
77
 
78
+ pdf_files = hasFile(history)
79
+ if pdf_files + 1 >= 4:
80
+ history = history + [("%s!!!"%os.path.basename(file), None)]
81
+ return history
82
+
83
  file_path = os.path.join(temp_dir.name, os.path.basename(file))
84
  # shutil.copyfile(file.name, file_path) # <---Asynchronous
85
  copy_pdf(file.name, file_path)
 
88
  history = history + [(os.path.basename(file), None)]
89
  return history
90
 
91
+ def clearClick():
92
+ print("clear temp files...")
93
+ temp_dir.cleanup()
94
 
95
  def format_prompt(message, history, model):
96
  if model is None or 'mistral' in model.lower():
 
118
  if model:
119
  local_llm = CustomLLM(repo_id=model, model_type=model_type, api_token=API_TOKEN)
120
 
121
+
122
+ if len(history) > 0 and len(history[-1]) > 0 and '===>' in history[-1][0]:
 
 
123
  new_model = history[-1][0].replace("===>", "")
124
  response = f"You have changed the model to {new_model}"
125
 
126
+ elif len(history) > 0 and len(history[-1]) > 0 and '.pdf!!!' in history[-1][0].lower():
127
+ response = f"Unable to add file. Maximum 3 files allowed."
128
+
129
+ elif len(history) > 0 and len(history[-1]) > 0 and '.pdf' in history[-1][0]:
130
+ response = "You uploaded a PDF file. You can ask questions from the file."
131
+
132
  else:
133
  prompt = history[-1][0]
134
 
 
193
  container=False,
194
  )
195
  btn = gr.UploadButton("📁", file_types=[".pdf"])
196
+ clear_btn = gr.ClearButton([chatbot, txt])
197
 
198
  drop.change(modelChanged, [chatbot, drop], [chatbot, drop], queue=False).then(
199
  bot, [chatbot, drop], chatbot
 
208
  bot, [chatbot, drop], chatbot
209
  )
210
 
211
+ clear_btn.click(clearClick)
212
+
213
 
214
 
215
  demo.queue()