Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,58 @@ import random
|
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 7 |
from huggingface_hub import hf_hub_download, HfApi
|
| 8 |
from duckduckgo_search import DDGS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# KONFIGURATION
|
| 11 |
DATASET_REPO = "DEIN_USERNAME/isaac-memory-db"
|
|
|
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 7 |
from huggingface_hub import hf_hub_download, HfApi
|
| 8 |
from duckduckgo_search import DDGS
|
| 9 |
+
import fitz # PyMuPDF für PDFs
|
| 10 |
+
import docx
|
| 11 |
+
|
| 12 |
+
def extract_text_from_file(file):
|
| 13 |
+
text = ""
|
| 14 |
+
if file.name.endswith(".pdf"):
|
| 15 |
+
doc = fitz.open(file.name)
|
| 16 |
+
for page in doc:
|
| 17 |
+
text += page.get_text()
|
| 18 |
+
elif file.name.endswith(".docx"):
|
| 19 |
+
doc = docx.Document(file.name)
|
| 20 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 21 |
+
elif file.name.endswith(".txt"):
|
| 22 |
+
with open(file.name, "r", encoding="utf-8") as f:
|
| 23 |
+
text = f.read()
|
| 24 |
+
return text
|
| 25 |
+
|
| 26 |
+
def upload_to_memory(file):
|
| 27 |
+
if file is None:
|
| 28 |
+
return "Keine Datei ausgewählt."
|
| 29 |
+
|
| 30 |
+
# Text extrahieren
|
| 31 |
+
extracted_text = extract_text_from_file(file)
|
| 32 |
+
|
| 33 |
+
# In Isaacs permanentes Gedächtnis (W-Vector) schreiben
|
| 34 |
+
memory = get_memory()
|
| 35 |
+
memory_id = str(len(memory))
|
| 36 |
+
memory[memory_id] = f"Dokument-Wissen ({file.name}): {extracted_text[:1000]}" # Kürzen für Prompt-Limit
|
| 37 |
+
update_memory(memory)
|
| 38 |
+
|
| 39 |
+
return f"Datei '{file.name}' wurde erfolgreich in die Evolution 2.0 Architektur integriert! 📄✅"
|
| 40 |
+
|
| 41 |
+
# --- UI Anpassung ---
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("# Isaac: Evolution 2.0 mit Datei-Upload")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column(scale=4):
|
| 47 |
+
chatbot = gr.Chatbot()
|
| 48 |
+
msg = gr.Textbox(placeholder="Frag Isaac oder gib eine Korrektur ein...")
|
| 49 |
+
with gr.Column(scale=1):
|
| 50 |
+
file_output = gr.Markdown("Lade ein Dokument hoch, um Isaac zu trainieren.")
|
| 51 |
+
upload_button = gr.UploadButton("Datei hochladen 📂", file_types=[".pdf", ".docx", ".txt"])
|
| 52 |
+
clear = gr.Button("Chat leeren")
|
| 53 |
+
|
| 54 |
+
# Logik für Upload
|
| 55 |
+
upload_button.upload(upload_to_memory, upload_button, file_output)
|
| 56 |
+
|
| 57 |
+
# Logik für Chat (Nutze deine bestehende evolution_chat Funktion)
|
| 58 |
+
msg.submit(evolution_chat, [msg, chatbot], [msg, chatbot])
|
| 59 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 60 |
+
|
| 61 |
|
| 62 |
# KONFIGURATION
|
| 63 |
DATASET_REPO = "DEIN_USERNAME/isaac-memory-db"
|