Files
wehub-resource-sync adf0d17497
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:17:32 +08:00

82 lines
2.2 KiB
Python

import gradio as gr
from gradio import ChatMessage
import time
sleep_time = 0.1
long_sleep_time = 1
def generate_response(message, history):
start_time = time.time()
responses = [
ChatMessage(
content="In order to find the current weather in San Francisco, I will need to use my weather tool.",
)
]
yield responses
time.sleep(sleep_time)
main_thought = ChatMessage(
content="",
metadata={"title": "Using Weather Tool", "id": 1, "status": "pending"},
)
responses.append(main_thought)
yield responses
time.sleep(long_sleep_time)
responses[-1].content = "Will check: weather.com and sunny.org"
yield responses
time.sleep(sleep_time)
responses.append(
ChatMessage(
content="Received weather from weather.com.",
metadata={"title": "Checking weather.com", "parent_id": 1, "id": 2, "duration": 0.05},
)
)
yield responses
sunny_start_time = time.time()
time.sleep(sleep_time)
sunny_thought = ChatMessage(
content="API Error when connecting to sunny.org 💥",
metadata={"title": "Checking sunny.org", "parent_id": 1, "id": 3, "status": "pending"},
)
responses.append(sunny_thought)
yield responses
time.sleep(sleep_time)
responses.append(
ChatMessage(
content="Failed again",
metadata={"title": "I will try again", "id": 4, "parent_id": 3, "duration": 0.1},
)
)
sunny_thought.metadata["status"] = "done"
sunny_thought.metadata["duration"] = time.time() - sunny_start_time
main_thought.metadata["status"] = "done"
main_thought.metadata["duration"] = time.time() - start_time
yield responses
time.sleep(long_sleep_time)
responses.append(
ChatMessage(
content="Based on the data only from weather.com, the current weather in San Francisco is 60 degrees and sunny.",
)
)
yield responses
demo = gr.ChatInterface(
generate_response,
title="Nested Thoughts Chat Interface",
examples=["What is the weather in San Francisco right now?"],
api_name="chat"
)
if __name__ == "__main__":
demo.launch()