freddyaboulton HF staff commited on
Commit
1ee46d1
·
verified ·
1 Parent(s): 7b1c2f5

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 +71 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Chatbot Thoughts
3
- emoji:
4
- colorFrom: gray
5
  colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 5.11.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: chatbot_thoughts
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 5.11.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: chatbot_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", "def simulate_thinking_chat(message: str, history: list):\n", " \"\"\"Mimicking thinking process and response\"\"\"\n", " # Add initial empty thinking message to chat history\n", "\n", " history.append( # Adds new message to the chat history list\n", " ChatMessage( # Creates a new chat message\n", " role=\"assistant\", # Specifies this is from the assistant\n", " content=\"\", # Initially empty content\n", " metadata={\"title\": \"Thinking... \"} # Setting a thinking header here\n", " )\n", " )\n", " time.sleep(0.5)\n", " yield history # Returns current state of chat history\n", " \n", " # Define the thoughts that LLM will \"think\" through\n", " thoughts = [\n", " \"First, I need to understand the core aspects of the query...\",\n", " \"Now, considering the broader context and implications...\",\n", " \"Analyzing potential approaches to formulate a comprehensive answer...\",\n", " \"Finally, structuring the response for clarity and completeness...\"\n", " ]\n", " \n", " # Variable to store all thoughts as they accumulate\n", " accumulated_thoughts = \"\"\n", " \n", " # Loop through each thought\n", " for thought in thoughts:\n", " time.sleep(0.5) # Add a samll delay for realism\n", " \n", " # Add new thought to accumulated thoughts with markdown bullet point\n", " accumulated_thoughts += f\"- {thought}\\n\\n\" # \\n\\n creates line breaks\n", " \n", " # Update the thinking message with all thoughts so far\n", " history[-1] = ChatMessage( # Updates last message in history\n", " role=\"assistant\",\n", " content=accumulated_thoughts.strip(), # Remove extra whitespace\n", " metadata={\"title\": \"Thinking...\"} # Shows thinking header\n", " )\n", " yield history # Returns updated chat history\n", " \n", " # After thinking is complete, adding the final response\n", " history.append(\n", " ChatMessage(\n", " role=\"assistant\",\n", " content=\"Based on my thoughts and analysis above, my response is: This dummy repro shows how thoughts of a thinking LLM can be progressively shown before providing its final answer.\"\n", " )\n", " )\n", " yield history # Returns final state of chat history\n", "\n", "# Gradio blocks with gr.chatbot\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"# Thinking LLM Demo \ud83e\udd14\")\n", " chatbot = gr.Chatbot(type=\"messages\", render_markdown=True)\n", " msg = gr.Textbox(placeholder=\"Type your message...\")\n", " \n", " msg.submit(\n", " lambda m, h: (m, h + [ChatMessage(role=\"user\", content=m)]),\n", " [msg, chatbot],\n", " [msg, chatbot]\n", " ).then(\n", " simulate_thinking_chat,\n", " [msg, chatbot],\n", " chatbot\n", " )\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio import ChatMessage
3
+ import time
4
+
5
+ def simulate_thinking_chat(message: str, history: list):
6
+ """Mimicking thinking process and response"""
7
+ # Add initial empty thinking message to chat history
8
+
9
+ history.append( # Adds new message to the chat history list
10
+ ChatMessage( # Creates a new chat message
11
+ role="assistant", # Specifies this is from the assistant
12
+ content="", # Initially empty content
13
+ metadata={"title": "Thinking... "} # Setting a thinking header here
14
+ )
15
+ )
16
+ time.sleep(0.5)
17
+ yield history # Returns current state of chat history
18
+
19
+ # Define the thoughts that LLM will "think" through
20
+ thoughts = [
21
+ "First, I need to understand the core aspects of the query...",
22
+ "Now, considering the broader context and implications...",
23
+ "Analyzing potential approaches to formulate a comprehensive answer...",
24
+ "Finally, structuring the response for clarity and completeness..."
25
+ ]
26
+
27
+ # Variable to store all thoughts as they accumulate
28
+ accumulated_thoughts = ""
29
+
30
+ # Loop through each thought
31
+ for thought in thoughts:
32
+ time.sleep(0.5) # Add a samll delay for realism
33
+
34
+ # Add new thought to accumulated thoughts with markdown bullet point
35
+ accumulated_thoughts += f"- {thought}\n\n" # \n\n creates line breaks
36
+
37
+ # Update the thinking message with all thoughts so far
38
+ history[-1] = ChatMessage( # Updates last message in history
39
+ role="assistant",
40
+ content=accumulated_thoughts.strip(), # Remove extra whitespace
41
+ metadata={"title": "Thinking..."} # Shows thinking header
42
+ )
43
+ yield history # Returns updated chat history
44
+
45
+ # After thinking is complete, adding the final response
46
+ history.append(
47
+ ChatMessage(
48
+ role="assistant",
49
+ content="Based on my thoughts and analysis above, my response is: This dummy repro shows how thoughts of a thinking LLM can be progressively shown before providing its final answer."
50
+ )
51
+ )
52
+ yield history # Returns final state of chat history
53
+
54
+ # Gradio blocks with gr.chatbot
55
+ with gr.Blocks() as demo:
56
+ gr.Markdown("# Thinking LLM Demo 🤔")
57
+ chatbot = gr.Chatbot(type="messages", render_markdown=True)
58
+ msg = gr.Textbox(placeholder="Type your message...")
59
+
60
+ msg.submit(
61
+ lambda m, h: (m, h + [ChatMessage(role="user", content=m)]),
62
+ [msg, chatbot],
63
+ [msg, chatbot]
64
+ ).then(
65
+ simulate_thinking_chat,
66
+ [msg, chatbot],
67
+ chatbot
68
+ )
69
+
70
+ if __name__ == "__main__":
71
+ demo.launch()