Fix sharing on chatbot with spaces (#10125)

* changes

* add changeset

* add changeset

---------

Co-authored-by: Ali Abid <aliabid94@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
aliabid94 2024-12-04 13:12:00 -08:00 committed by GitHub
parent 8530b6e2f9
commit b02c8b7d4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 42 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/chatbot": patch
"gradio": patch
---
fix:Fix sharing on chatbot with spaces

View File

@ -13,42 +13,44 @@ import type {
import type { LoadedComponent } from "../../core/src/types"; import type { LoadedComponent } from "../../core/src/types";
import { Gradio } from "@gradio/utils"; import { Gradio } from "@gradio/utils";
export const format_chat_for_sharing = async ( export const format_chat_for_sharing = async (
chat: [string | FileData | null, string | FileData | null][] chat: NormalisedMessage[]
): Promise<string> => { ): Promise<string> => {
let messages = await Promise.all( let messages = await Promise.all(
chat.map(async (message_pair) => { chat.map(async (message) => {
return await Promise.all( if (message.role === "system") return "";
message_pair.map(async (message, i) => { let speaker_emoji = message.role === "user" ? "😃" : "🤖";
if (message === null) return "";
let speaker_emoji = i === 0 ? "😃" : "🤖";
let html_content = ""; let html_content = "";
if (typeof message === "string") { if (message.type === "text") {
const regexPatterns = { const regexPatterns = {
audio: /<audio.*?src="(\/file=.*?)"/g, audio: /<audio.*?src="(\/file=.*?)"/g,
video: /<video.*?src="(\/file=.*?)"/g, video: /<video.*?src="(\/file=.*?)"/g,
image: /<img.*?src="(\/file=.*?)".*?\/>|!\[.*?\]\((\/file=.*?)\)/g image: /<img.*?src="(\/file=.*?)".*?\/>|!\[.*?\]\((\/file=.*?)\)/g
}; };
html_content = message; html_content = message.content;
for (let [_, regex] of Object.entries(regexPatterns)) { for (let [_, regex] of Object.entries(regexPatterns)) {
let match; let match;
while ((match = regex.exec(message)) !== null) { while ((match = regex.exec(message.content)) !== null) {
const fileUrl = match[1] || match[2]; const fileUrl = match[1] || match[2];
const newUrl = await uploadToHuggingFace(fileUrl, "url"); const newUrl = await uploadToHuggingFace(fileUrl, "url");
html_content = html_content.replace(fileUrl, newUrl); html_content = html_content.replace(fileUrl, newUrl);
} }
} }
} else { } else {
if (!message?.url) return ""; if (!message.content.value) return "";
const file_url = await uploadToHuggingFace(message.url, "url"); const url =
if (message.mime_type?.includes("audio")) { message.content.component === "video"
? message.content.value?.video.path
: message.content.value;
const file_url = await uploadToHuggingFace(url, "url");
if (message.content.component === "audio") {
html_content = `<audio controls src="${file_url}"></audio>`; html_content = `<audio controls src="${file_url}"></audio>`;
} else if (message.mime_type?.includes("video")) { } else if (message.content.component === "video") {
html_content = file_url; html_content = file_url;
} else if (message.mime_type?.includes("image")) { } else if (message.content.component === "image") {
html_content = `<img src="${file_url}" />`; html_content = `<img src="${file_url}" />`;
} }
} }
@ -56,15 +58,7 @@ export const format_chat_for_sharing = async (
return `${speaker_emoji}: ${html_content}`; return `${speaker_emoji}: ${html_content}`;
}) })
); );
}) return messages.join("\n");
);
return messages
.map((message_pair) =>
message_pair.join(
message_pair[0] !== "" && message_pair[1] !== "" ? "\n" : ""
)
)
.join("\n");
}; };
export interface UndoRetryData { export interface UndoRetryData {