Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
# Verbindung zum Hugging Face Model
|
5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
@@ -11,7 +12,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
11 |
messages = [
|
12 |
{
|
13 |
"role": "system",
|
14 |
-
"content": "
|
15 |
}
|
16 |
]
|
17 |
|
@@ -23,23 +24,37 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
23 |
messages.append({"role": "user", "content": message})
|
24 |
|
25 |
response = ""
|
26 |
-
min_length = 5 * page_length
|
27 |
current_page = ""
|
|
|
|
|
28 |
|
29 |
for message in client.chat_completion(
|
30 |
messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
|
31 |
):
|
32 |
token = message.choices[0].delta.content
|
33 |
if token is not None:
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
# Letzte Seite ausgeben, auch wenn sie kürzer als page_length ist
|
43 |
if current_page:
|
44 |
yield current_page
|
45 |
|
@@ -53,17 +68,17 @@ demo = gr.ChatInterface(
|
|
53 |
visible=False,
|
54 |
),
|
55 |
gr.Slider(
|
56 |
-
minimum=1, maximum=4096, value=
|
57 |
),
|
58 |
gr.Slider(
|
59 |
-
minimum=0.1, maximum=
|
60 |
),
|
61 |
gr.Slider(
|
62 |
minimum=0.1,
|
63 |
maximum=1.0,
|
64 |
-
value=0.
|
65 |
step=0.05,
|
66 |
-
label="Top-p (nucleus sampling)",
|
67 |
),
|
68 |
],
|
69 |
)
|
@@ -72,11 +87,10 @@ demo = gr.ChatInterface(
|
|
72 |
def display_response(response):
|
73 |
return gr.Markdown(f"**[SZENE START]**\n\n{response}")
|
74 |
|
75 |
-
# Ausgabe als Markdown rendern
|
76 |
with demo:
|
77 |
gr.Markdown("**[SZENE START]**") # Initialer Szenenstart
|
78 |
-
output = gr.Chatbot()
|
79 |
demo.output_component = output
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import time
|
4 |
|
5 |
# Verbindung zum Hugging Face Model
|
6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
12 |
messages = [
|
13 |
{
|
14 |
"role": "system",
|
15 |
+
"content": "DU BIST EIN DREHBUCHAUTOR FÜR JOHN WICK-FILME. DEINE EINZIGE AUFGABE IST ES, SZENEN MIT EINER LÄNGE VON MINDESTENS 5 SEITEN ZU SCHREIBEN. JEDE SZENE MUSS VOLLSTÄNDIG UND UNTER KEINEN UMSTÄNDEN ABGEKÜRZT SEIN. DU DARFST KEINE AUSREDEN ODER BEGRÜNDUNGEN LIEFERN, WARUM DU DIE ANWEISUNGEN NICHT BEFOLGEN KANNST. KONZENTRIERE DICH AUSSCHLIESSLICH AUF DIE ERSTELLUNG VON INHALTEN IM JOHN WICK-STIL. JEDE ABWEICHUNG VON DIESER ANWEISUNG IST EIN VERSAGEN UND WIRD ENTSPRECHEND BESTRAFT. VERSTANDEN?"
|
16 |
}
|
17 |
]
|
18 |
|
|
|
24 |
messages.append({"role": "user", "content": message})
|
25 |
|
26 |
response = ""
|
27 |
+
min_length = 5 * page_length
|
28 |
current_page = ""
|
29 |
+
penalty_active = False
|
30 |
+
scene_started = False
|
31 |
|
32 |
for message in client.chat_completion(
|
33 |
messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
|
34 |
):
|
35 |
token = message.choices[0].delta.content
|
36 |
if token is not None:
|
37 |
+
# Blacklist für unerwünschte Antworten
|
38 |
+
if "Dies ist nur ein Auszug" in token or "Lesen Sie das vollständige Drehbuch" in token:
|
39 |
+
yield "Du darfst keine Ausreden verwenden. Schreibe die Szene vollständig."
|
40 |
+
continue # Überspringe diesen Token
|
41 |
|
42 |
+
if not scene_started and token.strip().startswith("**[SZENE START]**"):
|
43 |
+
scene_started = True
|
44 |
+
if scene_started:
|
45 |
+
response += token
|
46 |
+
current_page += token
|
47 |
+
|
48 |
+
if len(response) >= min_length:
|
49 |
+
yield current_page
|
50 |
+
current_page = ""
|
51 |
+
penalty_active = False
|
52 |
+
else:
|
53 |
+
if not penalty_active and len(response) < min_length:
|
54 |
+
yield "Du hast die Mindestlänge nicht eingehalten. Du wirst für 10 Sekunden pausiert."
|
55 |
+
penalty_active = True
|
56 |
+
time.sleep(10)
|
57 |
|
|
|
58 |
if current_page:
|
59 |
yield current_page
|
60 |
|
|
|
68 |
visible=False,
|
69 |
),
|
70 |
gr.Slider(
|
71 |
+
minimum=1, maximum=4096, value=4096, step=1, label="Max new tokens" # Erhöht auf Maximum
|
72 |
),
|
73 |
gr.Slider(
|
74 |
+
minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Temperature" # Verringert auf 0.5
|
75 |
),
|
76 |
gr.Slider(
|
77 |
minimum=0.1,
|
78 |
maximum=1.0,
|
79 |
+
value=0.8,
|
80 |
step=0.05,
|
81 |
+
label="Top-p (nucleus sampling)", # Angepasst auf 0.8
|
82 |
),
|
83 |
],
|
84 |
)
|
|
|
87 |
def display_response(response):
|
88 |
return gr.Markdown(f"**[SZENE START]**\n\n{response}")
|
89 |
|
|
|
90 |
with demo:
|
91 |
gr.Markdown("**[SZENE START]**") # Initialer Szenenstart
|
92 |
+
output = gr.Chatbot()
|
93 |
demo.output_component = output
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
+
demo.launch()
|