Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import random
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
from mySystemPrompt import SYSTEM_PROMPT
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
# Model which is used
|
| 9 |
checkpoint = "CohereForAI/c4ai-command-r-plus"
|
|
|
|
|
|
|
| 10 |
# Inference client with the model (And HF-token if needed)
|
| 11 |
client = InferenceClient(checkpoint)
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
return newPrompt
|
| 28 |
|
|
|
|
| 29 |
def inference(message, history, systemPrompt=SYSTEM_PROMPT, temperature=0.9, maxTokens=512, topP=0.9, repPenalty=1.1):
|
| 30 |
# Updating the settings for the generation
|
| 31 |
client_settings = dict(
|
|
@@ -41,16 +48,22 @@ def inference(message, history, systemPrompt=SYSTEM_PROMPT, temperature=0.9, max
|
|
| 41 |
)
|
| 42 |
# Generating the response by passing the prompt in right format plus the client settings
|
| 43 |
stream = client.text_generation(format_prompt(message, history, systemPrompt),
|
| 44 |
-
|
| 45 |
# Reading the stream
|
| 46 |
partial_response = ""
|
| 47 |
for stream_part in stream:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
myAdditionalInputs = [
|
|
@@ -105,6 +118,7 @@ myChatbot = gr.Chatbot(avatar_images=["./ava_m.png", "./avatar_franzi.jpg"],
|
|
| 105 |
show_copy_button=False,
|
| 106 |
likeable=True)
|
| 107 |
|
|
|
|
| 108 |
myTextInput = gr.Textbox(lines=2,
|
| 109 |
max_lines=2,
|
| 110 |
placeholder="Send a message",
|
|
@@ -129,16 +143,19 @@ myClearButton = gr.Button(value="CLEAR",
|
|
| 129 |
size="sm")
|
| 130 |
|
| 131 |
|
| 132 |
-
gr.ChatInterface(
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient, TextGenerationStreamOutput
|
| 3 |
import random
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
from mySystemPrompt import SYSTEM_PROMPT
|
| 6 |
+
from datetime import datetime
|
| 7 |
|
| 8 |
|
| 9 |
# Model which is used
|
| 10 |
checkpoint = "CohereForAI/c4ai-command-r-plus"
|
| 11 |
+
checkpoint = "mistral-community/Mixtral-8x22B-v0.1"
|
| 12 |
+
path_to_log = "log.txt"
|
| 13 |
# Inference client with the model (And HF-token if needed)
|
| 14 |
client = InferenceClient(checkpoint)
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 16 |
+
|
| 17 |
+
if checkpoint == "mistral-community/Mixtral-8x22B-v0.1":
|
| 18 |
+
# Tokenizer chat template correction(Only works for mistral models)
|
| 19 |
+
chat_template = open("mistral-instruct.jinja").read()
|
| 20 |
+
chat_template = chat_template.replace(' ', '').replace('\n', '')
|
| 21 |
+
tokenizer.chat_template = chat_template
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def format_prompt(message, chatbot, system_prompt):
|
| 25 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 26 |
+
for user_message, bot_message in chatbot:
|
| 27 |
+
messages.append({"role": "user", "content": user_message})
|
| 28 |
+
messages.append({"role": "assistant", "content": bot_message})
|
| 29 |
+
messages.append({"role": "user", "content": message})
|
| 30 |
+
newPrompt = tokenizer.apply_chat_template(
|
| 31 |
+
messages, tokenize=False, add_generation_prompt=True, return_tensors="pt")
|
| 32 |
+
# newPrompt = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
|
| 33 |
return newPrompt
|
| 34 |
|
| 35 |
+
|
| 36 |
def inference(message, history, systemPrompt=SYSTEM_PROMPT, temperature=0.9, maxTokens=512, topP=0.9, repPenalty=1.1):
|
| 37 |
# Updating the settings for the generation
|
| 38 |
client_settings = dict(
|
|
|
|
| 48 |
)
|
| 49 |
# Generating the response by passing the prompt in right format plus the client settings
|
| 50 |
stream = client.text_generation(format_prompt(message, history, systemPrompt),
|
| 51 |
+
**client_settings)
|
| 52 |
# Reading the stream
|
| 53 |
partial_response = ""
|
| 54 |
for stream_part in stream:
|
| 55 |
+
if not stream_part.token.special:
|
| 56 |
+
partial_response += stream_part.token.text
|
| 57 |
+
yield partial_response
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def event_voting(vote_data: gr.LikeData):
|
| 61 |
+
if vote_data.liked:
|
| 62 |
+
pass
|
| 63 |
+
else:
|
| 64 |
+
vote_message = f'{datetime.now().strftime(" %I: %M %p on %B %d, %Y")}:\n{vote_data.value}\n'
|
| 65 |
+
with open(path_to_log, "a") as f:
|
| 66 |
+
f.write(vote_message)
|
| 67 |
|
| 68 |
|
| 69 |
myAdditionalInputs = [
|
|
|
|
| 118 |
show_copy_button=False,
|
| 119 |
likeable=True)
|
| 120 |
|
| 121 |
+
|
| 122 |
myTextInput = gr.Textbox(lines=2,
|
| 123 |
max_lines=2,
|
| 124 |
placeholder="Send a message",
|
|
|
|
| 143 |
size="sm")
|
| 144 |
|
| 145 |
|
| 146 |
+
with gr.ChatInterface(inference,
|
| 147 |
+
chatbot=myChatbot,
|
| 148 |
+
textbox=myTextInput,
|
| 149 |
+
title="FRANZI-Bot 2.0",
|
| 150 |
+
theme=myTheme,
|
| 151 |
+
# additional_inputs=myAdditionalInputs,
|
| 152 |
+
submit_btn=mySubmitButton,
|
| 153 |
+
stop_btn="STOP",
|
| 154 |
+
retry_btn=myRetryButton,
|
| 155 |
+
undo_btn=myUndoButton,
|
| 156 |
+
clear_btn=myClearButton) as chatApp:
|
| 157 |
+
myChatbot.like(event_voting, None, None)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
chatApp.queue().launch(show_api=False)
|