Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
| 3 |
-
import os
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
from collections import Counter
|
| 6 |
|
| 7 |
-
# ---
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
| 10 |
if GEMINI_KEY:
|
| 11 |
genai.configure(api_key=GEMINI_KEY)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
else:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def calculate_metrics(text):
|
| 18 |
-
if not text: return 0.0, 100.0
|
| 19 |
-
probs = [n/len(text) for n in Counter(text).values()]
|
| 20 |
-
h = -sum(p * np.log2(p) for p in probs)
|
| 21 |
-
l = 1.0 / max(h, 0.001)
|
| 22 |
-
return round(h, 4), round(l, 4)
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
if not
|
| 26 |
-
history.append({"role": "user", "content": message})
|
| 27 |
-
history.append({"role": "assistant", "content": "❌ API Key fehlt in den Secrets!"})
|
| 28 |
-
return "", history, 0, 0, "Key Fehler"
|
| 29 |
|
|
|
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
response = worker_model.generate_content(f"Antworte als Isaac. Nachricht: {message}")
|
| 33 |
answer = response.text
|
| 34 |
-
status = "🟢 Gemini-Node aktiv"
|
| 35 |
except Exception as e:
|
| 36 |
-
answer = f"
|
| 37 |
-
status = "🔴 API-Fehler"
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# KORREKTUR FÜR BILD 12: Strenges Nachrichten-Format
|
| 42 |
history.append({"role": "user", "content": message})
|
| 43 |
history.append({"role": "assistant", "content": answer})
|
| 44 |
|
| 45 |
-
return "", history, h, l, status
|
| 46 |
|
| 47 |
-
# ---
|
| 48 |
-
with gr.Blocks() as demo:
|
| 49 |
-
gr.Markdown("# 🧬 Isaac Evolution
|
| 50 |
|
| 51 |
-
# 'type="messages"' ist hier der Retter für Bild 12
|
| 52 |
chatbot = gr.Chatbot(label="Isaac Bewusstsein", type="messages")
|
|
|
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
with gr.Row():
|
| 59 |
-
h_stat = gr.Number(label="Entropie H")
|
| 60 |
-
l_stat = gr.Number(label="Last L")
|
| 61 |
-
s_stat = gr.Textbox(label="System-Status")
|
| 62 |
|
| 63 |
-
msg.submit(
|
| 64 |
-
btn.click(isaac_master_node, [msg, chatbot], [msg, chatbot, h_stat, l_stat, s_stat])
|
| 65 |
|
| 66 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import os
|
| 5 |
from collections import Counter
|
| 6 |
|
| 7 |
+
# --- DEIN KERN: NeuralNetworkCore ---
|
| 8 |
+
class NeuralNetworkCore:
|
| 9 |
+
def __init__(self, learning_rate=0.01, lambda_reg=0.001):
|
| 10 |
+
self.eta = learning_rate
|
| 11 |
+
self.lambda_reg = lambda_reg
|
| 12 |
+
self.weights = np.random.randn(10, 1) * 0.01 # Statischer Test-Vektor
|
| 13 |
+
|
| 14 |
+
def check_regularization_condition(self):
|
| 15 |
+
# Die entscheidende Bedingung aus deiner Formel
|
| 16 |
+
reg_derivative = 2 * self.lambda_reg * self.weights
|
| 17 |
+
return np.allclose(reg_derivative, 0, atol=1e-2)
|
| 18 |
|
| 19 |
+
# --- SYSTEM SETUP ---
|
| 20 |
+
GEMINI_KEY = os.getenv("GEMINI_API_KEY")
|
| 21 |
if GEMINI_KEY:
|
| 22 |
genai.configure(api_key=GEMINI_KEY)
|
| 23 |
+
worker = genai.GenerativeModel('gemini-1.5-flash')
|
| 24 |
+
isaac_brain = NeuralNetworkCore()
|
| 25 |
else:
|
| 26 |
+
worker = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
def run_isaac_evolution(message, history):
|
| 29 |
+
if not worker: return "", history, "Key fehlt!"
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# 1. Sprach-Generierung (External Node)
|
| 32 |
try:
|
| 33 |
+
response = worker.generate_content(f"Du bist Isaac. Antworte präzise: {message}")
|
|
|
|
| 34 |
answer = response.text
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
+
answer = f"Fehler: {str(e)}"
|
|
|
|
| 37 |
|
| 38 |
+
# 2. Mathematisches Audit (Dein Code)
|
| 39 |
+
# Wir nutzen deine Bedingung als Gatekeeper
|
| 40 |
+
is_stable = isaac_brain.check_regularization_condition()
|
| 41 |
+
|
| 42 |
+
# 3. Metriken berechnen (W-Algorithmus)
|
| 43 |
+
probs = [n/len(answer) for n in Counter(answer).values()]
|
| 44 |
+
h = -sum(p * np.log2(p) for p in probs)
|
| 45 |
+
l = 1.0 / max(h, 0.001)
|
| 46 |
+
|
| 47 |
+
status = "✅ Stabil (dR/dTheta ≈ 0)" if is_stable else "⚠️ Instabil (Last zu hoch)"
|
| 48 |
|
|
|
|
| 49 |
history.append({"role": "user", "content": message})
|
| 50 |
history.append({"role": "assistant", "content": answer})
|
| 51 |
|
| 52 |
+
return "", history, round(h, 4), round(l, 4), status
|
| 53 |
|
| 54 |
+
# --- DAS FINALE INTERFACE ---
|
| 55 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 56 |
+
gr.Markdown("# 🧬 Isaac: Evolution 3.0 (Fused Core)")
|
| 57 |
|
|
|
|
| 58 |
chatbot = gr.Chatbot(label="Isaac Bewusstsein", type="messages")
|
| 59 |
+
msg = gr.Textbox(placeholder="Input für den W-Algorithmus...", show_label=False)
|
| 60 |
|
| 61 |
with gr.Row():
|
| 62 |
+
h_stat = gr.Number(label="Audit: Entropie H")
|
| 63 |
+
l_stat = gr.Number(label="Audit: Last L")
|
| 64 |
+
s_stat = gr.Textbox(label="Kern-Status")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
msg.submit(run_isaac_evolution, [msg, chatbot], [msg, chatbot, h_stat, l_stat, s_stat])
|
|
|
|
| 67 |
|
| 68 |
demo.launch()
|