Update app.py
Browse files
app.py
CHANGED
|
@@ -2,47 +2,70 @@ from transformers import pipeline
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
MODELS = {
|
|
|
|
| 5 |
"German GPT": "dbmdz/german-gpt2",
|
| 6 |
-
"
|
| 7 |
-
"GPT2": "gpt2"
|
| 8 |
}
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def respond(message, history, model_choice):
|
| 11 |
try:
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
for human, assistant in history:
|
| 15 |
-
prompt += f"Mensch: {human}\nKI: {assistant}\n"
|
| 16 |
-
prompt += f"Mensch: {message}\nKI:"
|
| 17 |
|
| 18 |
# Pipeline mit optimierten Parametern
|
| 19 |
generator = pipeline(
|
| 20 |
'text-generation',
|
| 21 |
model=MODELS[model_choice],
|
| 22 |
-
max_new_tokens=
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
-
# Antwort generieren
|
| 28 |
-
|
| 29 |
-
response = full_response.split("KI:")[-1].split("\n")[0].strip()
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"❌ Fehler: {str(e)}"
|
| 35 |
|
|
|
|
| 36 |
gr.ChatInterface(
|
| 37 |
respond,
|
| 38 |
additional_inputs=[
|
| 39 |
gr.Dropdown(
|
| 40 |
list(MODELS.keys()),
|
| 41 |
label="Modell",
|
| 42 |
-
value="
|
| 43 |
-
info="
|
| 44 |
)
|
| 45 |
],
|
| 46 |
-
title="
|
| 47 |
-
description="Wähle '
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
).launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
MODELS = {
|
| 5 |
+
"TinyLlama-Chat": "TinyLlama/TinyLlama-1.1B-Chat-v1.0", # Chat-optimiert!
|
| 6 |
"German GPT": "dbmdz/german-gpt2",
|
| 7 |
+
"Mistral-Tiny": "alpindale/Mistral-7B-v0.1-hf" # Kleinere Mistral-Variante
|
|
|
|
| 8 |
}
|
| 9 |
|
| 10 |
+
def format_prompt(history, new_message, model_choice):
|
| 11 |
+
"""Erstellt modellspezifische Prompt-Formate"""
|
| 12 |
+
if "TinyLlama" in model_choice:
|
| 13 |
+
# TinyLlama benötigt spezielles Format
|
| 14 |
+
prompt = "<|system|>\nDu bist ein hilfsbereiter Assistent.</s>\n"
|
| 15 |
+
for human, assistant in history:
|
| 16 |
+
prompt += f"<|user|>\n{human}</s>\n<|assistant|>\n{assistant}</s>\n"
|
| 17 |
+
prompt += f"<|user|>\n{new_message}</s>\n<|assistant|>\n"
|
| 18 |
+
return prompt
|
| 19 |
+
|
| 20 |
+
# Allgemeines Format für andere Modelle
|
| 21 |
+
prompt = ""
|
| 22 |
+
for human, assistant in history:
|
| 23 |
+
prompt += f"### Mensch: {human}\n### KI: {assistant}\n"
|
| 24 |
+
prompt += f"### Mensch: {new_message}\n### KI:"
|
| 25 |
+
return prompt
|
| 26 |
+
|
| 27 |
def respond(message, history, model_choice):
|
| 28 |
try:
|
| 29 |
+
# Kontext-basierten Prompt erstellen
|
| 30 |
+
full_prompt = format_prompt(history, message, model_choice)
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Pipeline mit optimierten Parametern
|
| 33 |
generator = pipeline(
|
| 34 |
'text-generation',
|
| 35 |
model=MODELS[model_choice],
|
| 36 |
+
max_new_tokens=150,
|
| 37 |
+
temperature=0.8, # Mehr Kreativität
|
| 38 |
+
top_p=0.9,
|
| 39 |
+
repetition_penalty=1.2 # Verhindert Wiederholungen
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Antwort generieren
|
| 43 |
+
response = generator(full_prompt)[0]['generated_text']
|
|
|
|
| 44 |
|
| 45 |
+
# Modellspezifische Antwort-Extraktion
|
| 46 |
+
if "TinyLlama" in model_choice:
|
| 47 |
+
return response.split("<|assistant|>")[-1].strip()
|
| 48 |
+
else:
|
| 49 |
+
return response.split("### KI:")[-1].split("###")[0].strip()
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
+
return f"❌ Fehler: {str(e)}\n\nTipp: Probier ein anderes Modell oder kürzere Eingaben"
|
| 53 |
|
| 54 |
+
# UI mit besseren Erklärungen
|
| 55 |
gr.ChatInterface(
|
| 56 |
respond,
|
| 57 |
additional_inputs=[
|
| 58 |
gr.Dropdown(
|
| 59 |
list(MODELS.keys()),
|
| 60 |
label="Modell",
|
| 61 |
+
value="TinyLlama-Chat",
|
| 62 |
+
info="TinyLlama-Chat für beste Dialoge"
|
| 63 |
)
|
| 64 |
],
|
| 65 |
+
title="🚀 Verbesserter Kontext-Chat",
|
| 66 |
+
description="Wähle 'TinyLlama-Chat' & schreibe ganze Sätze!",
|
| 67 |
+
examples=[
|
| 68 |
+
["Was ist deine Lieblingsfarbe und warum?"],
|
| 69 |
+
["Erkläre Quantenphysik in 3 Sätzen"]
|
| 70 |
+
]
|
| 71 |
).launch()
|