Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,51 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from threading import Thread
|
| 3 |
-
from typing import Iterator
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
-
import
|
| 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 |
-
generate_kwargs = dict(
|
| 39 |
-
input_ids=input_ids["input_ids"],
|
| 40 |
-
max_length=input_ids["input_ids"].shape[1] + max_new_tokens,
|
| 41 |
-
temperature=temperature,
|
| 42 |
-
top_p=top_p,
|
| 43 |
-
top_k=top_k,
|
| 44 |
-
repetition_penalty=repetition_penalty,
|
| 45 |
-
pad_token_id=tokenizer.eos_token_id
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
return
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
fn=
|
| 54 |
-
inputs=
|
| 55 |
-
gr.Textbox(label="Message"),
|
| 56 |
-
gr.JSON(label="Chat History"),
|
| 57 |
-
gr.Textbox(label="System Prompt", lines=2),
|
| 58 |
-
gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS),
|
| 59 |
-
gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, step=0.1, value=0.6),
|
| 60 |
-
gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.9),
|
| 61 |
-
gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50),
|
| 62 |
-
gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 63 |
-
],
|
| 64 |
outputs="text",
|
| 65 |
-
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
-
# Starten des
|
| 69 |
-
|
| 70 |
-
chat_interface.launch()
|
| 71 |
-
|
| 72 |
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Initialisierung des Modells und des Tokenizers
|
| 6 |
+
tokenizer = GPT2Tokenizer.from_pretrained("Loewolf/GPT_1")
|
| 7 |
+
model = GPT2LMHeadModel.from_pretrained("Loewolf/GPT_1")
|
| 8 |
+
|
| 9 |
+
def generate_text(prompt):
|
| 10 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
| 11 |
+
|
| 12 |
+
# Erstelle eine Attention-Mask, die überall '1' ist
|
| 13 |
+
attention_mask = torch.ones(input_ids.shape, dtype=torch.bool)
|
| 14 |
+
|
| 15 |
+
# Bestimmung der maximalen Länge
|
| 16 |
+
max_length = model.config.n_positions if len(input_ids[0]) > model.config.n_positions else len(input_ids[0]) + 20
|
| 17 |
+
|
| 18 |
+
# Erzeugen von Text mit spezifischen Parametern
|
| 19 |
+
beam_output = model.generate(
|
| 20 |
+
input_ids,
|
| 21 |
+
attention_mask=attention_mask,
|
| 22 |
+
max_length=max_length,
|
| 23 |
+
min_length=4, # Mindestlänge der Antwort
|
| 24 |
+
num_beams=5,
|
| 25 |
+
no_repeat_ngram_size=2,
|
| 26 |
+
early_stopping=True,
|
| 27 |
+
temperature=0.9,
|
| 28 |
+
top_p=0.90,
|
| 29 |
+
top_k=50,
|
| 30 |
+
length_penalty=2.0,
|
| 31 |
+
do_sample=True,
|
| 32 |
+
eos_token_id=tokenizer.eos_token_id, # EOS Token setzen
|
| 33 |
+
pad_token_id=tokenizer.eos_token_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
+
text = tokenizer.decode(beam_output[0], skip_special_tokens=True)
|
| 37 |
+
return text
|
| 38 |
|
| 39 |
+
# Erstellung des Chatbot-Interface mit dem Titel "Löwolf Chat"
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=generate_text,
|
| 42 |
+
inputs=gr.Textbox(label="Schreibe hier...", placeholder="Stelle deine Frage..."),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
outputs="text",
|
| 44 |
+
title="Löwolf Chat",
|
| 45 |
+
description="Willkommen beim Löwolf Chat. Stelle deine Fragen und erhalte Antworten vom KI-Chatbot."
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Starten des Chatbot-Interfaces
|
| 49 |
+
iface.launch()
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|