Spaces:
Sleeping
Sleeping
Update app/model.py
Browse files- app/model.py +16 -25
app/model.py
CHANGED
|
@@ -1,35 +1,34 @@
|
|
| 1 |
# model.py
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
-
from llama_cpp import Llama
|
| 5 |
|
| 6 |
# --- Globale Variablen für das LLM ---
|
| 7 |
llm = None
|
| 8 |
model_loaded_successfully = False
|
| 9 |
|
| 10 |
# --- Konfiguration ---
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
GGUF_FILENAME = "mistral-7b-instruct-v0.3.Q4_K_M.gguf" # Beispiel
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# Stelle sicher, dass HF_HOME und HF_TOKEN von main.py oder Space Secrets korrekt gesetzt sind
|
| 18 |
-
# HF_TOKEN wird für den Download der GGUF-Datei vom Hub benötigt, wenn sie nicht öffentlich ist oder du Ratenlimits umgehen willst.
|
| 19 |
-
# HF_HOME wird für den Cache der heruntergeladenen GGUF-Datei verwendet.
|
| 20 |
print(f"model.py: HF_HOME ist (sollte sein): {os.environ.get('HF_HOME')}")
|
| 21 |
print(f"model.py: HF_TOKEN ist vorhanden: {'Ja' if os.environ.get('HF_TOKEN') else 'Nein'}")
|
| 22 |
|
| 23 |
-
print(f"Versuche, GGUF-Modell zu laden: {GGUF_REPO_ID}/{GGUF_FILENAME}")
|
| 24 |
|
| 25 |
try:
|
| 26 |
llm = Llama.from_pretrained(
|
| 27 |
repo_id=GGUF_REPO_ID,
|
| 28 |
filename=GGUF_FILENAME,
|
| 29 |
-
n_ctx=2048,
|
| 30 |
-
n_gpu_layers=0,
|
| 31 |
-
|
| 32 |
-
verbose=True # Gibt mehr Log-Ausgaben beim Laden
|
| 33 |
)
|
| 34 |
print(f"GGUF-Modell {GGUF_FILENAME} erfolgreich geladen.")
|
| 35 |
model_loaded_successfully = True
|
|
@@ -37,29 +36,22 @@ except Exception as e:
|
|
| 37 |
print(f"FATAL: Fehler beim Laden des GGUF-Modells: {type(e).__name__} - {e}", file=sys.stderr)
|
| 38 |
model_loaded_successfully = False
|
| 39 |
|
| 40 |
-
|
| 41 |
# --- Hauptfunktion zur Textgenerierung ---
|
| 42 |
def generate_response(full_prompt: str) -> str:
|
| 43 |
if not model_loaded_successfully or not llm:
|
| 44 |
return "Entschuldigung, das Sprachmodell ist derzeit nicht verfügbar (GGUF). Bitte überprüfe die Server-Logs."
|
| 45 |
|
| 46 |
-
# Prompt-Formatierung für Mistral Instruct GGUF-Modelle:
|
| 47 |
-
# Oft ist es: <s>[INST] System Prompt (optional) User Prompt [/INST] Antwort des Modells</s>
|
| 48 |
-
# Wenn dein full_prompt bereits "System\n\nUser: User" ist, musst du es anpassen.
|
| 49 |
-
# Annahme hier: full_prompt ist die gesamte Eingabe, die zwischen [INST] und [/INST] soll.
|
| 50 |
prompt_templated = f"<s>[INST] {full_prompt.strip()} [/INST]"
|
| 51 |
|
| 52 |
print(f"Generiere Antwort für GGUF-Prompt (erste 100 Zeichen): {prompt_templated[:100]}...")
|
| 53 |
try:
|
| 54 |
output = llm(
|
| 55 |
prompt_templated,
|
| 56 |
-
max_tokens=
|
| 57 |
-
stop=["</s>", "[INST]", "\nUser:"
|
| 58 |
-
echo=False,
|
| 59 |
temperature=0.7,
|
| 60 |
-
# top_p=0.9,
|
| 61 |
)
|
| 62 |
-
|
| 63 |
response_text = output["choices"][0]["text"]
|
| 64 |
print(f"Generierte GGUF-Antwort (erste 100 Zeichen): {response_text[:100]}...")
|
| 65 |
return response_text.strip()
|
|
@@ -68,6 +60,5 @@ def generate_response(full_prompt: str) -> str:
|
|
| 68 |
print(error_gen_output, file=sys.stderr)
|
| 69 |
return "Entschuldigung, bei der Antwortgenerierung mit GGUF ist ein interner Fehler aufgetreten."
|
| 70 |
|
| 71 |
-
|
| 72 |
if not model_loaded_successfully:
|
| 73 |
print("WARNUNG: Das GGUF-Sprachmodell wurde beim Start nicht erfolgreich geladen.", file=sys.stderr)
|
|
|
|
| 1 |
# model.py
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
|
| 6 |
# --- Globale Variablen für das LLM ---
|
| 7 |
llm = None
|
| 8 |
model_loaded_successfully = False
|
| 9 |
|
| 10 |
# --- Konfiguration ---
|
| 11 |
+
# KORREKTE REPO_ID VERWENDEN:
|
| 12 |
+
GGUF_REPO_ID = "MaziyarPanahi/Mistral-7B-Instruct-v0.3-GGUF" # <--- HIER KORRIGIERT!
|
| 13 |
+
|
| 14 |
+
# Wähle immer noch deine gewünschte Quantisierungsdatei aus dem MaziyarPanahi Repo:
|
| 15 |
+
GGUF_FILENAME = "mistral-7b-instruct-v0.3.Q4_K_M.gguf" # Beispiel: Q4_K_M
|
| 16 |
+
# Stelle sicher, dass dieser Dateiname im MaziyarPanahi Repo existiert!
|
| 17 |
+
# Gehe auf https://huggingface.co/MaziyarPanahi/Mistral-7B-Instruct-v0.3-GGUF/tree/main
|
| 18 |
+
# um die verfügbaren .gguf Dateien zu sehen.
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
print(f"model.py: HF_HOME ist (sollte sein): {os.environ.get('HF_HOME')}")
|
| 21 |
print(f"model.py: HF_TOKEN ist vorhanden: {'Ja' if os.environ.get('HF_TOKEN') else 'Nein'}")
|
| 22 |
|
| 23 |
+
print(f"Versuche, GGUF-Modell zu laden von: {GGUF_REPO_ID}/{GGUF_FILENAME}")
|
| 24 |
|
| 25 |
try:
|
| 26 |
llm = Llama.from_pretrained(
|
| 27 |
repo_id=GGUF_REPO_ID,
|
| 28 |
filename=GGUF_FILENAME,
|
| 29 |
+
n_ctx=2048,
|
| 30 |
+
n_gpu_layers=0,
|
| 31 |
+
verbose=True
|
|
|
|
| 32 |
)
|
| 33 |
print(f"GGUF-Modell {GGUF_FILENAME} erfolgreich geladen.")
|
| 34 |
model_loaded_successfully = True
|
|
|
|
| 36 |
print(f"FATAL: Fehler beim Laden des GGUF-Modells: {type(e).__name__} - {e}", file=sys.stderr)
|
| 37 |
model_loaded_successfully = False
|
| 38 |
|
|
|
|
| 39 |
# --- Hauptfunktion zur Textgenerierung ---
|
| 40 |
def generate_response(full_prompt: str) -> str:
|
| 41 |
if not model_loaded_successfully or not llm:
|
| 42 |
return "Entschuldigung, das Sprachmodell ist derzeit nicht verfügbar (GGUF). Bitte überprüfe die Server-Logs."
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
prompt_templated = f"<s>[INST] {full_prompt.strip()} [/INST]"
|
| 45 |
|
| 46 |
print(f"Generiere Antwort für GGUF-Prompt (erste 100 Zeichen): {prompt_templated[:100]}...")
|
| 47 |
try:
|
| 48 |
output = llm(
|
| 49 |
prompt_templated,
|
| 50 |
+
max_tokens=250,
|
| 51 |
+
stop=["</s>", "[INST]", "\nUser:", "\nNutzer:"],
|
| 52 |
+
echo=False,
|
| 53 |
temperature=0.7,
|
|
|
|
| 54 |
)
|
|
|
|
| 55 |
response_text = output["choices"][0]["text"]
|
| 56 |
print(f"Generierte GGUF-Antwort (erste 100 Zeichen): {response_text[:100]}...")
|
| 57 |
return response_text.strip()
|
|
|
|
| 60 |
print(error_gen_output, file=sys.stderr)
|
| 61 |
return "Entschuldigung, bei der Antwortgenerierung mit GGUF ist ein interner Fehler aufgetreten."
|
| 62 |
|
|
|
|
| 63 |
if not model_loaded_successfully:
|
| 64 |
print("WARNUNG: Das GGUF-Sprachmodell wurde beim Start nicht erfolgreich geladen.", file=sys.stderr)
|