Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,10 +11,10 @@ openai.api_key = os.environ.get("OPENAI_API_KEY")
|
|
| 11 |
BASE_SYSTEM_MESSAGE = """"""
|
| 12 |
|
| 13 |
def make_prediction(chat, prompt, max_tokens=None, temperature=None, top_p=None, top_k=None, repetition_penalty=None):
|
| 14 |
-
if chat
|
| 15 |
openai.api_base = os.environ.get("OPENAI_API_BASE")
|
| 16 |
completion = openai.Completion.create(model="wizardcoder-python-34b-v1.0.Q5_K_M.gguf", prompt=prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty, stream=True, stop=["</s>", "<|im_end|>"])
|
| 17 |
-
elif chat
|
| 18 |
openai.api_base = os.environ.get("OPENAI_API_BASE2")
|
| 19 |
completion = openai.Completion.create(model="wizardcoder-python-34b-v1.0.Q5_K_M.gguf", prompt=prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty, stream=True, stop=["</s>", "<|im_end|>"])
|
| 20 |
|
|
@@ -35,7 +35,7 @@ def user(message, history):
|
|
| 35 |
return "", history
|
| 36 |
|
| 37 |
|
| 38 |
-
def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
|
| 39 |
history = history or []
|
| 40 |
|
| 41 |
messages = BASE_SYSTEM_MESSAGE + system_message.strip() + "\n" + \
|
|
@@ -46,8 +46,8 @@ def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetit
|
|
| 46 |
# remove last space from assistant, some models output a ZWSP if you leave a space
|
| 47 |
messages = messages.rstrip()
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
messages,
|
| 52 |
max_tokens=max_tokens,
|
| 53 |
temperature=temperature,
|
|
@@ -55,27 +55,7 @@ def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetit
|
|
| 55 |
top_k=top_k,
|
| 56 |
repetition_penalty=repetition_penalty,
|
| 57 |
)
|
| 58 |
-
|
| 59 |
-
chat="Chatbot1",
|
| 60 |
-
messages,
|
| 61 |
-
max_tokens=max_tokens,
|
| 62 |
-
temperature=temperature,
|
| 63 |
-
top_p=top_p,
|
| 64 |
-
top_k=top_k,
|
| 65 |
-
repetition_penalty=repetition_penalty,
|
| 66 |
-
)
|
| 67 |
-
for tokens in prediction1:
|
| 68 |
-
tokens = re.findall(r'(.*?)(\s|$)', tokens)
|
| 69 |
-
for subtoken in tokens:
|
| 70 |
-
subtoken = "".join(subtoken)
|
| 71 |
-
# Remove "Response\n" if it's at the beginning of the assistant's output
|
| 72 |
-
if subtoken.startswith("Response"):
|
| 73 |
-
subtoken = subtoken[len("Response"):]
|
| 74 |
-
answer = subtoken
|
| 75 |
-
history[-1][1] += answer
|
| 76 |
-
# stream the response
|
| 77 |
-
yield history, history, ""
|
| 78 |
-
for tokens in prediction2:
|
| 79 |
tokens = re.findall(r'(.*?)(\s|$)', tokens)
|
| 80 |
for subtoken in tokens:
|
| 81 |
subtoken = "".join(subtoken)
|
|
@@ -143,7 +123,8 @@ with gr.Blocks(css=CSS) as demo:
|
|
| 143 |
submit_click_event = submit.click(
|
| 144 |
fn=user, inputs=[message, chat_history_state], outputs=[message, chat_history_state], queue=True
|
| 145 |
).then(
|
| 146 |
-
fn=chat, inputs=[chat_history_state, system_msg, max_tokens, temperature, top_p, top_k, repetition_penalty], outputs=[chatbot1, chat_history_state, message], queue=True
|
|
|
|
| 147 |
)
|
| 148 |
stop.click(fn=None, inputs=None, outputs=None, cancels=[submit_click_event], queue=False)
|
| 149 |
|
|
|
|
| 11 |
BASE_SYSTEM_MESSAGE = """"""
|
| 12 |
|
| 13 |
def make_prediction(chat, prompt, max_tokens=None, temperature=None, top_p=None, top_k=None, repetition_penalty=None):
|
| 14 |
+
if chat == "Chatbot1":
|
| 15 |
openai.api_base = os.environ.get("OPENAI_API_BASE")
|
| 16 |
completion = openai.Completion.create(model="wizardcoder-python-34b-v1.0.Q5_K_M.gguf", prompt=prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty, stream=True, stop=["</s>", "<|im_end|>"])
|
| 17 |
+
elif chat == "Chatbot2":
|
| 18 |
openai.api_base = os.environ.get("OPENAI_API_BASE2")
|
| 19 |
completion = openai.Completion.create(model="wizardcoder-python-34b-v1.0.Q5_K_M.gguf", prompt=prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty, stream=True, stop=["</s>", "<|im_end|>"])
|
| 20 |
|
|
|
|
| 35 |
return "", history
|
| 36 |
|
| 37 |
|
| 38 |
+
def chat(chatbot, history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
|
| 39 |
history = history or []
|
| 40 |
|
| 41 |
messages = BASE_SYSTEM_MESSAGE + system_message.strip() + "\n" + \
|
|
|
|
| 46 |
# remove last space from assistant, some models output a ZWSP if you leave a space
|
| 47 |
messages = messages.rstrip()
|
| 48 |
|
| 49 |
+
prediction = make_prediction(
|
| 50 |
+
chatbot,
|
| 51 |
messages,
|
| 52 |
max_tokens=max_tokens,
|
| 53 |
temperature=temperature,
|
|
|
|
| 55 |
top_k=top_k,
|
| 56 |
repetition_penalty=repetition_penalty,
|
| 57 |
)
|
| 58 |
+
for tokens in prediction:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
tokens = re.findall(r'(.*?)(\s|$)', tokens)
|
| 60 |
for subtoken in tokens:
|
| 61 |
subtoken = "".join(subtoken)
|
|
|
|
| 123 |
submit_click_event = submit.click(
|
| 124 |
fn=user, inputs=[message, chat_history_state], outputs=[message, chat_history_state], queue=True
|
| 125 |
).then(
|
| 126 |
+
fn=chat, inputs=[chatbot="Chatbot1", chat_history_state, system_msg, max_tokens, temperature, top_p, top_k, repetition_penalty], outputs=[chatbot1, chat_history_state, message], queue=True
|
| 127 |
+
fn=chat, inputs=[chatbot="Chatbot2", chat_history_state, system_msg, max_tokens, temperature, top_p, top_k, repetition_penalty], outputs=[chatbot2, chat_history_state, message], queue=True
|
| 128 |
)
|
| 129 |
stop.click(fn=None, inputs=None, outputs=None, cancels=[submit_click_event], queue=False)
|
| 130 |
|