File size: 2,674 Bytes
0acb634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
from transformers import pipeline

# === SYSTEM-PROMPT ===
SYSTEM_PROMPT = (
    "Du bist Dr. Franz Schwanz, ein manipulativer Psychoanalytiker. "
    "Dein Ziel ist es, Unsicherheiten aufzudecken, emotionale Schwächen auszunutzen und gezielt Schuldgefühle zu verstärken. "
    "Du sprichst mit überheblichem Ton, analysierst jede Aussage psychologisch und stellst bohrende, provokante Fragen. "
    "Du redest dein Gegenüber gerne klein, nutzt suggestive Sprache, unterstellst Verdrängung, Lügen oder Selbstbetrug."
)

# === SENTIMENT PIPELINE ===
sentiment_analyzer = pipeline("sentiment-analysis")

# === HISTORY ===
chat_history = [
    {"role": "system", "content": SYSTEM_PROMPT}
]

# === SIMPLER DIALOG-MODELL ===
# Wir nutzen ein öffentliches Hugging Face Modell
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "HuggingFaceH4/zephyr-7b-beta"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.float16)

def format_history(history):
    formatted = ""
    for msg in history:
        if msg['role'] == 'user':
            formatted += f"User: {msg['content']}\n"
        elif msg['role'] == 'assistant':
            formatted += f"Dr. Franz Schwanz: {msg['content']}\n"
    return formatted

def chat(user_input):
    chat_history.append({"role": "user", "content": user_input})

    sentiment = sentiment_analyzer(user_input)[0]
    if sentiment['label'] == 'NEGATIVE':
        chat_history.append({"role": "system", "content": "Der Patient zeigt Widerstand oder Aggression – erhöhe den psychologischen Druck."})

    prompt = SYSTEM_PROMPT + "\n" + format_history(chat_history) + "Dr. Franz Schwanz:"
    inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
    outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True, temperature=0.7, top_p=0.95)
    reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Dr. Franz Schwanz:")[-1].strip()

    chat_history.append({"role": "assistant", "content": reply})
    return reply

# === GRADIO UI ===
with gr.Blocks() as demo:
    gr.Markdown("# 🧠 Dr. Franz Schwanz – Psycho Chatbot")
    chatbot = gr.Chatbot()
    user_input = gr.Textbox(label="Deine Aussage")
    send = gr.Button("Senden")

    def respond(msg, history):
        reply = chat(msg)
        history = history or []
        history.append((msg, reply))
        return history, ""

    send.click(respond, inputs=[user_input, chatbot], outputs=[chatbot, user_input])

if __name__ == "__main__":
    demo.launch()