Spaces:
Running
Running
Update app.py
Browse files
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 (
|
| 6 |
COUNCIL_MEMBERS = {
|
| 7 |
-
"Der Architekt
|
| 8 |
-
"Der Glitch
|
| 9 |
-
"Der Debugger
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
round_notes = ""
|
| 39 |
for name, model_id in COUNCIL_MEMBERS.items():
|
| 40 |
-
system_msg = f"Du bist
|
| 41 |
if r > 0:
|
| 42 |
-
system_msg += " Beziehe dich auf die vorherigen Argumente
|
| 43 |
|
| 44 |
answer = ask_model(model_id, system_msg, current_context)
|
| 45 |
formatted_answer = f"**{name}**: {answer}"
|
| 46 |
|
| 47 |
-
history.append(
|
| 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 |
-
|
| 55 |
-
final_res = ask_model("mistralai/Mixtral-8x7B-Instruct-v0.1", "Du bist der Moderator.", current_context + final_prompt)
|
| 56 |
|
| 57 |
-
history.append(
|
| 58 |
-
history.append(
|
| 59 |
yield history
|
| 60 |
|
| 61 |
-
# Gradio UI
|
| 62 |
-
with gr.Blocks(
|
| 63 |
-
gr.Markdown("# 🏛️
|
| 64 |
-
gr.Markdown("
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
-
input_text = gr.Textbox(label="
|
| 68 |
-
rounds_slider = gr.Slider(minimum=1, maximum=3, value=1, step=1, label="
|
| 69 |
|
| 70 |
-
start_btn = gr.Button("
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
start_btn.click(run_council, inputs=[input_text, rounds_slider], outputs=[chatbot])
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
| 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())
|