freddyaboulton HF Staff commited on
Commit
5cbccd9
·
verified ·
1 Parent(s): e2eb458

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +6 -6
  2. run.ipynb +1 -0
  3. run.py +81 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Chatinterface Nested Thoughts
3
- emoji: 🦀
4
- colorFrom: red
5
  colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 5.12.0
8
- app_file: app.py
9
  pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: chatinterface_nested_thoughts
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 5.12.0
9
+ app_file: run.py
10
  pinned: false
11
+ hf_oauth: true
12
  ---
 
 
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_nested_thoughts"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from gradio import ChatMessage\n", "import time\n", "\n", "sleep_time = 0.1\n", "long_sleep_time = 1\n", "\n", "def generate_response(message, history):\n", " start_time = time.time()\n", " responses = [\n", " ChatMessage(\n", " content=\"In order to find the current weather in San Francisco, I will need to use my weather tool.\",\n", " )\n", " ]\n", " yield responses\n", " time.sleep(sleep_time)\n", "\n", " main_thought = ChatMessage(\n", " content=\"\",\n", " metadata={\"title\": \"Using Weather Tool\", \"id\": 1, \"status\": \"pending\"},\n", " )\n", "\n", " responses.append(main_thought)\n", "\n", " yield responses\n", " time.sleep(long_sleep_time)\n", " responses[-1].content = \"Will check: weather.com and sunny.org\"\n", " yield responses\n", " time.sleep(sleep_time)\n", " responses.append(\n", " ChatMessage(\n", " content=\"Received weather from weather.com.\",\n", " metadata={\"title\": \"Checking weather.com\", \"parent_id\": 1, \"id\": 2, \"duration\": 0.05},\n", " )\n", " )\n", " yield responses\n", "\n", " sunny_start_time = time.time()\n", " time.sleep(sleep_time)\n", " sunny_thought = ChatMessage(\n", " content=\"API Error when connecting to sunny.org \ud83d\udca5\",\n", " metadata={\"title\": \"Checking sunny.org\", \"parent_id\": 1, \"id\": 3, \"status\": \"pending\"},\n", " )\n", "\n", " responses.append(sunny_thought)\n", " yield responses\n", "\n", " time.sleep(sleep_time)\n", " responses.append(\n", " ChatMessage(\n", " content=\"Failed again\",\n", " metadata={\"title\": \"I will try again\", \"id\": 4, \"parent_id\": 3, \"duration\": 0.1},\n", "\n", " )\n", " )\n", " sunny_thought.metadata[\"status\"] = \"done\"\n", " sunny_thought.metadata[\"duration\"] = time.time() - sunny_start_time\n", "\n", " main_thought.metadata[\"status\"] = \"done\"\n", " main_thought.metadata[\"duration\"] = time.time() - start_time\n", "\n", " yield responses\n", "\n", " time.sleep(long_sleep_time)\n", "\n", " responses.append(\n", " ChatMessage(\n", " content=\"Based on the data only from weather.com, the current weather in San Francisco is 60 degrees and sunny.\",\n", " )\n", " )\n", " yield responses\n", "\n", "demo = gr.ChatInterface(\n", " generate_response,\n", " type=\"messages\",\n", " title=\"Nested Thoughts Chat Interface\",\n", " examples=[\"What is the weather in San Francisco right now?\"]\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio import ChatMessage
3
+ import time
4
+
5
+ sleep_time = 0.1
6
+ long_sleep_time = 1
7
+
8
+ def generate_response(message, history):
9
+ start_time = time.time()
10
+ responses = [
11
+ ChatMessage(
12
+ content="In order to find the current weather in San Francisco, I will need to use my weather tool.",
13
+ )
14
+ ]
15
+ yield responses
16
+ time.sleep(sleep_time)
17
+
18
+ main_thought = ChatMessage(
19
+ content="",
20
+ metadata={"title": "Using Weather Tool", "id": 1, "status": "pending"},
21
+ )
22
+
23
+ responses.append(main_thought)
24
+
25
+ yield responses
26
+ time.sleep(long_sleep_time)
27
+ responses[-1].content = "Will check: weather.com and sunny.org"
28
+ yield responses
29
+ time.sleep(sleep_time)
30
+ responses.append(
31
+ ChatMessage(
32
+ content="Received weather from weather.com.",
33
+ metadata={"title": "Checking weather.com", "parent_id": 1, "id": 2, "duration": 0.05},
34
+ )
35
+ )
36
+ yield responses
37
+
38
+ sunny_start_time = time.time()
39
+ time.sleep(sleep_time)
40
+ sunny_thought = ChatMessage(
41
+ content="API Error when connecting to sunny.org 💥",
42
+ metadata={"title": "Checking sunny.org", "parent_id": 1, "id": 3, "status": "pending"},
43
+ )
44
+
45
+ responses.append(sunny_thought)
46
+ yield responses
47
+
48
+ time.sleep(sleep_time)
49
+ responses.append(
50
+ ChatMessage(
51
+ content="Failed again",
52
+ metadata={"title": "I will try again", "id": 4, "parent_id": 3, "duration": 0.1},
53
+
54
+ )
55
+ )
56
+ sunny_thought.metadata["status"] = "done"
57
+ sunny_thought.metadata["duration"] = time.time() - sunny_start_time
58
+
59
+ main_thought.metadata["status"] = "done"
60
+ main_thought.metadata["duration"] = time.time() - start_time
61
+
62
+ yield responses
63
+
64
+ time.sleep(long_sleep_time)
65
+
66
+ responses.append(
67
+ ChatMessage(
68
+ content="Based on the data only from weather.com, the current weather in San Francisco is 60 degrees and sunny.",
69
+ )
70
+ )
71
+ yield responses
72
+
73
+ demo = gr.ChatInterface(
74
+ generate_response,
75
+ type="messages",
76
+ title="Nested Thoughts Chat Interface",
77
+ examples=["What is the weather in San Francisco right now?"]
78
+ )
79
+
80
+ if __name__ == "__main__":
81
+ demo.launch()