File size: 1,655 Bytes
f863056
f372d1e
a727207
f372d1e
 
ede33c6
 
f372d1e
a727207
2909fb3
 
 
6a1cc2f
2909fb3
 
6a1cc2f
2909fb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82c02cc
db924a3
a727207
54e1be2
a727207
4429d9b
a727207
 
d163b82
54e1be2
a727207
 
82c02cc
a727207
4429d9b
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
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch

# Initialisierung des Modells und des Tokenizers
tokenizer = GPT2Tokenizer.from_pretrained("Loewolf/GPT_1")
model = GPT2LMHeadModel.from_pretrained("Loewolf/GPT_1")

def generate_text(prompt):
    input_ids = tokenizer.encode(prompt, return_tensors="pt")

    # Erstelle eine Attention-Mask, die überall '1' ist
    attention_mask = torch.ones(input_ids.shape, dtype=torch.long)

    # Bestimmung der maximalen Länge
    max_length = model.config.n_positions if len(input_ids[0]) > model.config.n_positions else len(input_ids[0]) + 100

    # Erzeugen von Text mit spezifischen Parametern
    beam_output = model.generate(
        input_ids,
        attention_mask=attention_mask,
        max_length=max_length,
        min_length=4,  # Mindestlänge der Antwort
        num_beams=5,
        no_repeat_ngram_size=2,
        early_stopping=True,
        temperature=0.9,
        top_p=0.90,
        top_k=50,
        length_penalty=2.0,
        do_sample=True,
        eos_token_id=tokenizer.eos_token_id,  # EOS Token setzen
        pad_token_id=tokenizer.eos_token_id 
    )

    text = tokenizer.decode(beam_output[0], skip_special_tokens=True)
    return text

# Erstellung des Chatbot-Interface mit dem Titel "Löwolf Chat"
iface = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(label="Schreibe hier...", placeholder="Stelle deine Frage..."),
    outputs="text",
    title="Löwolf Chat",
    description="Willkommen beim Löwolf Chat. Stelle deine Fragen und erhalte Antworten vom KI-Chatbot."
)

# Starten des Chatbot-Interfaces
iface.launch()