Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -115,11 +115,19 @@ class PromptManager:
|
|
| 115 |
# 2. CORE SERVICES (API & UI Helpers)
|
| 116 |
# ==========================================
|
| 117 |
class LLMService:
|
| 118 |
-
"""Kapselt die gesamte API-Kommunikation mit Hugging Face."""
|
| 119 |
def __init__(self):
|
| 120 |
self.client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 121 |
|
|
|
|
|
|
|
| 122 |
def ask(self, model_id, system_prompt, user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
messages = [
|
| 124 |
{"role": "system", "content": system_prompt},
|
| 125 |
{"role": "user", "content": user_input}
|
|
@@ -131,8 +139,17 @@ class LLMService:
|
|
| 131 |
):
|
| 132 |
if hasattr(chunk, "choices") and chunk.choices and len(chunk.choices) > 0:
|
| 133 |
response += chunk.choices[0].delta.content or ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
return response
|
| 135 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
return f"🚨 System Error ({model_id}): {str(e)}"
|
| 137 |
|
| 138 |
class UIHelper:
|
|
@@ -229,28 +246,6 @@ class PlenumOrchestrator:
|
|
| 229 |
history.append({"role": "assistant", "content": final_res})
|
| 230 |
yield history
|
| 231 |
|
| 232 |
-
# ==========================================
|
| 233 |
-
# MLFLOW TRACKING BLOCK (DagsHub)
|
| 234 |
-
# ==========================================
|
| 235 |
-
try:
|
| 236 |
-
# Speichert die Daten deines aktuellen Durchlaufs
|
| 237 |
-
with mlflow.start_run(run_name="PromptPlenum_Session"):
|
| 238 |
-
|
| 239 |
-
# Basis-Parameter loggen
|
| 240 |
-
mlflow.log_param("user_prompt_length", len(user_prompt))
|
| 241 |
-
mlflow.log_param("discussion_rounds", rounds)
|
| 242 |
-
mlflow.log_param("moderator_model", MODERATOR_MODEL)
|
| 243 |
-
|
| 244 |
-
# Die Textblöcke als lesbare Dateien (Artefakte) hochladen
|
| 245 |
-
mlflow.log_text(user_prompt, "inputs/user_prompt.txt")
|
| 246 |
-
mlflow.log_text(discussion_history, "logs/full_discussion.txt")
|
| 247 |
-
mlflow.log_text(consensus_res, "logs/moderator_consensus.txt")
|
| 248 |
-
mlflow.log_text(final_res, "outputs/final_result.txt")
|
| 249 |
-
|
| 250 |
-
print("✅ MLflow Logging erfolgreich.")
|
| 251 |
-
except Exception as e:
|
| 252 |
-
print(f"⚠️ MLflow Logging fehlgeschlagen: {e}")
|
| 253 |
-
|
| 254 |
# Instanziiere den Orchestrator
|
| 255 |
orchestrator = PlenumOrchestrator()
|
| 256 |
|
|
|
|
| 115 |
# 2. CORE SERVICES (API & UI Helpers)
|
| 116 |
# ==========================================
|
| 117 |
class LLMService:
|
| 118 |
+
"""Kapselt die gesamte API-Kommunikation mit Hugging Face und trackt sie in MLflow Traces."""
|
| 119 |
def __init__(self):
|
| 120 |
self.client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 121 |
|
| 122 |
+
# NEU: Dieser Decorator erzeugt den wunderschönen Eintrag in der MLflow "Traces" UI
|
| 123 |
+
@mlflow.trace(name="LLM_Turn", span_type="LLM")
|
| 124 |
def ask(self, model_id, system_prompt, user_input):
|
| 125 |
+
|
| 126 |
+
# Wir holen uns den aktuellen "Trace" und füttern ihn mit den Inputs für die UI
|
| 127 |
+
span = mlflow.get_current_active_span()
|
| 128 |
+
if span:
|
| 129 |
+
span.set_inputs({"Model": model_id, "System_Prompt": system_prompt, "User_Input": user_input})
|
| 130 |
+
|
| 131 |
messages = [
|
| 132 |
{"role": "system", "content": system_prompt},
|
| 133 |
{"role": "user", "content": user_input}
|
|
|
|
| 139 |
):
|
| 140 |
if hasattr(chunk, "choices") and chunk.choices and len(chunk.choices) > 0:
|
| 141 |
response += chunk.choices[0].delta.content or ""
|
| 142 |
+
|
| 143 |
+
# Wenn die KI fertig ist, speichern wir ihre Antwort im Trace
|
| 144 |
+
if span:
|
| 145 |
+
span.set_outputs({"Response": response})
|
| 146 |
+
|
| 147 |
return response
|
| 148 |
except Exception as e:
|
| 149 |
+
# Bei einem Fehler färbt MLflow den Trace rot ein
|
| 150 |
+
if span:
|
| 151 |
+
span.set_status("ERROR")
|
| 152 |
+
span.set_outputs({"Error": str(e)})
|
| 153 |
return f"🚨 System Error ({model_id}): {str(e)}"
|
| 154 |
|
| 155 |
class UIHelper:
|
|
|
|
| 246 |
history.append({"role": "assistant", "content": final_res})
|
| 247 |
yield history
|
| 248 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
# Instanziiere den Orchestrator
|
| 250 |
orchestrator = PlenumOrchestrator()
|
| 251 |
|