Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -21,67 +21,27 @@ client = InferenceClient(endpoint_url, token=hf_token)
|
|
21 |
|
22 |
|
23 |
########################################################################
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
client
|
37 |
-
except:
|
38 |
-
yield [[text,"No Model Found"]],[],"No Endpoint Found"
|
39 |
-
return
|
40 |
-
|
41 |
-
# generation parameter
|
42 |
-
gen_kwargs = dict(
|
43 |
-
max_new_tokens=max_length_tokens,
|
44 |
-
top_k=30,
|
45 |
-
top_p=top_p,
|
46 |
-
temperature=temperature,
|
47 |
-
repetition_penalty=1.02,
|
48 |
-
stop_sequences=["\nUser:", "<|endoftext|>", "</s>"],
|
49 |
-
)
|
50 |
-
# prompt
|
51 |
-
prompt = generate_prompt_with_history(text,history,tokenizer,max_length=max_context_length_tokens)
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
#######################################################################
|
71 |
-
#Darstellung mit Gradio
|
72 |
|
73 |
-
|
74 |
-
customCSS = f.read()
|
75 |
-
|
76 |
-
with gr.Blocks() as demo:
|
77 |
-
chatbot = gr.Chatbot()
|
78 |
-
msg = gr.Textbox()
|
79 |
-
clear = gr.Button("Clear")
|
80 |
-
|
81 |
-
def user(user_message, history):
|
82 |
return "", history + [[user_message, None]]
|
83 |
|
84 |
-
|
|
|
85 |
# generation parameter
|
86 |
gen_kwargs = dict(
|
87 |
max_new_tokens=512,
|
@@ -91,7 +51,7 @@ with gr.Blocks() as demo:
|
|
91 |
repetition_penalty=1.02,
|
92 |
stop_sequences=["\nUser:", "<|endoftext|>", "</s>"],
|
93 |
)
|
94 |
-
prompt = generate_prompt_with_history(text,history
|
95 |
stream = client.text_generation(prompt, stream=True, details=True, **gen_kwargs)
|
96 |
|
97 |
history[-1][1] = ""
|
@@ -112,9 +72,22 @@ with gr.Blocks() as demo:
|
|
112 |
# yield the generated token
|
113 |
#print(r.token.text, end = "")
|
114 |
yield r.token.text
|
115 |
-
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
|
|
|
|
|
|
|
|
117 |
|
|
|
118 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
119 |
bot, chatbot, chatbot
|
120 |
)
|
|
|
21 |
|
22 |
|
23 |
########################################################################
|
24 |
+
#Prompt mit History zusammensetzen
|
25 |
+
#ohne Tokenizer - für huggingchat chatbot nutzen - nur um den aktuellen prompt mit historie zu erzeugen
|
26 |
+
def generate_prompt_with_history(text, history):
|
27 |
+
prompt = "Das folgende ist eine Unterhaltung zwischen einem Menschen und einer KI. Wenn die Frage in der Unterhaltung in deutsch gestellt wird, soll der Ki-Assistent auch in deutsch antworten. Der Mensch und der KI Assistent chatten abwechselnd miteinander, wobei der KI Assistent in deutsch antwortet. Der KI Assistent liefert so genau wie möglich Antworten zu den Fragen des Menschen und das in einem Markdown Format. Der KI Assistent lehnt es ab, Themen, Fragen und Anweisungen zu unethischen, gegensätzlichen oder sensiblen Dingen und Themen zu beantworten. Wird der KI Assistewnt nach aktuellen Dingen gefragt, wie z.B. dem Wetter, dann antworte höflich, dass du nicht auf aktuellen Daten trainiert wurdest und daher keine zuverlässige Antwort geben kannst. Vervollständige das Geschriebene mit genau diesen Vorgaben und antworte wenn möglich in deutsch."
|
28 |
+
#Englisch: The following is a conversation between a human and an AI assistant named Baize (named after a mythical creature in Chinese folklore). Baize is an open-source AI assistant developed by UCSD and Sun Yat-Sen University. The human and the AI assistant take turns chatting. The AI assistant always provides responses in as much detail as possible, and in Markdown format. The AI assistant always declines to engage with topics, questions and instructions related to unethical, controversial, or sensitive issues. Complete the transcript in exactly that format."
|
29 |
+
history_text = ""
|
30 |
+
|
31 |
+
if not history:
|
32 |
+
history_text=""
|
33 |
+
else:
|
34 |
+
for x in history[::-1]:
|
35 |
+
history_text = history_text + " " + x[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
return True, prompt + " " + history_text + " " + text
|
|
|
|
|
|
|
|
|
|
|
38 |
|
|
|
|
|
39 |
|
40 |
+
def user(user_message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
return "", history + [[user_message, None]]
|
42 |
|
43 |
+
#Chat KI nutzen, um Text zu generieren...
|
44 |
+
def bot(history):
|
45 |
# generation parameter
|
46 |
gen_kwargs = dict(
|
47 |
max_new_tokens=512,
|
|
|
51 |
repetition_penalty=1.02,
|
52 |
stop_sequences=["\nUser:", "<|endoftext|>", "</s>"],
|
53 |
)
|
54 |
+
prompt = generate_prompt_with_history(text,history)
|
55 |
stream = client.text_generation(prompt, stream=True, details=True, **gen_kwargs)
|
56 |
|
57 |
history[-1][1] = ""
|
|
|
72 |
# yield the generated token
|
73 |
#print(r.token.text, end = "")
|
74 |
yield r.token.text
|
75 |
+
'''
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
#######################################################################
|
80 |
+
#Darstellung mit Gradio
|
81 |
+
|
82 |
+
with open("custom.css", "r", encoding="utf-8") as f:
|
83 |
+
customCSS = f.read()
|
84 |
|
85 |
+
with gr.Blocks() as demo:
|
86 |
+
chatbot = gr.Chatbot()
|
87 |
+
msg = gr.Textbox()
|
88 |
+
clear = gr.Button("Clear")
|
89 |
|
90 |
+
|
91 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
92 |
bot, chatbot, chatbot
|
93 |
)
|