Spaces:
Running
Running
Commit
•
cf545bf
1
Parent(s):
6efec2d
use gradio blocks and chatbot (#2)
Browse files- use gradio blocks and chatbot (f2b8faf5fd99c7a718e029d1eb2fecc2441ee9e3)
Co-authored-by: AK <akhaliq@users.noreply.huggingface.co>
app.py
CHANGED
@@ -31,7 +31,7 @@ def generate_response(api_key: str, prompt: str) -> Tuple[List[Tuple[str, str, f
|
|
31 |
Generate a response to the given prompt using a step-by-step reasoning approach.
|
32 |
"""
|
33 |
system_message = """You are an expert AI assistant that explains your reasoning step by step. For each step, provide a title that describes what you're doing in that step, along with the content. Decide if you need another step or if you're ready to give the final answer. Respond in JSON format with 'title', 'content', and 'next_action' (either 'continue' or 'final_answer') keys. USE AS MANY REASONING STEPS AS POSSIBLE. AT LEAST 3. BE AWARE OF YOUR LIMITATIONS AS AN LLM AND WHAT YOU CAN AND CANNOT DO. IN YOUR REASONING, INCLUDE EXPLORATION OF ALTERNATIVE ANSWERS. CONSIDER YOU MAY BE WRONG, AND IF YOU ARE WRONG IN YOUR REASONING, WHERE IT WOULD BE. FULLY TEST ALL OTHER POSSIBILITIES. YOU CAN BE WRONG. WHEN YOU SAY YOU ARE RE-EXAMINING, ACTUALLY RE-EXAMINE, AND USE ANOTHER APPROACH TO DO SO. DO NOT JUST SAY YOU ARE RE-EXAMINING. USE AT LEAST 3 METHODS TO DERIVE THE ANSWER. USE BEST PRACTICES."""
|
34 |
-
|
35 |
messages = [
|
36 |
{"role": "system", "content": system_message},
|
37 |
{"role": "user", "content": prompt},
|
@@ -51,7 +51,7 @@ def generate_response(api_key: str, prompt: str) -> Tuple[List[Tuple[str, str, f
|
|
51 |
steps.append((f"Step {step_count}: {step_data['title']}", step_data['content'], thinking_time))
|
52 |
messages.append({"role": "assistant", "content": json.dumps(step_data)})
|
53 |
|
54 |
-
if step_data
|
55 |
break
|
56 |
|
57 |
step_count += 1
|
@@ -63,34 +63,87 @@ def generate_response(api_key: str, prompt: str) -> Tuple[List[Tuple[str, str, f
|
|
63 |
thinking_time = time.time() - start_time
|
64 |
total_thinking_time += thinking_time
|
65 |
|
66 |
-
steps.append(("Final Answer", final_data
|
67 |
|
68 |
return steps, total_thinking_time
|
69 |
|
70 |
-
def generate_ui(api_key: str, prompt: str) -> str:
|
71 |
"""
|
72 |
Generate the UI output based on the response to the given prompt.
|
73 |
"""
|
74 |
steps, total_time = generate_response(api_key, prompt)
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
81 |
|
82 |
-
# Gradio Interface with
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
-
|
|
|
31 |
Generate a response to the given prompt using a step-by-step reasoning approach.
|
32 |
"""
|
33 |
system_message = """You are an expert AI assistant that explains your reasoning step by step. For each step, provide a title that describes what you're doing in that step, along with the content. Decide if you need another step or if you're ready to give the final answer. Respond in JSON format with 'title', 'content', and 'next_action' (either 'continue' or 'final_answer') keys. USE AS MANY REASONING STEPS AS POSSIBLE. AT LEAST 3. BE AWARE OF YOUR LIMITATIONS AS AN LLM AND WHAT YOU CAN AND CANNOT DO. IN YOUR REASONING, INCLUDE EXPLORATION OF ALTERNATIVE ANSWERS. CONSIDER YOU MAY BE WRONG, AND IF YOU ARE WRONG IN YOUR REASONING, WHERE IT WOULD BE. FULLY TEST ALL OTHER POSSIBILITIES. YOU CAN BE WRONG. WHEN YOU SAY YOU ARE RE-EXAMINING, ACTUALLY RE-EXAMINE, AND USE ANOTHER APPROACH TO DO SO. DO NOT JUST SAY YOU ARE RE-EXAMINING. USE AT LEAST 3 METHODS TO DERIVE THE ANSWER. USE BEST PRACTICES."""
|
34 |
+
|
35 |
messages = [
|
36 |
{"role": "system", "content": system_message},
|
37 |
{"role": "user", "content": prompt},
|
|
|
51 |
steps.append((f"Step {step_count}: {step_data['title']}", step_data['content'], thinking_time))
|
52 |
messages.append({"role": "assistant", "content": json.dumps(step_data)})
|
53 |
|
54 |
+
if step_data.get('next_action') == 'final_answer':
|
55 |
break
|
56 |
|
57 |
step_count += 1
|
|
|
63 |
thinking_time = time.time() - start_time
|
64 |
total_thinking_time += thinking_time
|
65 |
|
66 |
+
steps.append(("Final Answer", final_data.get('content', 'No final answer provided.'), thinking_time))
|
67 |
|
68 |
return steps, total_thinking_time
|
69 |
|
70 |
+
def generate_ui(api_key: str, prompt: str) -> Tuple[List[Tuple[str, str]], float]:
|
71 |
"""
|
72 |
Generate the UI output based on the response to the given prompt.
|
73 |
"""
|
74 |
steps, total_time = generate_response(api_key, prompt)
|
75 |
+
conversation = []
|
76 |
+
for title, content, _ in steps:
|
77 |
+
if title.startswith("Step"):
|
78 |
+
conversation.append(("Assistant", f"**{title}**\n\n{content}"))
|
79 |
+
elif title == "Final Answer":
|
80 |
+
conversation.append(("Assistant", f"**{title}**\n\n{content}"))
|
81 |
+
else:
|
82 |
+
conversation.append(("Assistant", content))
|
83 |
+
return conversation, total_time
|
84 |
|
85 |
+
# Gradio Blocks Interface with a Chatbot component and API key input
|
86 |
+
def main():
|
87 |
+
with gr.Blocks() as demo:
|
88 |
+
gr.Markdown("# o1-like Chain of Thought - LLaMA-3.1 70B on Cerebras")
|
89 |
+
gr.Markdown("""
|
90 |
+
Implement Chain of Thought with prompting to improve output accuracy.
|
91 |
+
Powered by Cerebras, ensuring fast reasoning steps.
|
92 |
+
""")
|
93 |
+
|
94 |
+
with gr.Row():
|
95 |
+
api_key_input = gr.Textbox(
|
96 |
+
label="Cerebras API Key",
|
97 |
+
type="password",
|
98 |
+
placeholder="Enter your Cerebras API key",
|
99 |
+
show_label=True
|
100 |
+
)
|
101 |
+
|
102 |
+
chatbot = gr.Chatbot(label="Conversation")
|
103 |
+
with gr.Row():
|
104 |
+
user_input = gr.Textbox(
|
105 |
+
label="Your Query",
|
106 |
+
placeholder="Enter your query here...",
|
107 |
+
show_label=True
|
108 |
+
)
|
109 |
+
submit_btn = gr.Button("Submit")
|
110 |
+
|
111 |
+
thinking_time_display = gr.Textbox(
|
112 |
+
label="Total Thinking Time",
|
113 |
+
value="",
|
114 |
+
interactive=False
|
115 |
+
)
|
116 |
+
|
117 |
+
def respond(api_key, message, history):
|
118 |
+
if not api_key:
|
119 |
+
return history, "Please provide a valid Cerebras API key."
|
120 |
+
|
121 |
+
steps, total_time = generate_response(api_key, message)
|
122 |
+
for title, content, _ in steps:
|
123 |
+
if title.startswith("Step"):
|
124 |
+
history.append(("Assistant", f"**{title}**\n\n{content}"))
|
125 |
+
elif title == "Final Answer":
|
126 |
+
history.append(("Assistant", f"**{title}**\n\n{content}"))
|
127 |
+
else:
|
128 |
+
history.append(("Assistant", content))
|
129 |
+
return history, f"**Total thinking time:** {total_time:.2f} seconds"
|
130 |
+
|
131 |
+
submit_btn.click(
|
132 |
+
fn=respond,
|
133 |
+
inputs=[api_key_input, user_input, chatbot],
|
134 |
+
outputs=[chatbot, thinking_time_display],
|
135 |
+
queue=True
|
136 |
+
)
|
137 |
+
|
138 |
+
# Optional: Allow pressing Enter to submit
|
139 |
+
user_input.submit(
|
140 |
+
fn=respond,
|
141 |
+
inputs=[api_key_input, user_input, chatbot],
|
142 |
+
outputs=[chatbot, thinking_time_display],
|
143 |
+
queue=True
|
144 |
+
)
|
145 |
+
|
146 |
+
demo.launch()
|
147 |
|
148 |
if __name__ == "__main__":
|
149 |
+
main()
|