aidn commited on
Commit
3b36e9a
·
verified ·
1 Parent(s): 91f6209

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+ "Strategist": "mistralai/Mistral-7B-Instruct-v0.3",
8
+ "Creative": "meta-llama/Meta-Llama-3-8B-Instruct",
9
+ "Critic": "google/gemma-2-9b-it"
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):
16
+ messages = [
17
+ {"role": "system", "content": system_prompt},
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 = []
32
+ current_context = f"Die ursprüngliche Frage lautet: {user_prompt}\n\n"
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()