Stemini commited on
Commit
0b94e1b
·
verified ·
1 Parent(s): 76da1c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -78
app.py CHANGED
@@ -2,8 +2,8 @@ import gradio as gr
2
  import torch
3
  import json
4
  import os
5
- import fitz # PyMuPDF für PDFs
6
- from transformers import AutoModelForCausalLM, AutoTokenizer
7
  from huggingface_hub import hf_hub_download, HfApi
8
  from duckduckgo_search import DDGS
9
  from sentence_transformers import SentenceTransformer, util
@@ -13,10 +13,9 @@ DATASET_REPO = "Stemini/isaac-memory-db"
13
  HF_TOKEN = os.getenv("HF_TOKEN")
14
  api = HfApi()
15
 
16
- # Modelle laden
17
- model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
18
- tokenizer = AutoTokenizer.from_pretrained(model_name)
19
- model = AutoModelForCausalLM.from_pretrained(model_name)
20
  search_model = SentenceTransformer('all-MiniLM-L6-v2')
21
 
22
  # --- FUNKTIONEN ---
@@ -31,101 +30,61 @@ def update_memory(new_memory):
31
  with open("memory.json", "w") as f: json.dump(new_memory, f)
32
  api.upload_file(path_or_fileobj="memory.json", path_in_repo="memory.json", repo_id=DATASET_REPO, repo_type="dataset", token=HF_TOKEN)
33
 
34
- def web_search(query):
35
- try:
36
- with DDGS() as ddgs:
37
- results = [r['body'] for r in ddgs.text(query, max_results=2)]
38
- return " ".join(results)
39
- except: return "Keine Websuche möglich."
40
-
41
  def find_best_context(query, memory):
42
- if not memory or len(memory) == 0: return "Kein Vorwissen."
43
  passages = list(memory.values())
44
  query_emb = search_model.encode(query, convert_to_tensor=True)
45
  passage_embs = search_model.encode(passages, convert_to_tensor=True)
46
  hits = util.semantic_search(query_emb, passage_embs, top_k=1)
47
  return passages[hits[0][0]['corpus_id']]
48
 
49
- def process_file(file):
50
- if file is None: return "Keine Datei ausgewählt."
51
- text = ""
52
- if file.name.endswith(".pdf"):
53
- doc = fitz.open(file.name)
54
- for page in doc: text += page.get_text()
55
- else:
56
- with open(file.name, "r", encoding="utf-8") as f: text = f.read()
57
-
58
- memory = get_memory()
59
- memory[str(len(memory))] = f"Dokument ({os.path.basename(file.name)}): {text[:1500]}"
60
- update_memory(memory)
61
- return f"Erfolg: '{os.path.basename(file.name)}' wurde integriert."
62
-
63
  def chat_logic(message, history):
64
  memory = get_memory()
65
-
66
- # 1. Korrektur-Befehl
67
- if message.lower().startswith("korrektur:"):
68
- info = message.replace("korrektur:", "").strip()
69
- memory[str(len(memory))] = f"Manuelle Korrektur: {info}"
70
- update_memory(memory)
71
- history.append((message, "🧬 W-Vector Update: Wissen permanent verankert."))
72
- return "", history
73
-
74
- # 2. RAG & Delta I
75
  context = find_best_context(message, memory)
76
- input_text = f"<|system|>\nKontext: {context}</s>\n<|user|>\n{message}</s>\n<|assistant|>\n"
77
- inputs = tokenizer(input_text, return_tensors="pt")
78
 
79
- with torch.no_grad():
80
- outputs = model(**inputs, labels=inputs["input_ids"])
81
- delta_i = outputs.loss.item()
82
-
83
- # 3. Autonome Websuche
84
- search_triggered = False
85
- if delta_i > 2.0:
86
- search_triggered = True
87
- web_info = web_search(message)
88
- memory[str(len(memory))] = f"Web-Wissen: {web_info[:300]}"
89
- update_memory(memory)
90
- input_text = f"<|system|>\nWeb-Daten: {web_info[:300]}</s>\n<|user|>\n{message}</s>\n<|assistant|>\n"
91
- inputs = tokenizer(input_text, return_tensors="pt")
92
-
93
- # 4. Generierung
94
- gen_ids = model.generate(**inputs, max_new_tokens=150, temperature=0.7)
95
- answer = tokenizer.decode(gen_ids[0], skip_special_tokens=True).split("<|assistant|>\n")[-1]
96
 
97
- # 5. Output für Gradio (Tupel-Format für Stabilität)
98
- full_response = f"{answer}\n\n---\n📊 ΔI: {delta_i:.4f} | {'🌐 Web' if search_triggered else '🧠 Intern'}"
99
- history.append((message, full_response))
100
  return "", history
101
 
102
- # --- GRADIO INTERFACE ---
 
 
 
 
 
 
 
 
 
 
103
 
104
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
105
- gr.Markdown("# Isaac: Evolution 2.0 Master-Node")
106
 
107
- chatbot = gr.Chatbot(height=450, label="Isaac Chat")
 
108
 
109
  with gr.Row():
110
- msg = gr.Textbox(
111
- label="Deine Nachricht",
112
- placeholder="Hier tippen...",
113
- show_label=True,
114
- scale=4
115
- )
116
- submit_btn = gr.Button("Senden", variant="primary", scale=1)
117
 
118
  with gr.Row():
119
- with gr.Column():
120
- file_upload = gr.File(label="PDF/TXT Upload", file_types=[".pdf", ".txt"])
121
- with gr.Column():
122
- upload_status = gr.Textbox(label="Status", interactive=False)
123
 
124
- # Event-Handling für Enter und Button
125
  msg.submit(chat_logic, [msg, chatbot], [msg, chatbot])
126
- submit_btn.click(chat_logic, [msg, chatbot], [msg, chatbot])
127
-
128
- file_upload.upload(process_file, file_upload, upload_status)
129
 
130
  if __name__ == "__main__":
131
  demo.launch()
 
2
  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
 
13
  HF_TOKEN = os.getenv("HF_TOKEN")
14
  api = HfApi()
15
 
16
+ # Schnelles Modell für CPU (Gradio 6 kompatibel)
17
+ model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
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
  # --- FUNKTIONEN ---
 
30
  with open("memory.json", "w") as f: json.dump(new_memory, f)
31
  api.upload_file(path_or_fileobj="memory.json", path_in_repo="memory.json", repo_id=DATASET_REPO, repo_type="dataset", token=HF_TOKEN)
32
 
 
 
 
 
 
 
 
33
  def find_best_context(query, memory):
34
+ if not memory: return "Kein Vorwissen."
35
  passages = list(memory.values())
36
  query_emb = search_model.encode(query, convert_to_tensor=True)
37
  passage_embs = search_model.encode(passages, convert_to_tensor=True)
38
  hits = util.semantic_search(query_emb, passage_embs, top_k=1)
39
  return passages[hits[0][0]['corpus_id']]
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def chat_logic(message, history):
42
  memory = get_memory()
 
 
 
 
 
 
 
 
 
 
43
  context = find_best_context(message, memory)
 
 
44
 
45
+ # Prompt-Struktur
46
+ prompt = f"<|system|>\nKontext: {context}</s>\n<|user|>\n{message}</s>\n<|assistant|>\n"
47
+
48
+ # Generierung (max 50 Tokens für Speed auf CPU)
49
+ outputs = pipe(prompt, max_new_tokens=50, do_sample=True, temperature=0.7)
50
+ answer = outputs[0]["generated_text"].split("<|assistant|>\n")[-1]
51
+
52
+ # GRADIO 6 FORMAT: Liste aus Dictionaries
53
+ history.append({"role": "user", "content": message})
54
+ history.append({"role": "assistant", "content": answer})
 
 
 
 
 
 
 
55
 
 
 
 
56
  return "", history
57
 
58
+ def process_file(file):
59
+ if file is None: return "Keine Datei."
60
+ text = ""
61
+ doc = fitz.open(file.name)
62
+ for page in doc: text += page.get_text()
63
+ memory = get_memory()
64
+ memory[str(len(memory))] = f"Doc: {text[:500]}"
65
+ update_memory(memory)
66
+ return "Integriert."
67
+
68
+ # --- INTERFACE (GRADIO 6 OPTIMIERT) ---
69
 
70
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
71
+ gr.Markdown("# Isaac: Evolution 2.0")
72
 
73
+ # WICHTIG: type="messages" ist in Gradio 6 Pflicht für Dictionaries
74
+ chatbot = gr.Chatbot(height=450, label="Isaac Chat", type="messages")
75
 
76
  with gr.Row():
77
+ msg = gr.Textbox(label="Deine Nachricht", placeholder="Tippen...", scale=4)
78
+ btn = gr.Button("Senden", variant="primary", scale=1)
 
 
 
 
 
79
 
80
  with gr.Row():
81
+ upl = gr.File(label="PDF/TXT Upload", scale=1)
82
+ stat = gr.Textbox(label="Status", interactive=False, scale=1)
 
 
83
 
84
+ # Event-Handling
85
  msg.submit(chat_logic, [msg, chatbot], [msg, chatbot])
86
+ btn.click(chat_logic, [msg, chatbot], [msg, chatbot])
87
+ upl.upload(process_file, upl, stat)
 
88
 
89
  if __name__ == "__main__":
90
  demo.launch()