Mr-Vicky-01 commited on
Commit
5454d65
1 Parent(s): c87b467

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -58,15 +58,17 @@ def handle_query(query):
58
  )
59
  ]
60
  text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
61
- query_engine = index.as_query_engine(text_qa_template=text_qa_template)
62
  answer = query_engine.query(query)
 
63
 
64
- if hasattr(answer, 'response'):
65
- return answer.response
66
- elif isinstance(answer, dict) and 'response' in answer:
67
- return answer['response']
68
- else:
69
- return "Sorry, I couldn't find an answer."
 
70
 
71
 
72
  # Streamlit app initialization
@@ -77,6 +79,10 @@ st.markdown("chat here👇")
77
  if 'messages' not in st.session_state:
78
  st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about its content.'}]
79
 
 
 
 
 
80
  with st.sidebar:
81
  st.title("Menu:")
82
  uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button")
@@ -90,11 +96,19 @@ with st.sidebar:
90
  st.success("Done")
91
 
92
  user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
93
- if user_prompt:
 
 
 
 
 
94
  st.session_state.messages.append({'role': 'user', "content": user_prompt})
95
- response = handle_query(user_prompt)
96
- st.session_state.messages.append({'role': 'assistant', "content": response})
97
 
98
- for message in st.session_state.messages:
99
- with st.chat_message(message['role']):
100
- st.write(message['content'])
 
 
 
 
58
  )
59
  ]
60
  text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
61
+ query_engine = index.as_query_engine(text_qa_template=text_qa_template, streaming=True)
62
  answer = query_engine.query(query)
63
+ yield answer.print_response_stream()
64
 
65
+
66
+ # if hasattr(answer, 'response'):
67
+ # return answer.response
68
+ # elif isinstance(answer, dict) and 'response' in answer:
69
+ # return answer['response']
70
+ # else:
71
+ # return "Sorry, I couldn't find an answer."
72
 
73
 
74
  # Streamlit app initialization
 
79
  if 'messages' not in st.session_state:
80
  st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about its content.'}]
81
 
82
+ for message in st.session_state.messages:
83
+ with st.chat_message(message['role']):
84
+ st.write(message['content'])
85
+
86
  with st.sidebar:
87
  st.title("Menu:")
88
  uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button")
 
96
  st.success("Done")
97
 
98
  user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
99
+ # if user_prompt:
100
+ # st.session_state.messages.append({'role': 'user', "content": user_prompt})
101
+ # response = handle_query(user_prompt)
102
+ # st.session_state.messages.append({'role': 'assistant', "content": response})
103
+
104
+ if user_prompt and uploaded_file:
105
  st.session_state.messages.append({'role': 'user', "content": user_prompt})
106
+ with st.chat_message("user", avatar="👽"):
107
+ st.write(user_prompt)
108
 
109
+ if st.session_state.messages[-1]["role"] != "assistant":
110
+ with st.chat_message("assistant"):
111
+ response = handle_query(user_prompt)
112
+ full_response = st.write_stream(response)
113
+ message = {"role": "assistant", "content": full_response}
114
+ st.session_state.messages.append(message)