arman77mxx commited on
Commit
2a4ee1d
verified
1 Parent(s): be2b280

se agrega funcion de limpieza de textbox y que se quede fija el historial

Browse files
Files changed (1) hide show
  1. app.py +22 -3
app.py CHANGED
@@ -33,15 +33,34 @@ def create_embeddings(pdfs):
33
 
34
  return knowledge_base
35
 
 
 
 
 
 
 
36
  if pdf_files:
37
  knowledge_base = create_embeddings(pdf_files)
38
- user_question = st.text_input("Haz una pregunta sobre tus PDFs:")
39
 
40
- if user_question:
41
  os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
42
  docs = knowledge_base.similarity_search(user_question, 3)
43
  llm = ChatOpenAI(model_name='gpt-3.5-turbo')
44
  chain = load_qa_chain(llm, chain_type="stuff")
45
  respuesta = chain.run(input_documents=docs, question=user_question)
46
 
47
- st.write(respuesta)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  return knowledge_base
35
 
36
+ # Inicializa el estado de sesi贸n para la pregunta del usuario y el historial
37
+ if 'user_question' not in st.session_state:
38
+ st.session_state['user_question'] = ''
39
+ if 'history' not in st.session_state:
40
+ st.session_state['history'] = []
41
+
42
  if pdf_files:
43
  knowledge_base = create_embeddings(pdf_files)
44
+ user_question = st.text_input("Haz una pregunta sobre tus PDFs:", value=st.session_state['user_question'])
45
 
46
+ if st.button("Preguntar"):
47
  os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
48
  docs = knowledge_base.similarity_search(user_question, 3)
49
  llm = ChatOpenAI(model_name='gpt-3.5-turbo')
50
  chain = load_qa_chain(llm, chain_type="stuff")
51
  respuesta = chain.run(input_documents=docs, question=user_question)
52
 
53
+ # A帽ade la pregunta y la respuesta al historial
54
+ st.session_state['history'].append((user_question, respuesta))
55
+
56
+ # Limpiar el cuadro de texto despu茅s de mostrar la respuesta
57
+ st.session_state['user_question'] = ''
58
+
59
+ # Forzar la recarga de la p谩gina para actualizar el cuadro de texto
60
+ st.experimental_rerun()
61
+
62
+ # Mostrar el historial de preguntas y respuestas
63
+ st.write("### Historial de preguntas y respuestas")
64
+ for question, answer in st.session_state['history']:
65
+ st.write(f"**Pregunta:** {question}")
66
+ st.write(f"**Respuesta:** {answer}")