Skip to content

Commit

Permalink
chatbot component tweaks (#8594)
Browse files Browse the repository at this point in the history
  • Loading branch information
pngwn committed Jun 24, 2024
1 parent d35c290 commit 530f8a0
Show file tree
Hide file tree
Showing 34 changed files with 721 additions and 361 deletions.
13 changes: 13 additions & 0 deletions .changeset/clear-cloths-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@gradio/audio": patch
"@gradio/chatbot": patch
"@gradio/gallery": patch
"@gradio/icons": patch
"@gradio/image": patch
"@gradio/plot": patch
"@gradio/video": patch
"@gradio/wasm": patch
"gradio": patch
---

fix:chatbot component tweaks
2 changes: 1 addition & 1 deletion .config/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const base = defineConfig({
timeout: 30000,
testMatch: /.*\.spec\.ts/,
testDir: "..",
workers: process.env.CI ? 1 : undefined,
workers: 1,
retries: 3
});

Expand Down
2 changes: 1 addition & 1 deletion demo/chatbot_core_components/run.ipynb

Large diffs are not rendered by default.

52 changes: 50 additions & 2 deletions demo/chatbot_core_components/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,48 @@

# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.

txt = """
Absolutely! The mycorrhizal network, often referred to as the "Wood Wide Web," is a symbiotic association between fungi and the roots of most plant species. Here’s a deeper dive into how it works and its implications:
### How It Works
1. **Symbiosis**: Mycorrhizal fungi attach to plant roots, extending far into the soil. The plant provides the fungi with carbohydrates produced via photosynthesis. In return, the fungi help the plant absorb water and essential nutrients like phosphorus and nitrogen from the soil.
2. **Network Formation**: The fungal hyphae (thread-like structures) connect individual plants, creating an extensive underground network. This network can link many plants together, sometimes spanning entire forests.
3. **Communication**: Trees and plants use this network to communicate and share resources. For example, a tree under attack by pests can send chemical signals through the mycorrhizal network to warn neighboring trees. These trees can then produce defensive chemicals to prepare for the impending threat.
### Benefits and Functions
1. **Resource Sharing**: The network allows for the redistribution of resources among plants. For instance, a large, established tree might share excess nutrients and water with smaller, younger trees, promoting overall forest health.
2. **Defense Mechanism**: The ability to share information about pests and diseases enhances the resilience of plant communities. This early warning system helps plants activate their defenses before they are directly affected.
3. **Support for Seedlings**: Young seedlings, which have limited root systems, benefit immensely from the mycorrhizal network. They receive nutrients and water from larger plants, increasing their chances of survival and growth.
### Ecological Impact
1. **Biodiversity**: The mycorrhizal network supports biodiversity by fostering a cooperative environment. Plants of different species can coexist and thrive because of the shared resources and information.
2. **Forest Health**: The network enhances the overall health of forests. By enabling efficient nutrient cycling and supporting plant defenses, it contributes to the stability and longevity of forest ecosystems.
3. **Climate Change Mitigation**: Healthy forests act as significant carbon sinks, absorbing carbon dioxide from the atmosphere. The mycorrhizal network plays a critical role in maintaining forest health and, consequently, in mitigating climate change.
### Research and Discoveries
1. **Suzanne Simard's Work**: Ecologist Suzanne Simard’s research has been pivotal in uncovering the complexities of the mycorrhizal network. She demonstrated that trees of different species can share resources and that "mother trees" (large, older trees) play a crucial role in nurturing younger plants.
2. **Implications for Conservation**: Understanding the mycorrhizal network has significant implications for conservation efforts. It highlights the importance of preserving not just individual trees but entire ecosystems, including the fungal networks that sustain them.
### Practical Applications
1. **Agriculture**: Farmers and horticulturists are exploring the use of mycorrhizal fungi to improve crop yields and soil health. By incorporating these fungi into agricultural practices, they can reduce the need for chemical fertilizers and enhance plant resilience.
2. **Reforestation**: In reforestation projects, introducing mycorrhizal fungi can accelerate the recovery of degraded lands. The fungi help establish healthy plant communities, ensuring the success of newly planted trees.
The "Wood Wide Web" exemplifies the intricate and often hidden connections that sustain life on Earth. It’s a reminder of the profound interdependence within natural systems and the importance of preserving these delicate relationships.
"""


def random_plot():
df = px.data.iris()
Expand All @@ -28,7 +70,7 @@ def random_plot():

def html_src(harm_level):
return f"""
<div style="display: flex; gap: 5px;padding: 2px 4px;margin-top: -40px">
<div style="display: flex; gap: 5px;">
<div style="background-color: {color_map[harm_level]}; padding: 2px; border-radius: 5px;">
{harm_level}
</div>
Expand Down Expand Up @@ -149,7 +191,7 @@ def bot(history, response_type):
html_src(random.choice(["harmful", "neutral", "beneficial"]))
)
else:
history[-1][1] = "Cool!"
history[-1][1] = txt
return history


Expand All @@ -160,6 +202,12 @@ def bot(history, response_type):
elem_id="chatbot",
bubble_full_width=False,
scale=1,
show_copy_button=True,
avatar_images=(
None, # os.path.join("files", "avatar.png"),
os.path.join("files", "avatar.png"),
),
# layout="panel",
)
response_type = gr.Radio(
[
Expand Down
6 changes: 6 additions & 0 deletions js/audio/player/AudioPlayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
pause: undefined;
edit: undefined;
end: undefined;
load: undefined;
}>();
const create_waveform = (): void => {
Expand Down Expand Up @@ -98,6 +99,10 @@
dispatch("play");
});
$: waveform?.on("load", () => {
dispatch("load");
});
const handle_trim_audio = async (
start: number,
end: number
Expand Down Expand Up @@ -149,6 +154,7 @@
src={value.url}
controls
autoplay={waveform_settings.autoplay}
on:load
/>
{:else}
<div
Expand Down
1 change: 1 addition & 0 deletions js/audio/static/StaticAudio.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
on:pause
on:play
on:stop
on:load
/>
{:else}
<Empty size="small">
Expand Down
4 changes: 2 additions & 2 deletions js/chatbot/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
export let likeable = false;
export let show_share_button = false;
export let rtl = false;
export let show_copy_button = false;
export let show_copy_button = true;
export let sanitize_html = true;
export let bubble_full_width = true;
export let layout: "bubble" | "panel" = "bubble";
Expand Down Expand Up @@ -88,7 +88,7 @@
<BlockLabel
{show_label}
Icon={Chat}
float={false}
float={true}
label={label || "Chatbot"}
/>
{/if}
Expand Down
1 change: 1 addition & 0 deletions js/chatbot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@gradio/theme": "workspace:^",
"@gradio/upload": "workspace:^",
"@gradio/utils": "workspace:^",
"@gradio/wasm": "workspace:^",
"@types/dompurify": "^3.0.2",
"@types/katex": "^0.16.0",
"@types/prismjs": "1.26.4",
Expand Down
101 changes: 101 additions & 0 deletions js/chatbot/shared/ButtonPanel.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<script lang="ts">
import LikeDislike from "./LikeDislike.svelte";
import Copy from "./Copy.svelte";
import type { FileData } from "@gradio/client";
import DownloadIcon from "./Download.svelte";
import { DownloadLink } from "@gradio/wasm/svelte";
export let likeable: boolean;
export let show_copy_button: boolean;
export let show: boolean;
export let message: Record<string, any>;
export let position: "right" | "left";
export let avatar: FileData | null;
export let show_download = false;
export let handle_action: (selected: string | null) => void;
export let layout: "bubble" | "panel";
$: show_copy = show_copy_button && message && message?.type === "text";
$: show_download =
(show_download && message?.value?.video?.url) || message?.value?.url;
</script>

{#if show}
<div
class="message-buttons-{position} {layout} message-buttons {avatar !==
null && 'with-avatar'}"
>
{#if show_copy}
<Copy value={message.value} />
{/if}
{#if show_download}
<DownloadLink
href={message?.value?.video?.url || message?.value?.url}
download={message?.value?.video?.orig_name ||
message.value.orig_name ||
"image"}
>
<span class="icon-wrap">
<DownloadIcon />
</span>
</DownloadLink>
{/if}
{#if likeable && position === "left"}
<LikeDislike {handle_action} padded={show_copy || show_download} />
{/if}
</div>
{/if}

<style>
.icon-wrap {
display: block;
color: var(--body-text-color-subdued);
}
.icon-wrap:hover {
color: var(--body-text-color);
}
.message-buttons-right,
.message-buttons-left {
border-radius: var(--radius-md);
display: flex;
align-items: center;
bottom: 0;
height: var(--size-7);
align-self: self-end;
/* position: absolute; */
bottom: -15px;
margin: 2px;
padding-left: 5px;
z-index: 1;
padding-bottom: var(--spacing-xl);
padding: var(--spacing-md) 2px;
}
.message-buttons-left {
align-self: start;
left: 0px;
}
.message-buttons-right {
right: 5px;
}
.panel.message-buttons-left,
.panel.message-buttons-right {
margin: 10px 0 2px 0;
}
.message-buttons {
left: 0px;
right: 0px;
top: unset;
bottom: calc(-30px - var(--spacing-xl));
display: flex;
justify-content: flex-start;
align-items: center;
gap: 0px;
}
.message-buttons :global(> *) {
margin-right: 7px;
}
</style>
Loading

0 comments on commit 530f8a0

Please sign in to comment.