Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from openai import OpenAI
|
|
4 |
import json
|
5 |
import requests
|
6 |
import datetime
|
|
|
7 |
|
8 |
openai_api_key = os.getenv("OPENROUTER_API_KEY")
|
9 |
openai_base_url = os.getenv("OPENAI_BASE_URL")
|
@@ -215,17 +216,18 @@ def export_chat(history):
|
|
215 |
# Convert to JSONL format (each line is a JSON object)
|
216 |
jsonl_content = "\n".join([json.dumps(msg) for msg in messages])
|
217 |
|
218 |
-
# Create a file for download
|
219 |
-
|
220 |
-
|
|
|
221 |
|
222 |
-
return
|
223 |
|
224 |
def import_chat(file):
|
225 |
"""Import chat history from a JSONL file."""
|
226 |
try:
|
227 |
-
content = file.
|
228 |
-
lines = [line.strip() for line in content.split(
|
229 |
|
230 |
messages = [json.loads(line) for line in lines]
|
231 |
|
@@ -242,7 +244,7 @@ def import_chat(file):
|
|
242 |
i += 2
|
243 |
else:
|
244 |
i += 1
|
245 |
-
|
246 |
return new_history
|
247 |
except Exception as e:
|
248 |
raise gr.Error(f"Error importing chat: {str(e)}")
|
@@ -282,10 +284,10 @@ with gr.Blocks(css=css) as demo:
|
|
282 |
import_btn = gr.UploadButton("Import Chat", file_types=[".jsonl"])
|
283 |
|
284 |
# Connect buttons to functions
|
285 |
-
export_btn.click(fn=export_chat, inputs=chatbot, outputs=gr.File())
|
286 |
import_btn.upload(fn=import_chat, inputs=[import_btn], outputs=chatbot)
|
287 |
|
288 |
gr.Markdown(LICENSE)
|
289 |
|
290 |
if __name__ == "__main__":
|
291 |
-
demo.launch()
|
|
|
4 |
import json
|
5 |
import requests
|
6 |
import datetime
|
7 |
+
import tempfile
|
8 |
|
9 |
openai_api_key = os.getenv("OPENROUTER_API_KEY")
|
10 |
openai_base_url = os.getenv("OPENAI_BASE_URL")
|
|
|
216 |
# Convert to JSONL format (each line is a JSON object)
|
217 |
jsonl_content = "\n".join([json.dumps(msg) for msg in messages])
|
218 |
|
219 |
+
# Create a temporary file for download
|
220 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jsonl", mode="w", encoding="utf-8")
|
221 |
+
temp_file.write(jsonl_content)
|
222 |
+
temp_file.close()
|
223 |
|
224 |
+
return temp_file.name # Return the file path
|
225 |
|
226 |
def import_chat(file):
|
227 |
"""Import chat history from a JSONL file."""
|
228 |
try:
|
229 |
+
content = file.read() # Read the file directly as a string
|
230 |
+
lines = [line.strip() for line in content.split("\n") if line.strip()]
|
231 |
|
232 |
messages = [json.loads(line) for line in lines]
|
233 |
|
|
|
244 |
i += 2
|
245 |
else:
|
246 |
i += 1
|
247 |
+
|
248 |
return new_history
|
249 |
except Exception as e:
|
250 |
raise gr.Error(f"Error importing chat: {str(e)}")
|
|
|
284 |
import_btn = gr.UploadButton("Import Chat", file_types=[".jsonl"])
|
285 |
|
286 |
# Connect buttons to functions
|
287 |
+
export_btn.click(fn=export_chat, inputs=chatbot, outputs=gr.File(label="Download Chat History"))
|
288 |
import_btn.upload(fn=import_chat, inputs=[import_btn], outputs=chatbot)
|
289 |
|
290 |
gr.Markdown(LICENSE)
|
291 |
|
292 |
if __name__ == "__main__":
|
293 |
+
demo.launch(share=True)
|