Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,61 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
import torch
|
4 |
|
5 |
# Load the model
|
6 |
model_name = "wop/kosmox-gguf"
|
7 |
-
model =
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
formatted = "<BOS>"
|
12 |
-
for message in messages:
|
13 |
-
if message['from'] == 'human':
|
14 |
-
formatted += ' ' + message['value'] + ' '
|
15 |
-
elif message['from'] == 'gpt':
|
16 |
-
formatted += ' ' + message['value'] + ' '
|
17 |
-
else:
|
18 |
-
formatted += '<|' + message['from'] + '|> ' + message['value'] + ' '
|
19 |
-
if add_generation_prompt:
|
20 |
-
formatted += ' '
|
21 |
-
return formatted
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
# Prepare the chat history
|
26 |
-
messages = [{"from": "system", "value": system_message}]
|
27 |
-
for user_msg, bot_msg in history:
|
28 |
-
if user_msg:
|
29 |
-
messages.append({"from": "human", "value": user_msg})
|
30 |
-
if bot_msg:
|
31 |
-
messages.append({"from": "gpt", "value": bot_msg})
|
32 |
-
messages.append({"from": "human", "value": message})
|
33 |
-
|
34 |
-
# Format the chat input for the model
|
35 |
-
chat_input = format_chat(messages, add_generation_prompt=False)
|
36 |
-
|
37 |
-
# Tokenize input (assuming model can handle raw text inputs internally)
|
38 |
-
inputs = torch.tensor([ord(c) for c in chat_input]).unsqueeze(0) # Dummy tokenization
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Generate response
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
temperature=temperature,
|
46 |
-
top_p=top_p,
|
47 |
-
do_sample=True
|
48 |
-
)
|
49 |
|
50 |
-
response
|
51 |
-
yield response.strip()
|
52 |
|
53 |
# Define the Gradio interface
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
gr.
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
)
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModel, AutoTokenizer
|
|
|
3 |
|
4 |
# Load the model
|
5 |
model_name = "wop/kosmox-gguf"
|
6 |
+
model = AutoModel.from_pretrained(model_name)
|
7 |
|
8 |
+
# Assuming we need to load a corresponding tokenizer
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Define the chat template
|
12 |
+
chat_template = "{{ bos_token }}{% for message in messages %}{% if message['from'] == 'human' %}{{' ' + message['value'] + ' '}}{% elif message['from'] == 'gpt' %}{{' ' + message['value'] + ' '}}{% else %}{{'<|' + message['from'] + '|> ' + message['value'] + ' '}}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ ' ' }}{% endif %}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
def chat(messages):
|
15 |
+
# Prepare the input
|
16 |
+
context = chat_template.format(
|
17 |
+
bos_token=tokenizer.bos_token,
|
18 |
+
messages=messages,
|
19 |
+
add_generation_prompt=True
|
20 |
+
)
|
21 |
+
|
22 |
+
# Tokenize the input
|
23 |
+
inputs = tokenizer(context, return_tensors='pt')
|
24 |
+
|
25 |
# Generate response
|
26 |
+
outputs = model.generate(**inputs)
|
27 |
+
|
28 |
+
# Decode the response
|
29 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
return response
|
|
|
32 |
|
33 |
# Define the Gradio interface
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
chatbot = gr.Chatbot()
|
36 |
+
with gr.Row():
|
37 |
+
with gr.Column():
|
38 |
+
user_input = gr.Textbox(
|
39 |
+
placeholder="Type your message here...",
|
40 |
+
label="Your message"
|
41 |
+
)
|
42 |
+
send_button = gr.Button("Send")
|
43 |
+
with gr.Column():
|
44 |
+
chat_output = gr.Textbox(
|
45 |
+
label="Chatbot response",
|
46 |
+
interactive=False
|
47 |
+
)
|
48 |
+
|
49 |
+
def respond(message, history):
|
50 |
+
history = history or []
|
51 |
+
history.append({"from": "human", "value": message})
|
52 |
+
|
53 |
+
response = chat(history)
|
54 |
+
history.append({"from": "gpt", "value": response})
|
55 |
+
|
56 |
+
return history, history[-1]['value']
|
57 |
+
|
58 |
+
send_button.click(respond, [user_input, chatbot], [chatbot, chat_output])
|
59 |
+
|
60 |
+
# Launch the Gradio interface
|
61 |
+
demo.launch()
|