SauravMaheshkar commited on
Commit
5e9da0f
·
unverified ·
1 Parent(s): c3cfbdd

fix: add file content to chat state

Browse files
Files changed (1) hide show
  1. app.py +45 -51
app.py CHANGED
@@ -93,16 +93,11 @@ def update_state(history: HistoryType, message: Optional[Dict[str, str]]):
93
  if isinstance(message, dict) and "files" in message:
94
  for file_path in message["files"]:
95
  try:
96
- state.context = load_paper_as_context(file_path=file_path)
97
- doc_context = [x.get_content() for x in state.context]
98
- history.append(
99
- {
100
- "role": "assistant",
101
- "content": " ".join(doc_context)[
102
- : pipe.model.config.max_position_embeddings
103
- ],
104
- }
105
- )
106
  except Exception as e:
107
  history.append(
108
  {"role": "assistant", "content": f"Error loading file: {str(e)}"}
@@ -149,47 +144,46 @@ def bot(history: HistoryType):
149
 
150
  def create_interface():
151
  with gr.Blocks() as demo:
152
- with weave.attributes({"session": "hf-app"}):
153
- global state
154
- state = ChatState()
155
- gr.Markdown(
156
- """
157
- <a href="https://github.com/SauravMaheshkar/papersai">
158
- <div align="center"><h1>papers.ai</h1></div>
159
- </a>
160
- """,
161
- )
162
- chatbot = gr.Chatbot(
163
- show_label=False,
164
- height=600,
165
- type="messages",
166
- show_copy_all_button=True,
167
- placeholder="Upload a research paper and ask questions!!",
168
- )
169
-
170
- chat_input = gr.MultimodalTextbox(
171
- interactive=True,
172
- file_count="single",
173
- placeholder="Upload a document or type your message...",
174
- show_label=False,
175
- )
176
-
177
- chat_msg = chat_input.submit(
178
- fn=update_state,
179
- inputs=[chatbot, chat_input],
180
- outputs=[chatbot, chat_input],
181
- )
182
-
183
- bot_msg = chat_msg.then( # noqa: F841
184
- fn=bot, inputs=[chatbot], outputs=chatbot, api_name="bot_response"
185
- )
186
-
187
- chatbot.like(
188
- fn=record_feedback,
189
- inputs=None,
190
- outputs=None,
191
- like_user_message=True,
192
- )
193
 
194
  return demo
195
 
 
93
  if isinstance(message, dict) and "files" in message:
94
  for file_path in message["files"]:
95
  try:
96
+ text = load_paper_as_context(file_path=file_path)
97
+ doc_context = [x.get_content() for x in text]
98
+ state.context = " ".join(doc_context)[
99
+ : pipe.model.config.max_position_embeddings
100
+ ]
 
 
 
 
 
101
  except Exception as e:
102
  history.append(
103
  {"role": "assistant", "content": f"Error loading file: {str(e)}"}
 
144
 
145
  def create_interface():
146
  with gr.Blocks() as demo:
147
+ global state
148
+ state = ChatState()
149
+ gr.Markdown(
150
+ """
151
+ <a href="https://github.com/SauravMaheshkar/papersai">
152
+ <div align="center"><h1>papers.ai</h1></div>
153
+ </a>
154
+ """,
155
+ )
156
+ chatbot = gr.Chatbot(
157
+ show_label=False,
158
+ height=600,
159
+ type="messages",
160
+ show_copy_all_button=True,
161
+ placeholder="Upload a research paper and ask questions!!",
162
+ )
163
+
164
+ chat_input = gr.MultimodalTextbox(
165
+ interactive=True,
166
+ file_count="single",
167
+ placeholder="Upload a document or type your message...",
168
+ show_label=False,
169
+ )
170
+
171
+ chat_msg = chat_input.submit(
172
+ fn=update_state,
173
+ inputs=[chatbot, chat_input],
174
+ outputs=[chatbot, chat_input],
175
+ )
176
+
177
+ bot_msg = chat_msg.then( # noqa: F841
178
+ fn=bot, inputs=[chatbot], outputs=chatbot, api_name="bot_response"
179
+ )
180
+
181
+ chatbot.like(
182
+ fn=record_feedback,
183
+ inputs=None,
184
+ outputs=None,
185
+ like_user_message=True,
186
+ )
 
187
 
188
  return demo
189