Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,10 @@ import torch
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import fitz
|
|
|
|
|
|
|
| 6 |
from transformers import pipeline
|
| 7 |
from huggingface_hub import hf_hub_download, HfApi
|
| 8 |
-
from duckduckgo_search import DDGS
|
| 9 |
from sentence_transformers import SentenceTransformer, util
|
| 10 |
|
| 11 |
# --- KONFIGURATION ---
|
|
@@ -17,85 +18,86 @@ model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
|
| 17 |
pipe = pipeline("text-generation", model=model_id, torch_dtype=torch.bfloat16, device_map="cpu")
|
| 18 |
search_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 19 |
|
| 20 |
-
# ---
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
if not memory: return "Du bist Isaac, eine loyale und intelligente KI."
|
| 34 |
-
passages = list(memory.values())
|
| 35 |
-
query_emb = search_model.encode(query, convert_to_tensor=True)
|
| 36 |
-
passage_embs = search_model.encode(passages, convert_to_tensor=True)
|
| 37 |
-
hits = util.semantic_search(query_emb, passage_embs, top_k=1)
|
| 38 |
-
return passages[hits[0][0]['corpus_id']]
|
| 39 |
|
| 40 |
def chat_logic(message, history):
|
| 41 |
memory = get_memory()
|
| 42 |
context = find_best_context(message, memory)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
prompt = (
|
| 46 |
-
f"<|system|>\nDu bist Isaac, eine fortgeschrittene KI. "
|
| 47 |
-
f"Dein aktuelles Wissen: {context}\n"
|
| 48 |
-
f"Antworte dem User direkt, humorvoll und präzise auf Deutsch.</s>\n"
|
| 49 |
-
f"<|user|>\n{message}</s>\n"
|
| 50 |
-
f"<|assistant|>\n"
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
outputs = pipe(
|
| 54 |
-
prompt,
|
| 55 |
-
max_new_tokens=80,
|
| 56 |
-
do_sample=True,
|
| 57 |
-
temperature=0.4, # Weniger Chaos, mehr Fokus
|
| 58 |
-
repetition_penalty=1.3
|
| 59 |
-
)
|
| 60 |
|
|
|
|
| 61 |
answer = outputs[0]["generated_text"].split("<|assistant|>\n")[-1].strip()
|
| 62 |
|
| 63 |
-
# Falls er sich im Kreis dreht oder System-Tags wiederholt
|
| 64 |
-
clean_answer = answer.split("</s>")[0].split("<|")[0].strip()
|
| 65 |
-
|
| 66 |
history.append({"role": "user", "content": message})
|
| 67 |
-
history.append({"role": "assistant", "content":
|
| 68 |
-
|
| 69 |
return "", history
|
| 70 |
|
| 71 |
def process_file(file):
|
| 72 |
-
if file is None: return "Warte auf
|
| 73 |
-
text = ""
|
| 74 |
doc = fitz.open(file.name)
|
| 75 |
-
|
|
|
|
| 76 |
memory = get_memory()
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# --- INTERFACE ---
|
| 82 |
|
| 83 |
-
with gr.Blocks() as demo:
|
| 84 |
-
gr.Markdown("# 🧬 Isaac: Evolution 2.0")
|
| 85 |
-
|
| 86 |
-
chatbot = gr.Chatbot(height=450, label="Isaac's Bewusstsein")
|
| 87 |
|
| 88 |
-
with gr.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
msg.submit(chat_logic, [msg, chatbot], [msg, chatbot])
|
| 97 |
btn.click(chat_logic, [msg, chatbot], [msg, chatbot])
|
| 98 |
upl.upload(process_file, upl, stat)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import fitz
|
| 6 |
+
import numpy as np
|
| 7 |
+
from collections import Counter
|
| 8 |
from transformers import pipeline
|
| 9 |
from huggingface_hub import hf_hub_download, HfApi
|
|
|
|
| 10 |
from sentence_transformers import SentenceTransformer, util
|
| 11 |
|
| 12 |
# --- KONFIGURATION ---
|
|
|
|
| 18 |
pipe = pipeline("text-generation", model=model_id, torch_dtype=torch.bfloat16, device_map="cpu")
|
| 19 |
search_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 20 |
|
| 21 |
+
# --- MATHEMATISCHE KERNELEMENTE (W-ALGORITHMUS) ---
|
| 22 |
|
| 23 |
+
def calculate_shannon_entropy(text):
|
| 24 |
+
"""
|
| 25 |
+
Berechnet die Entropie als Maß für die Informationsdichte.
|
| 26 |
+
Ein Teil der Transparenz-Bedingung R.
|
| 27 |
+
"""
|
| 28 |
+
if not text: return 0
|
| 29 |
+
probabilities = [n/len(text) for n in Counter(text).values()]
|
| 30 |
+
entropy = -sum(p * np.log2(p) for p in probabilities)
|
| 31 |
+
return entropy
|
| 32 |
|
| 33 |
+
def w_algorithm_check(new_data, memory):
|
| 34 |
+
"""
|
| 35 |
+
Delta W = η * ∇ I_tot
|
| 36 |
+
Bedingung: dR/dθ = 0
|
| 37 |
+
Prüft, ob die neue Information die System-Inkonsistenz erhöht.
|
| 38 |
+
"""
|
| 39 |
+
# Referenzwert R aus dem bestehenden Gedächtnis
|
| 40 |
+
existing_text = " ".join(list(memory.values())[-5:])
|
| 41 |
+
r_baseline = calculate_shannon_entropy(existing_text) if existing_text else 3.0
|
| 42 |
+
|
| 43 |
+
# R-Wert der neuen Information
|
| 44 |
+
r_new = calculate_shannon_entropy(new_data)
|
| 45 |
+
|
| 46 |
+
# Delta R Berechnung (Die Bedingung: darf nicht negativ sein)
|
| 47 |
+
# Ein zu hoher Entropie-Sprung deutet auf 'Rauschen' hin,
|
| 48 |
+
# ein zu niedriger auf 'Apathie' (Informationsverlust).
|
| 49 |
+
is_consistent = 2.0 < r_new < 5.5 # Idealer Korridor für Transparenz R
|
| 50 |
+
|
| 51 |
+
return is_consistent, r_new
|
| 52 |
|
| 53 |
+
# --- FUNKTIONEN ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def chat_logic(message, history):
|
| 56 |
memory = get_memory()
|
| 57 |
context = find_best_context(message, memory)
|
| 58 |
|
| 59 |
+
prompt = f"<|system|>\nIsaac-Modus: W-Algorithmus aktiv. Kontext: {context}</s>\n<|user|>\n{message}</s>\n<|assistant|>\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
outputs = pipe(prompt, max_new_tokens=100, do_sample=True, temperature=0.4, repetition_penalty=1.2)
|
| 62 |
answer = outputs[0]["generated_text"].split("<|assistant|>\n")[-1].strip()
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
history.append({"role": "user", "content": message})
|
| 65 |
+
history.append({"role": "assistant", "content": answer})
|
|
|
|
| 66 |
return "", history
|
| 67 |
|
| 68 |
def process_file(file):
|
| 69 |
+
if file is None: return "Warte auf Daten..."
|
|
|
|
| 70 |
doc = fitz.open(file.name)
|
| 71 |
+
text = "".join([page.get_text() for page in doc])
|
| 72 |
+
|
| 73 |
memory = get_memory()
|
| 74 |
+
consistent, r_val = w_algorithm_check(text, memory)
|
| 75 |
+
|
| 76 |
+
if consistent:
|
| 77 |
+
# Delta W: Update des Vektors
|
| 78 |
+
memory[str(len(memory))] = text[:1000]
|
| 79 |
+
update_memory(memory)
|
| 80 |
+
return f"W-Algorithmus: Integration erfolgreich. R={r_val:.2f}. System ist klüger geworden."
|
| 81 |
+
else:
|
| 82 |
+
# Blockade nach dR/dθ = 0 Regel
|
| 83 |
+
return f"W-Algorithmus: Integration blockiert! R={r_val:.2f} verletzt Transparenz-Bedingung."
|
| 84 |
|
| 85 |
# --- INTERFACE ---
|
| 86 |
|
| 87 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 88 |
+
gr.Markdown("# 🧬 Isaac: Evolution 2.0 (W-Algorithmus)")
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
with gr.Tab("Bewusstsein"):
|
| 91 |
+
chatbot = gr.Chatbot(height=450, type="messages")
|
| 92 |
+
msg = gr.Textbox(label="Input")
|
| 93 |
+
btn = gr.Button("Senden")
|
| 94 |
+
|
| 95 |
+
with gr.Tab("Evolution (W-Update)"):
|
| 96 |
+
upl = gr.File(label="Wissens-Quelle hochladen")
|
| 97 |
+
stat = gr.Textbox(label="Mathematische Validierung (R-Wert)")
|
| 98 |
|
| 99 |
msg.submit(chat_logic, [msg, chatbot], [msg, chatbot])
|
| 100 |
btn.click(chat_logic, [msg, chatbot], [msg, chatbot])
|
| 101 |
upl.upload(process_file, upl, stat)
|
| 102 |
|
| 103 |
+
demo.launch()
|
|
|