wop commited on
Commit
0348877
1 Parent(s): c18814e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -55
app.py CHANGED
@@ -1,66 +1,61 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM
3
- import torch
4
 
5
  # Load the model
6
  model_name = "wop/kosmox-gguf"
7
- model = AutoModelForCausalLM.from_pretrained(model_name)
8
 
9
- # Define the chat template function
10
- def format_chat(messages, add_generation_prompt):
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
- # Function to generate responses
24
- def respond(message, history, system_message, max_tokens, temperature, top_p):
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
- with torch.no_grad():
42
- outputs = model.generate(
43
- input_ids=inputs,
44
- max_length=max_tokens,
45
- temperature=temperature,
46
- top_p=top_p,
47
- do_sample=True
48
- )
49
 
50
- response = ''.join([chr(t) for t in outputs[0].tolist() if t < 256]) # Dummy decoding
51
- yield response.strip()
52
 
53
  # Define the Gradio interface
54
- demo = gr.ChatInterface(
55
- respond,
56
- additional_inputs=[
57
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
58
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
59
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
60
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
61
- ],
62
- )
63
-
64
- # Launch the demo
65
- if __name__ == "__main__":
66
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()