Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,59 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
model
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Erstellen der Gradio-Schnittstelle
|
23 |
with gr.Blocks() as demo:
|
24 |
with gr.Row():
|
25 |
with gr.Column():
|
26 |
-
history = gr.Textbox(label="Chatverlauf", lines=10, interactive=False)
|
27 |
user_input = gr.Textbox(label="Deine Nachricht")
|
28 |
submit_btn = gr.Button("Senden")
|
29 |
with gr.Column():
|
30 |
-
|
31 |
-
temperature = gr.Slider(minimum=0, maximum=1, step=0.01, label="Temperature", value=0.9)
|
32 |
top_k = gr.Slider(minimum=0, maximum=100, step=1, label="Top K", value=50)
|
33 |
top_p = gr.Slider(minimum=0, maximum=1, step=0.01, label="Top P", value=0.9)
|
34 |
-
|
35 |
|
36 |
submit_btn.click(
|
37 |
-
|
38 |
-
inputs=[user_input, history, temperature, top_k, top_p,
|
39 |
-
outputs=[
|
40 |
)
|
41 |
|
42 |
# Starten der Gradio-App
|
43 |
demo.launch()
|
44 |
|
45 |
-
|
|
|
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/GPT1")
|
7 |
+
model = GPT2LMHeadModel.from_pretrained("Loewolf/GPT1")
|
8 |
+
model.to("cpu") # Stellen Sie sicher, dass das Modell auf der CPU läuft
|
9 |
+
|
10 |
+
# Chat-Verlauf initialisieren
|
11 |
+
chat_history = []
|
12 |
+
|
13 |
+
def generate_text(input_text, history, temperature, top_k, top_p, max_length):
|
14 |
+
global chat_history
|
15 |
+
# Hinzufügen der neuen Eingabe zum Chat-Verlauf
|
16 |
+
chat_history.append(f"Nutzer: {input_text}")
|
17 |
+
new_input = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors='pt')
|
18 |
+
new_input = new_input.to("cpu") # Stellen Sie sicher, dass Tensoren auf der CPU sind
|
19 |
+
|
20 |
+
# Generieren der Antwort des Modells
|
21 |
+
chat_history_ids = tokenizer.encode(" ".join(chat_history) + tokenizer.eos_token, return_tensors='pt')
|
22 |
+
chat_history_ids = chat_history_ids.to("cpu")
|
23 |
+
|
24 |
+
# Achten Sie auf die Größe der Tokens für das Modell
|
25 |
+
chat_history_ids = chat_history_ids[:, -tokenizer.model_max_length :]
|
26 |
+
|
27 |
+
# Generieren der Antwort
|
28 |
+
model_output = model.generate(chat_history_ids, max_length=max_length, pad_token_id=tokenizer.eos_token_id,
|
29 |
+
temperature=temperature, top_k=top_k, top_p=top_p)
|
30 |
+
|
31 |
+
# Antwort in Text umwandeln
|
32 |
+
response = tokenizer.decode(model_output[:, chat_history_ids.shape[-1]:][0], skip_special_tokens=True)
|
33 |
+
chat_history.append(f"Löwolf GPT: {response}")
|
34 |
+
|
35 |
+
# Rückgabe des aktualisierten Chat-Verlaufs
|
36 |
+
return " ".join(chat_history)
|
37 |
|
38 |
# Erstellen der Gradio-Schnittstelle
|
39 |
with gr.Blocks() as demo:
|
40 |
with gr.Row():
|
41 |
with gr.Column():
|
42 |
+
history = gr.Textbox(label="Chatverlauf", value=" ".join(chat_history), lines=10, interactive=False)
|
43 |
user_input = gr.Textbox(label="Deine Nachricht")
|
44 |
submit_btn = gr.Button("Senden")
|
45 |
with gr.Column():
|
46 |
+
temperature = gr.Slider(minimum=0, maximum=1, step=0.01, label="Temperature", value=0.7)
|
|
|
47 |
top_k = gr.Slider(minimum=0, maximum=100, step=1, label="Top K", value=50)
|
48 |
top_p = gr.Slider(minimum=0, maximum=1, step=0.01, label="Top P", value=0.9)
|
49 |
+
max_length = gr.Slider(minimum=1, maximum=100, step=1, label="Maximale Länge", value=60)
|
50 |
|
51 |
submit_btn.click(
|
52 |
+
generate_text,
|
53 |
+
inputs=[user_input, history, temperature, top_k, top_p, max_length],
|
54 |
+
outputs=[history]
|
55 |
)
|
56 |
|
57 |
# Starten der Gradio-App
|
58 |
demo.launch()
|
59 |
|
|