Spaces:
Sleeping
Sleeping
fix: add file content to chat state
Browse files
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 |
-
|
97 |
-
doc_context = [x.get_content() for x in
|
98 |
-
|
99 |
-
|
100 |
-
|
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 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
<
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
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 |
|