File size: 24,836 Bytes
a707ccd 361c4d3 ea0faa1 361c4d3 b9d6d53 a707ccd ccc6355 b9d6d53 6a12f54 ccc6355 6a12f54 b9d6d53 ccc6355 b9d6d53 ccc6355 6a12f54 07a46f8 7714f74 b27451f 7714f74 ccc6355 07a46f8 ccc6355 07a46f8 7714f74 07a46f8 7714f74 b27451f ccc6355 7714f74 07a46f8 ccc6355 7714f74 ccc6355 07a46f8 ccc6355 07a46f8 7714f74 07a46f8 7714f74 07a46f8 ccc6355 07a46f8 ccc6355 07a46f8 ea0faa1 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 ea0faa1 07a46f8 d858dc3 07a46f8 5138a85 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 ccc6355 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 07a46f8 b9d6d53 ea0faa1 ccc6355 b9d6d53 ccc6355 07a46f8 ccc6355 b9d6d53 ccc6355 b9d6d53 ccc6355 07a46f8 ccc6355 07a46f8 b9d6d53 ccc6355 07a46f8 b9d6d53 07a46f8 ccc6355 07a46f8 ccc6355 5138a85 ccc6355 07a46f8 361c4d3 ccc6355 361c4d3 ccc6355 361c4d3 ccc6355 07a46f8 ccc6355 361c4d3 ccc6355 361c4d3 ccc6355 361c4d3 ccc6355 07a46f8 ccc6355 07a46f8 ccc6355 07a46f8 ccc6355 b9d6d53 ccc6355 07a46f8 ccc6355 07a46f8 ccc6355 07a46f8 ccc6355 07a46f8 ccc6355 07a46f8 ccc6355 07a46f8 ccc6355 ea0faa1 07a46f8 |
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
import gradio as gr
import os
import spaces # Import the spaces library
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer
import torch
from threading import Thread
import logging
from typing import Tuple, List, Dict, Generator
# --- Logging Configuration ---
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# --- Model & Quantization Settings ---
MODEL_ID = "unsloth/DeepSeek-R1-Distill-Qwen-7B-unsloth-bnb-4bit"
models: Dict[str, AutoModelForCausalLM] = {}
tokenizers: Dict[str, AutoTokenizer] = {}
bnb_config_4bit = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16, # Or torch.float16 if needed
)
def get_model_and_tokenizer() -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
"""
Lazy-load the model and tokenizer if not already loaded.
Returns:
Tuple[model, tokenizer]: The loaded model and tokenizer.
"""
if "7B" not in models:
logging.info(f"Loading 7B model: {MODEL_ID} on demand")
try:
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
quantization_config=bnb_config_4bit,
torch_dtype=torch.bfloat16, # Or torch.float16 if needed
device_map='auto',
trust_remote_code=True,
)
model.eval() # Set the model to evaluation mode
models["7B"] = model
tokenizers["7B"] = tokenizer
logging.info("Loaded 7B model on demand.")
except Exception as e:
logging.error(f"Failed to load model and tokenizer: {e}")
raise e
return models["7B"], tokenizers["7B"]
# --- Default Prompt Templates for Multiple Presets ---
default_prompts = {
"coding": {
"brainstorm": """**Coding Brainstorm (Round 1)**
As a Senior Code Analyst, analyze the following problem and list key challenges and potential approaches.
**User Request:**
{user_prompt}
**Guidelines:**
1. Identify coding challenges.
2. Suggest potential methods and approaches.
3. Highlight any critical edge cases.
""",
"round2": """**Advanced Reasoning & Code Generation (Round 2)**
Based on your initial analysis:
**Initial Analysis:**
{brainstorm_response}
**User Request:**
{user_prompt}
**Task:**
1. Generate production-ready code with advanced reasoning.
2. Include a pun-filled birthday message with a coding twist within your output.
3. Comment the code clearly.
""",
"synthesis": """**Synthesis & Final Refinement (Round 3)**
Review the detailed code and reasoning below, and synthesize a final, refined response that:
1. Combines the brainstorming insights and advanced code generation.
2. Summarizes the solution succinctly.
3. Provides any additional improvements.
**Detailed Code & Reasoning:**
{round2_response}
""",
"rationale": """**Pun Generation and Rationale (Round 4)**
Based on the final refined response below, generate a clear, stand-alone pun-filled birthday message with a coding twist, then explain in detail why that pun was chosen.
Final Refined Response:
{final_response}
Your answer should:
1. Clearly output the pun as a separate line.
2. Explain the pun’s connection to birthdays and coding concepts (e.g., binary, syntax).
3. Describe any creative insights behind the choice.
"""
},
"math": {
"brainstorm": """**Math Problem Brainstorm (Round 1)**
As an expert mathematician, analyze the following problem and outline key concepts and strategies.
**Problem:**
{user_prompt}
**Guidelines:**
1. Identify the mathematical concepts involved.
2. List potential strategies or methods.
3. Note any assumptions or conditions.
""",
"round2": """**Solution Strategy Development (Round 2)**
Based on the initial analysis:
**Initial Analysis:**
{brainstorm_response}
**Problem:**
{user_prompt}
**Task:**
1. Develop a detailed strategy to solve the problem.
2. Include potential methods and intermediate steps.
""",
"synthesis": """**Solution Synthesis (Round 3)**
Review the strategy and previous analysis below, and produce a refined, step-by-step solution that:
1. Clearly explains the solution path.
2. Highlights key steps and justifications.
3. Summarizes the final answer.
**Detailed Strategy:**
{round2_response}
""",
"rationale": """**Solution Rationale (Round 4)**
Based on the final refined solution below, provide a detailed explanation of the key steps and mathematical insights.
Final Refined Solution:
{final_response}
Your response should:
1. Clearly explain why each step was taken.
2. Detail any assumptions and mathematical principles used.
3. Summarize the creative reasoning behind the solution.
"""
},
"writing": {
"brainstorm": """**Creative Brainstorm (Round 1)**
As a seasoned writer, brainstorm creative ideas for the following writing prompt.
**Writing Prompt:**
{user_prompt}
**Guidelines:**
1. List key themes and creative directions.
2. Suggest multiple approaches to the narrative.
3. Highlight any unique stylistic ideas.
""",
"round2": """**Outline Generation (Round 2)**
Based on the brainstorming below:
**Brainstormed Ideas:**
{brainstorm_response}
**Writing Prompt:**
{user_prompt}
**Task:**
1. Generate a detailed outline for a creative piece.
2. Organize the ideas into a coherent structure.
3. Provide bullet points or sections for the narrative.
""",
"synthesis": """**Draft Writing (Round 3)**
Review the outline below and produce a refined draft of the creative piece that:
1. Synthesizes the brainstorming insights and the outline.
2. Provides a coherent and engaging narrative.
3. Includes stylistic and thematic elements.
**Outline:**
{round2_response}
""",
"rationale": """**Final Editing and Rationale (Round 4)**
Based on the final draft below, refine the piece further and provide a detailed explanation of your creative choices.
Final Draft:
{final_response}
Your answer should:
1. Present the final refined text.
2. Explain the narrative choices, stylistic decisions, and thematic connections.
3. Detail any creative insights that influenced the final version.
"""
}
}
# --- Domain Detection ---
def detect_domain(user_prompt: str) -> str:
"""
Detect the domain based on keywords.
Args:
user_prompt (str): The user query.
Returns:
str: One of 'math', 'writing', or 'coding' (defaulting to coding).
"""
prompt_lower = user_prompt.lower()
math_keywords = ["solve", "integral", "derivative", "equation", "proof", "calculate", "sum", "product"]
writing_keywords = ["write", "story", "essay", "novel", "poem", "article", "narrative", "creative"]
coding_keywords = ["code", "program", "debug", "compile", "algorithm", "function"]
if any(kw in prompt_lower for kw in math_keywords):
logging.info("Domain detected as: math")
return "math"
elif any(kw in prompt_lower for kw in writing_keywords):
logging.info("Domain detected as: writing")
return "writing"
elif any(kw in prompt_lower for kw in coding_keywords):
logging.info("Domain detected as: coding")
return "coding"
else:
logging.info("No specific domain detected; defaulting to coding")
return "coding"
# --- Memory Management ---
class MemoryManager:
"""Encapsulate shared memory for storing and retrieving conversation items."""
def __init__(self) -> None:
self.shared_memory: List[str] = []
def store(self, item: str) -> None:
"""Store a memory item and log an excerpt."""
self.shared_memory.append(item)
logging.info(f"[Memory Stored]: {item[:50]}...")
def retrieve(self, query: str, top_k: int = 3) -> List[str]:
"""Retrieve recent memory items containing the query text."""
query_lower = query.lower()
relevant = [item for item in self.shared_memory if query_lower in item.lower()]
if not relevant:
logging.info("[Memory Retrieval]: No relevant memories found.")
else:
logging.info(f"[Memory Retrieval]: Found {len(relevant)} relevant memories.")
return relevant[-top_k:]
global_memory_manager = MemoryManager()
# --- Unified Generation Function ---
def generate_response(model, tokenizer, prompt: str, max_tokens: int, temperature: float, top_p: float) -> str:
"""Generate a response for a given prompt."""
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
kwargs = dict(
input_ids=input_ids,
streamer=streamer,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
do_sample=True,
)
thread = Thread(target=model.generate, kwargs=kwargs)
with torch.no_grad():
thread.start()
response = ""
try:
for text in streamer:
response += text
except Exception as e:
logging.error(f"Error during generation: {e}")
raise e
thread.join()
return response
# --- Multi-Round Agent Class ---
class MultiRoundAgent:
"""
Encapsulate the multi-round prompt chaining and response generation.
This class runs a 4-round pipeline based on the given preset.
"""
def __init__(self, model, tokenizer, prompt_templates: Dict[str, str], memory_manager: MemoryManager):
self.model = model
self.tokenizer = tokenizer
self.prompt_templates = prompt_templates
self.memory_manager = memory_manager
def run_pipeline(self, user_prompt: str, params: Dict, show_raw: bool = False) -> Generator[str, None, None]:
# Round 1: Brainstorming / Analysis
logging.info("--- Round 1 ---")
prompt_r1 = self.prompt_templates["brainstorm"].format(user_prompt=user_prompt)
r1 = generate_response(self.model, self.tokenizer, prompt_r1, params.get("max_new_tokens"), params.get("temp"), params.get("top_p"))
self.memory_manager.store(f"Round 1 Response: {r1}")
# Round 2: Secondary Generation (strategy/outline/code)
logging.info("--- Round 2 ---")
prompt_r2 = self.prompt_templates["round2"].format(brainstorm_response=r1, user_prompt=user_prompt)
r2 = generate_response(self.model, self.tokenizer, prompt_r2, params.get("max_new_tokens") + 100, params.get("temp"), params.get("top_p"))
self.memory_manager.store(f"Round 2 Response: {r2}")
# Round 3: Synthesis & Refinement (streaming updates)
logging.info("--- Round 3 ---")
prompt_r3 = self.prompt_templates["synthesis"].format(round2_response=r2)
input_ids_r3 = self.tokenizer.encode(prompt_r3, return_tensors="pt").to(self.model.device)
streamer_r3 = TextIteratorStreamer(self.tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
kwargs_r3 = dict(
input_ids=input_ids_r3,
streamer=streamer_r3,
max_new_tokens=params.get("max_new_tokens") // 2,
temperature=params.get("temp"),
top_p=params.get("top_p")
)
thread_r3 = Thread(target=self.model.generate, kwargs=kwargs_r3)
with torch.no_grad():
thread_r3.start()
r3 = ""
try:
for text in streamer_r3:
r3 += text
yield r3 # Yield progressive updates from Round 3
except Exception as e:
logging.error(f"Error during Round 3 streaming: {e}")
raise e
thread_r3.join()
self.memory_manager.store(f"Final Synthesis Response: {r3}")
# Round 4: Rationale / Final Output
logging.info("--- Round 4 ---")
prompt_r4 = self.prompt_templates["rationale"].format(final_response=r3)
r4 = generate_response(self.model, self.tokenizer, prompt_r4, 300, params.get("temp"), params.get("top_p"))
self.memory_manager.store(f"Round 4 Response: {r4}")
# Construct final output based on the show_raw flag.
if show_raw:
final_output = (
f"{r4}\n\n[Raw Outputs]\n"
f"Round 1:\n{r1}\n\n"
f"Round 2:\n{r2}\n\n"
f"Round 3:\n{r3}\n\n"
f"Round 4:\n{r4}\n"
)
else:
final_output = r4
yield final_output
# --- Swarm Agent Iterative Function ---
@spaces.GPU(duration=180) # Adjust duration as needed
def swarm_agent_iterative(user_prompt: str, temp: float, top_p: float, max_new_tokens: int, memory_top_k: int,
prompt_templates: Dict[str, str], domain: str, show_raw: bool) -> Generator[str, None, None]:
"""
Wraps the multi-round agent functionality. Depending on the detected domain,
it runs the 4-round pipeline.
"""
model, tokenizer = get_model_and_tokenizer()
agent = MultiRoundAgent(model, tokenizer, prompt_templates, global_memory_manager)
params = {"temp": temp, "top_p": top_p, "max_new_tokens": max_new_tokens}
return agent.run_pipeline(user_prompt, params, show_raw)
# --- Explanation Function for Additional Requests ---
def handle_explanation_request(user_prompt: str, history: List) -> str:
"""
Retrieve stored rationale and additional context from conversation history,
then generate an explanation.
"""
retrieved = global_memory_manager.retrieve("Round 4 Response:", top_k=3)
explanation_prompt = "Below are previous final outputs and related context from our conversation:\n"
if retrieved:
for item in retrieved:
explanation_prompt += f"- {item}\n"
else:
explanation_prompt += "No stored final output found.\n"
explanation_prompt += "\nRecent related exchanges:\n"
for chat in history:
if ("explain" in chat[0].lower()) or (chat[1] and "explain" in chat[1].lower()):
explanation_prompt += f"User: {chat[0]}\nAssistant: {chat[1]}\n"
explanation_prompt += "\nBased on the above context, please provide a detailed explanation of the creative choices."
model, tokenizer = get_model_and_tokenizer()
explanation = generate_response(model, tokenizer, explanation_prompt, 300, 0.7, 0.9)
return explanation
# --- Helper to Format History ---
def format_history(history: List) -> List[Dict[str, str]]:
"""
Convert history (list of [user, assistant] pairs) into a list of message dictionaries.
"""
messages = []
for item in history:
if isinstance(item, (list, tuple)) and len(item) == 2:
user_msg, assistant_msg = item
messages.append({"role": "user", "content": user_msg})
if assistant_msg:
messages.append({"role": "assistant", "content": assistant_msg})
elif isinstance(item, dict):
messages.append(item)
return messages
# --- Gradio Chat Interface Function ---
def gradio_interface(message: str, history: List, param_state: Dict, prompt_state: Dict) -> Generator[List[Dict[str, str]], None, None]:
"""
Called by Gradio's ChatInterface. Uses current generation parameters and preset prompt templates.
If the user asks for an explanation, routes the request accordingly.
"""
if "explain" in message.lower():
explanation = handle_explanation_request(message, history)
history = history + [[message, explanation]]
yield format_history(history)
return
try:
temp = float(param_state.get("temperature", 0.5))
top_p = float(param_state.get("top_p", 0.9))
max_new_tokens = int(param_state.get("max_new_tokens", 300))
memory_top_k = int(param_state.get("memory_top_k", 2))
show_raw = bool(param_state.get("show_raw_output", False))
except Exception as e:
logging.error(f"Parameter conversion error: {e}")
temp, top_p, max_new_tokens, memory_top_k, show_raw = 0.5, 0.9, 300, 2, False
domain = detect_domain(message)
# Get the prompt templates for the detected domain; default to coding if not set.
prompt_templates = prompt_state.get(domain, default_prompts.get(domain, default_prompts["coding"]))
history = history + [[message, ""]]
for partial_response in swarm_agent_iterative(
user_prompt=message,
temp=temp,
top_p=top_p,
max_new_tokens=max_new_tokens,
memory_top_k=memory_top_k,
prompt_templates=prompt_templates,
domain=domain,
show_raw=show_raw
):
history[-1][1] = partial_response
yield format_history(history)
# --- UI Settings & Styling ---
ui_description = '''
<div>
<h1 style="text-align: center;">DeepSeek Agent Swarm Chat</h1>
<p style="text-align: center;">
Multi-round agent with 4-round prompt chaining for three presets:
<br>- Coding
<br>- Math
<br>- Writing
</p>
</div>
'''
ui_license = """
<p/>
---
"""
ui_placeholder = """
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">DeepSeek Agent Swarm</h1>
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
</div>
"""
css = """
h1 {
text-align: center;
display: block;
}
#duplicate-button {
margin: auto;
color: white;
background: #1565c0;
border-radius: 100vh;
}
"""
# --- Gradio UI ---
with gr.Blocks(css=css, title="DeepSeek Agent Swarm Chat") as demo:
gr.Markdown(ui_description)
# Hidden states for parameters and prompt configurations.
param_state = gr.State({
"temperature": 0.5,
"top_p": 0.9,
"max_new_tokens": 300,
"memory_top_k": 2,
"show_raw_output": False, # New parameter for raw output
})
prompt_state = gr.State({
"coding": default_prompts["coding"],
"math": default_prompts["math"],
"writing": default_prompts["writing"],
})
with gr.Tabs():
with gr.Tab("Chat"):
chatbot = gr.Chatbot(height=450, placeholder=ui_placeholder, label="Agent Swarm Output", type="messages")
gr.ChatInterface(
fn=gradio_interface,
chatbot=chatbot,
additional_inputs=[param_state, prompt_state],
examples=[
['How can we build a robust web service that scales efficiently under load?'],
['Solve the integral of x^2 from 0 to 1.'],
['Write a short story about a mysterious writer in a busy city.'],
['Create a pun-filled birthday message with a coding twist.']
],
cache_examples=False,
type="messages",
)
with gr.Tab("Parameters"):
gr.Markdown("### Generation Parameters")
temp_slider = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.5, label="Temperature")
top_p_slider = gr.Slider(minimum=0.01, maximum=1.0, step=0.05, value=0.9, label="Top P")
max_tokens_num = gr.Number(value=300, label="Max new tokens", precision=0)
memory_topk_slider = gr.Slider(minimum=1, maximum=5, step=1, value=2, label="Memory Retrieval Top K")
show_raw_checkbox = gr.Checkbox(value=False, label="Show Raw Output") # New checkbox for raw output
save_params_btn = gr.Button("Save Parameters")
save_params_btn.click(
lambda t, p, m, k, s: {
"temperature": t,
"top_p": p,
"max_new_tokens": m,
"memory_top_k": k,
"show_raw_output": s
},
inputs=[temp_slider, top_p_slider, max_tokens_num, memory_topk_slider, show_raw_checkbox],
outputs=param_state,
)
with gr.Tab("Prompt Config"):
gr.Markdown("### Configure Prompt Templates for Each Preset")
with gr.Tabs():
with gr.Tab("Coding"):
prompt_brainstorm_box_code = gr.Textbox(
value=default_prompts["coding"]["brainstorm"],
label="Brainstorm Prompt (Coding)",
lines=8,
)
prompt_round2_box_code = gr.Textbox(
value=default_prompts["coding"]["round2"],
label="Round 2 Prompt (Coding)",
lines=8,
)
prompt_synthesis_box_code = gr.Textbox(
value=default_prompts["coding"]["synthesis"],
label="Synthesis Prompt (Coding)",
lines=8,
)
prompt_rationale_box_code = gr.Textbox(
value=default_prompts["coding"]["rationale"],
label="Rationale Prompt (Coding)",
lines=8,
)
with gr.Tab("Math"):
prompt_brainstorm_box_math = gr.Textbox(
value=default_prompts["math"]["brainstorm"],
label="Brainstorm Prompt (Math)",
lines=8,
)
prompt_round2_box_math = gr.Textbox(
value=default_prompts["math"]["round2"],
label="Round 2 Prompt (Math)",
lines=8,
)
prompt_synthesis_box_math = gr.Textbox(
value=default_prompts["math"]["synthesis"],
label="Synthesis Prompt (Math)",
lines=8,
)
prompt_rationale_box_math = gr.Textbox(
value=default_prompts["math"]["rationale"],
label="Rationale Prompt (Math)",
lines=8,
)
with gr.Tab("Writing"):
prompt_brainstorm_box_writing = gr.Textbox(
value=default_prompts["writing"]["brainstorm"],
label="Brainstorm Prompt (Writing)",
lines=8,
)
prompt_round2_box_writing = gr.Textbox(
value=default_prompts["writing"]["round2"],
label="Round 2 Prompt (Writing)",
lines=8,
)
prompt_synthesis_box_writing = gr.Textbox(
value=default_prompts["writing"]["synthesis"],
label="Synthesis Prompt (Writing)",
lines=8,
)
prompt_rationale_box_writing = gr.Textbox(
value=default_prompts["writing"]["rationale"],
label="Rationale Prompt (Writing)",
lines=8,
)
save_prompts_btn = gr.Button("Save Prompts")
def save_prompts(code_brain, code_r2, code_syn, code_rat, math_brain, math_r2, math_syn, math_rat, writing_brain, writing_r2, writing_syn, writing_rat):
return {
"coding": {
"brainstorm": code_brain,
"round2": code_r2,
"synthesis": code_syn,
"rationale": code_rat,
},
"math": {
"brainstorm": math_brain,
"round2": math_r2,
"synthesis": math_syn,
"rationale": math_rat,
},
"writing": {
"brainstorm": writing_brain,
"round2": writing_r2,
"synthesis": writing_syn,
"rationale": writing_rat,
}
}
save_prompts_btn.click(
save_prompts,
inputs=[prompt_brainstorm_box_code, prompt_round2_box_code, prompt_synthesis_box_code, prompt_rationale_box_code,
prompt_brainstorm_box_math, prompt_round2_box_math, prompt_synthesis_box_math, prompt_rationale_box_math,
prompt_brainstorm_box_writing, prompt_round2_box_writing, prompt_synthesis_box_writing, prompt_rationale_box_writing],
outputs=prompt_state,
)
gr.Markdown(ui_license)
if __name__ == "__main__":
demo.launch()
|