Shreyas094 commited on
Commit
176123e
1 Parent(s): a6c785f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -69,18 +69,18 @@ def update_vectors(files, parser):
69
 
70
  return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
71
 
72
- def generate_chunked_response(prompt, max_tokens=1000, max_chunks=5):
73
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
74
  headers = {"Authorization": f"Bearer {huggingface_token}"}
75
  payload = {
76
  "inputs": prompt,
77
  "parameters": {
78
  "max_new_tokens": max_tokens,
79
- "temperature": 0.7,
80
- "top_p": 0.95,
81
  "top_k": 40,
82
- "repetition_penalty": 1.1,
83
- "stop": ["</s>", "[/INST]"] # Add stop tokens
84
  }
85
  }
86
 
@@ -175,13 +175,13 @@ After writing the document, please provide a list of sources used in your respon
175
 
176
  return main_content, sources
177
 
178
- def chatbot_interface(message, history, use_web_search):
179
  if use_web_search:
180
- main_content, sources = get_response_with_search(message)
181
  formatted_response = f"{main_content}\n\nSources:\n{sources}"
182
  else:
183
- response = get_response_from_pdf(message)
184
- formatted_response = response # No sources for PDF responses
185
 
186
  history.append((message, formatted_response))
187
  return history
@@ -201,6 +201,11 @@ with gr.Blocks() as demo:
201
  chatbot = gr.Chatbot(label="Conversation")
202
  msg = gr.Textbox(label="Ask a question")
203
  use_web_search = gr.Checkbox(label="Use Web Search", value=False)
 
 
 
 
 
204
  submit = gr.Button("Submit")
205
 
206
  gr.Examples(
@@ -213,8 +218,12 @@ with gr.Blocks() as demo:
213
  inputs=msg,
214
  )
215
 
216
- submit.click(chatbot_interface, inputs=[msg, chatbot, use_web_search], outputs=[chatbot])
217
- msg.submit(chatbot_interface, inputs=[msg, chatbot, use_web_search], outputs=[chatbot])
 
 
 
 
218
 
219
  gr.Markdown(
220
  """
@@ -223,7 +232,8 @@ with gr.Blocks() as demo:
223
  2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
224
  3. Ask questions in the textbox.
225
  4. Toggle "Use Web Search" to switch between PDF chat and web search.
226
- 5. Click "Submit" or press Enter to get a response.
 
227
  """
228
  )
229
 
 
69
 
70
  return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
71
 
72
+ def generate_chunked_response(prompt, max_tokens=1000, max_chunks=5, temperature=0.7, repetition_penalty=1.1):
73
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
74
  headers = {"Authorization": f"Bearer {huggingface_token}"}
75
  payload = {
76
  "inputs": prompt,
77
  "parameters": {
78
  "max_new_tokens": max_tokens,
79
+ "temperature": temperature,
80
+ "top_p": 0.4,
81
  "top_k": 40,
82
+ "repetition_penalty": repetition_penalty,
83
+ "stop": ["</s>", "[/INST]"]
84
  }
85
  }
86
 
 
175
 
176
  return main_content, sources
177
 
178
+ def chatbot_interface(message, history, use_web_search, temperature, repetition_penalty):
179
  if use_web_search:
180
+ main_content, sources = get_response_with_search(message, temperature, repetition_penalty)
181
  formatted_response = f"{main_content}\n\nSources:\n{sources}"
182
  else:
183
+ response = get_response_from_pdf(message, temperature, repetition_penalty)
184
+ formatted_response = response
185
 
186
  history.append((message, formatted_response))
187
  return history
 
201
  chatbot = gr.Chatbot(label="Conversation")
202
  msg = gr.Textbox(label="Ask a question")
203
  use_web_search = gr.Checkbox(label="Use Web Search", value=False)
204
+
205
+ with gr.Row():
206
+ temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
207
+ repetition_penalty_slider = gr.Slider(minimum=1.0, maximum=2.0, value=1.1, step=0.1, label="Repetition Penalty")
208
+
209
  submit = gr.Button("Submit")
210
 
211
  gr.Examples(
 
218
  inputs=msg,
219
  )
220
 
221
+ submit.click(chatbot_interface,
222
+ inputs=[msg, chatbot, use_web_search, temperature_slider, repetition_penalty_slider],
223
+ outputs=[chatbot])
224
+ msg.submit(chatbot_interface,
225
+ inputs=[msg, chatbot, use_web_search, temperature_slider, repetition_penalty_slider],
226
+ outputs=[chatbot])
227
 
228
  gr.Markdown(
229
  """
 
232
  2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
233
  3. Ask questions in the textbox.
234
  4. Toggle "Use Web Search" to switch between PDF chat and web search.
235
+ 5. Adjust Temperature and Repetition Penalty sliders to fine-tune the response generation.
236
+ 6. Click "Submit" or press Enter to get a response.
237
  """
238
  )
239