This commit is contained in:
Saifeddine ALOUI 2023-11-22 20:49:54 +01:00
parent 5c6005507b
commit 96b245e138
6 changed files with 136 additions and 37 deletions

View File

@ -412,6 +412,36 @@ class DiscussionsDB:
return discussions
def export_discussions_to_markdown(self, discussions_ids:list, title = ""):
# Convert the list of discussion IDs to a tuple
discussions_ids_tuple = tuple(discussions_ids)
txt = ','.join(['?'] * len(discussions_ids_tuple))
db_discussions = self.select(
f"SELECT * FROM discussion WHERE id IN ({txt})",
discussions_ids_tuple
)
discussions = f"# {title}" if title!="" else ""
for row in db_discussions:
discussion_id = row[0]
discussion_title = row[1]
discussions += f"## {discussion_title}\n"
rows = self.select(f"SELECT sender, content, message_type, rank, parent_message_id, binding, model, personality, created_at, finished_generating_at FROM message WHERE discussion_id=?",(discussion_id,))
for message_row in rows:
sender = message_row[0]
content = message_row[1]
content_type = message_row[2]
rank = message_row[3]
parent_message_id = message_row[4]
binding = message_row[5]
model = message_row[6]
personality = message_row[7]
created_at = message_row[8]
finished_generating_at = message_row[9]
discussions +=f"### {sender}:\n{content}\n"
discussions +=f"\n"
return discussions
class Message:
def __init__(

9
app.py
View File

@ -692,7 +692,14 @@ try:
def export_multiple_discussions(self):
data = request.get_json()
discussion_ids = data["discussion_ids"]
discussions = self.db.export_discussions_to_json(discussion_ids)
export_format = data["export_format"]
if export_format=="json":
discussions = self.db.export_discussions_to_json(discussion_ids)
elif export_format=="markdown":
discussions = self.db.export_discussions_to_markdown(discussion_ids)
else:
discussions = self.db.export_discussions_to_markdown(discussion_ids)
return jsonify(discussions)
def import_multiple_discussions(self):

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

4
web/dist/index.html vendored
View File

@ -6,8 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LoLLMS WebUI - Welcome</title>
<script type="module" crossorigin src="/assets/index-8d00ac76.js"></script>
<link rel="stylesheet" href="/assets/index-c14230cd.css">
<script type="module" crossorigin src="/assets/index-c54d7272.js"></script>
<link rel="stylesheet" href="/assets/index-6c6983a2.css">
</head>
<body>
<div id="app"></div>

View File

@ -174,9 +174,13 @@
<div class="flex gap-3">
<button class="text-2xl hover:text-secondary duration-75 active:scale-90 rotate-90"
title="Export selected to a file" type="button" @click.stop="exportDiscussions">
title="Export selected to a json file" type="button" @click.stop="exportDiscussionsAsJson">
<i data-feather="log-out"></i>
</button>
<button class="text-2xl hover:text-secondary duration-75 active:scale-90 rotate-90"
title="Export selected to a martkdown file" type="button" @click.stop="exportDiscussionsAsMarkdown">
<i data-feather="bookmark"></i>
</button>
<button class="text-2xl hover:text-secondary duration-75 active:scale-90 " title="Select All"
type="button" @click.stop="selectAllDiscussions">
<i data-feather="list"></i>
@ -798,11 +802,12 @@ export default {
return {}
}
},
async export_multiple_discussions(discussionIdArr) {
async export_multiple_discussions(discussionIdArr, export_format) {
try {
if (discussionIdArr.length > 0) {
const res = await axios.post('/export_multiple_discussions', {
discussion_ids: discussionIdArr
discussion_ids: discussionIdArr,
export_format: export_format
})
if (res) {
@ -1578,6 +1583,17 @@ export default {
a.click();
document.body.removeChild(a);
},
saveMarkdowntoFile(markdownData, filename) {
filename = filename || "data.md"
const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([markdownData], {
type: "text/plain"
}));
a.setAttribute("download", filename);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
},
parseJsonObj(obj) {
try {
const ret = JSON.parse(obj)
@ -1597,7 +1613,53 @@ export default {
fileReader.readAsText(file)
})
},
async exportDiscussions() {
async exportDiscussionsAsMarkdown() {
// Export selected discussions
const discussionIdArr = this.list.filter((item) => item.checkBoxValue == true).map((item) => {
return item.id
})
if (discussionIdArr.length > 0) {
console.log("export", discussionIdArr)
let dateObj = new Date()
const year = dateObj.getFullYear();
const month = (dateObj.getMonth() + 1).toString().padStart(2, "0");
const day = dateObj.getDate().toString().padStart(2, "0");
const hours = dateObj.getHours().toString().padStart(2, "0");
const minutes = dateObj.getMinutes().toString().padStart(2, "0");
const seconds = dateObj.getSeconds().toString().padStart(2, "0");
const formattedDate =
year +
"." +
month +
"." +
day +
"." +
hours +
"" +
minutes +
"" +
seconds;
const filename = 'discussions_export_' + formattedDate + '.md'
this.loading = true
const res = await this.export_multiple_discussions(discussionIdArr,"markdown")
if (res) {
this.saveMarkdowntoFile(res, filename)
this.$refs.toast.showToast("Successfully exported", 4, true)
this.isCheckbox = false
} else {
this.$refs.toast.showToast("Failed to export discussions", 4, false)
}
this.loading = false
}
},
async exportDiscussionsAsJson() {
// Export selected discussions
const discussionIdArr = this.list.filter((item) => item.checkBoxValue == true).map((item) => {
@ -1629,7 +1691,7 @@ export default {
const filename = 'discussions_export_' + formattedDate + '.json'
this.loading = true
const res = await this.export_multiple_discussions(discussionIdArr)
const res = await this.export_multiple_discussions(discussionIdArr, "json")
if (res) {
this.saveJSONtoFile(res, filename)