aidn commited on
Commit
2c9e8e8
·
verified ·
1 Parent(s): 09df286

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -30
app.py CHANGED
@@ -2,14 +2,13 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # Liste der Ratsmitglieder (Du kannst die Modelle hier anpassen)
6
  COUNCIL_MEMBERS = {
7
- "Der Architekt (Llama 3.3)": "meta-llama/Llama-3.3-70B-Instruct:cheapest",
8
- "Der Glitch (DeepSeek V3)": "deepseek-ai/DeepSeek-V3:cheapest",
9
- "Der Debugger (Qwen Coder)": "Qwen/Qwen2.5-Coder-32B-Instruct:cheapest"
10
  }
11
 
12
- # Dein HF_TOKEN sollte in den Space Settings hinterlegt sein
13
  client = InferenceClient(token=os.getenv("HF_TOKEN"))
14
 
15
  def ask_model(model_id, system_prompt, user_input):
@@ -18,14 +17,17 @@ def ask_model(model_id, system_prompt, user_input):
18
  {"role": "user", "content": user_input}
19
  ]
20
  response = ""
21
- for message in client.chat_completion(
22
- model=model_id,
23
- messages=messages,
24
- max_tokens=500,
25
- stream=True
26
- ):
27
- response += message.choices[0].delta.content or ""
28
- return response
 
 
 
29
 
30
  def run_council(user_prompt, rounds):
31
  history = []
@@ -33,43 +35,46 @@ def run_council(user_prompt, rounds):
33
 
34
  for r in range(int(rounds)):
35
  round_header = f"--- RUNDE {r+1} ---"
36
- history.append((None, f"## {round_header}"))
 
 
37
 
38
  round_notes = ""
39
  for name, model_id in COUNCIL_MEMBERS.items():
40
- system_msg = f"Du bist der {name} in einem Expertenrat. Diskutiere kurz und prägnant."
41
  if r > 0:
42
- system_msg += " Beziehe dich auf die vorherigen Argumente und finde einen Konsens."
43
 
44
  answer = ask_model(model_id, system_msg, current_context)
45
  formatted_answer = f"**{name}**: {answer}"
46
 
47
- history.append((None, formatted_answer))
48
  round_notes += f"\n{formatted_answer}\n"
49
  yield history # Live-Update in der UI
50
 
51
  current_context += f"\nZusammenfassung Runde {r+1}:{round_notes}"
52
 
53
  # Finale Einigung
54
- final_prompt = "Fasse die Diskussion zusammen und gib eine finale, konsolidierte Antwort."
55
- final_res = ask_model("mistralai/Mixtral-8x7B-Instruct-v0.1", "Du bist der Moderator.", current_context + final_prompt)
56
 
57
- history.append((None, "### 🏆 FINALE ENTSCHEIDUNG"))
58
- history.append((None, final_res))
59
  yield history
60
 
61
- # Gradio UI
62
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
63
- gr.Markdown("# 🏛️ PromptPlenum42 (aka LLMCouncil)")
64
- gr.Markdown("Drei Experten-Modelle diskutieren deinen Prompt und versuchen, einen Konsens zu finden.")
65
 
66
  with gr.Row():
67
- input_text = gr.Textbox(label="Dein Thema / Frage", placeholder="Sollten wir zum Mars fliegen?")
68
- rounds_slider = gr.Slider(minimum=1, maximum=3, value=1, step=1, label="Diskussionsrunden")
69
 
70
- start_btn = gr.Button("Diskussion starten", variant="primary")
71
- chatbot = gr.Chatbot(label="Council Protokoll", height=600)
 
72
 
73
  start_btn.click(run_council, inputs=[input_text, rounds_slider], outputs=[chatbot])
74
 
75
- demo.launch()
 
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ # Liste der Ratsmitglieder (nerdig & geeky)
6
  COUNCIL_MEMBERS = {
7
+ "Der Architekt": "meta-llama/Llama-3.3-70B-Instruct:cheapest",
8
+ "Der Glitch": "deepseek-ai/DeepSeek-V3:cheapest",
9
+ "Der Debugger": "Qwen/Qwen2.5-Coder-32B-Instruct:cheapest"
10
  }
11
 
 
12
  client = InferenceClient(token=os.getenv("HF_TOKEN"))
13
 
14
  def ask_model(model_id, system_prompt, user_input):
 
17
  {"role": "user", "content": user_input}
18
  ]
19
  response = ""
20
+ try:
21
+ for message in client.chat_completion(
22
+ model=model_id,
23
+ messages=messages,
24
+ max_tokens=500,
25
+ stream=True
26
+ ):
27
+ response += message.choices[0].delta.content or ""
28
+ return response
29
+ except Exception as e:
30
+ return f"🚨 Error: {str(e)}"
31
 
32
  def run_council(user_prompt, rounds):
33
  history = []
 
35
 
36
  for r in range(int(rounds)):
37
  round_header = f"--- RUNDE {r+1} ---"
38
+ # Neues Gradio 6 Format: Dictionary statt Tuple
39
+ history.append({"role": "assistant", "content": f"## {round_header}"})
40
+ yield history
41
 
42
  round_notes = ""
43
  for name, model_id in COUNCIL_MEMBERS.items():
44
+ system_msg = f"Du bist {name} in einem Expertenrat. Diskutiere kurz und prägnant."
45
  if r > 0:
46
+ system_msg += " Beziehe dich auf die vorherigen Argumente deiner Kollegen."
47
 
48
  answer = ask_model(model_id, system_msg, current_context)
49
  formatted_answer = f"**{name}**: {answer}"
50
 
51
+ history.append({"role": "assistant", "content": formatted_answer})
52
  round_notes += f"\n{formatted_answer}\n"
53
  yield history # Live-Update in der UI
54
 
55
  current_context += f"\nZusammenfassung Runde {r+1}:{round_notes}"
56
 
57
  # Finale Einigung
58
+ final_res = ask_model("mistralai/Mixtral-8x7B-Instruct-v0.1", "Du bist der Moderator.", current_context + "Fasse alles final zusammen.")
 
59
 
60
+ history.append({"role": "assistant", "content": "### 🏆 FINALE ENTSCHEIDUNG"})
61
+ history.append({"role": "assistant", "content": final_res})
62
  yield history
63
 
64
+ # Gradio 6 UI
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("# 🏛️ Der Subraum-Stammtisch")
67
+ gr.Markdown("> Status: Initialisiere Prompt-Plenum auf Frequenz 0x42...")
68
 
69
  with gr.Row():
70
+ input_text = gr.Textbox(label="Input-Vektor (Deine Frage)", placeholder="Sollten wir zum Mars fliegen?")
71
+ rounds_slider = gr.Slider(minimum=1, maximum=3, value=1, step=1, label="Diskussionszyklen")
72
 
73
+ start_btn = gr.Button("Protokoll starten", variant="primary")
74
+ # type="messages" ist entscheidend für Gradio 6
75
+ chatbot = gr.Chatbot(label="Council Protokoll", height=600, type="messages")
76
 
77
  start_btn.click(run_council, inputs=[input_text, rounds_slider], outputs=[chatbot])
78
 
79
+ # Theme in launch() verschoben
80
+ demo.launch(theme=gr.themes.Soft())