MohammedNasser commited on
Commit
32dbb49
·
verified ·
1 Parent(s): 1e43581

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -200,8 +200,8 @@ def validate_pdf(pdf):
200
  return "الملف صالح للدردشة", True
201
 
202
  def upload_pdf(pdf_file):
203
- global vectorstore, chain,chat_history # Use global variables to store state
204
- chat_history = []
205
  data = load_pdf(pdf_file)
206
  vectorstore = prepare_vectorstore(data)
207
  chain = create_chain(vectorstore)
@@ -209,7 +209,7 @@ def upload_pdf(pdf_file):
209
 
210
 
211
  def chat(user_input):
212
- global chain chat_history # Access the global chain variable
213
 
214
  chat_history.append({"role": "user", "content": user_input})
215
 
@@ -226,7 +226,7 @@ def chat(user_input):
226
  response = chain({"question": prompt})
227
  assistant_response = response["answer"]
228
 
229
- chat_history.append({"role": "assistant", "content": assistant_response})
230
  # Generate a unique identifier for the audio file
231
  audio_id = str(uuid.uuid4())
232
 
@@ -235,7 +235,7 @@ def chat(user_input):
235
  audio_file = f"{audio_id}.mp3"
236
  tts.save(audio_file)
237
 
238
- history_display = [(h["role"], h["content"]) for h in chat_history]
239
  return history_display, audio_file
240
 
241
  with gr.Blocks(css=custom_css) as demo:
 
200
  return "الملف صالح للدردشة", True
201
 
202
  def upload_pdf(pdf_file):
203
+ global vectorstore, chain,chathistory
204
+ chathistory = []
205
  data = load_pdf(pdf_file)
206
  vectorstore = prepare_vectorstore(data)
207
  chain = create_chain(vectorstore)
 
209
 
210
 
211
  def chat(user_input):
212
+ global chain chathistory
213
 
214
  chat_history.append({"role": "user", "content": user_input})
215
 
 
226
  response = chain({"question": prompt})
227
  assistant_response = response["answer"]
228
 
229
+ chathistory.append({"role": "assistant", "content": assistant_response})
230
  # Generate a unique identifier for the audio file
231
  audio_id = str(uuid.uuid4())
232
 
 
235
  audio_file = f"{audio_id}.mp3"
236
  tts.save(audio_file)
237
 
238
+ history_display = [(h["role"], h["content"]) for h in chathistory]
239
  return history_display, audio_file
240
 
241
  with gr.Blocks(css=custom_css) as demo: