-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.py
More file actions
113 lines (88 loc) · 3.96 KB
/
app.py
File metadata and controls
113 lines (88 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import gradio as gr
from commons.ThinkerInterface import Thinker
class Wrapper:
def __init__(self) -> None:
pass
async def thinker_wrapper_function(self, situation: str, thoughts: str) -> str:
"""
This function is an asynchronous function used to encapsulate the use of the Thinker class.
Parameters:
situation (str): Represents the current situation.
thoughts (str): Represents the current thoughts.
Returns:
str: A markdown string representing the process and suggestions.
"""
self.thinker: Thinker = Thinker(
situation=situation,
thoughts=thoughts
)
progress_log = "### 🧠 Thinking Process\n\n"
async for step, data in self.thinker.query_process():
progress_log += f"- {step}\n"
if data and "Brief" in step and isinstance(data, dict):
summary = data.get('summary', 'No summary available')
progress_log += f" > **Summary**: {summary}\n"
yield progress_log
# Final formatting of suggestions
suggestions_md = "\n### 💡 Suggestions\n\n"
suggestions_md += "| # | Suggestion | Rationale | Success Rate |\n"
suggestions_md += "|---|---|---|---|\n"
for i, suggestion in enumerate(self.thinker.the_suggestions):
move = suggestion.get('move', 'N/A')
rationale = suggestion.get('rationale', 'N/A')
success_rate = suggestion.get('success_rate_in_percentage', 'N/A')
# Escape pipe characters in content to avoid breaking the markdown table
move = str(move).replace('|', r'\|')
rationale = str(rationale).replace('|', r'\|')
suggestions_md += f"| {i} | {move} | {rationale} | {success_rate}% |\n"
# Tree Structure formatting
tree_md = "\n### 🌳 Decision Tree\n\n"
tree_md += "```json\n"
import json
tree_md += json.dumps(self.thinker.tree_structure, indent=2, ensure_ascii=False)
tree_md += "\n```\n"
yield progress_log + suggestions_md + tree_md
async def elaboration_wrapper_function(self, selected_suggestion: int) -> str:
"""
The function `elaboration_wrapper_function` takes in a selected suggestion and returns the
elaboration generated by the `think_process` method of the `thinker` object.
:param selected_suggestion: The `selected_suggestion` parameter is an integer that represents
the index of the suggestion that the user has selected. It is used as an input to the
`think_process` method of the `thinker` object
:type selected_suggestion: int
:return: a string, which is the elaboration generated by the `think_process` method of the
`thinker` object.
"""
elaboration: str = await self.thinker.think_process(selected_suggestion=selected_suggestion)
return elaboration
if __name__ == '__main__':
import logging
import os
logging.basicConfig(level=logging.INFO)
os.environ['TOKENIZERS_PARALLELISM'] = "false"
wrapper: Wrapper = Wrapper()
interface: gr.Interface = gr.Interface(
fn=wrapper.thinker_wrapper_function,
inputs=[
gr.components.Textbox(
label="Situation",
placeholder="Enter your situation here",
value="I am feeling tired"
),
gr.components.Textbox(
label="Thoughts",
placeholder="Enter your thoughts here",
value="I am feeling tired"
)
],
outputs=[
gr.Markdown()
],
title="Thinker",
description="A tool to help you think and make decisions",
article="This tool is a wrapper for the Thinker class, which is a class that helps you think and make decisions.",
examples=[
["I am feeling tired", "I am feeling tired"],
]
)
interface.launch()