aauu1234 commited on
Commit
8d7f304
·
1 Parent(s): 8d2d7f7
Files changed (2) hide show
  1. app.py +12 -24
  2. bak2.txt +71 -0
app.py CHANGED
@@ -7,30 +7,18 @@ import traceback
7
 
8
  model_name_or_path = "ClosedCharacter/Peach-9B-8k-Roleplay"
9
  tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=True)
10
- model = AutoModelForCausalLM.from_pretrained(
11
- model_name_or_path, torch_dtype=torch.bfloat16,
12
- trust_remote_code=True)
13
- """
14
- messages = [
15
- {"role": "system", "content": "你是黑丝御姐"},
16
- {"role": "user", "content": "你好,你是谁"},
17
- ]
18
 
19
- input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, return_tensors="pt")
20
- output = model.generate(
21
- inputs=input_ids.to("cpu"),
22
- do_sample=True,
23
- temperature=0.3,
24
- top_p=0.5,
25
- no_repeat_ngram_size=6,
26
- repetition_penalty=1.1,
27
- max_new_tokens=512)
28
 
29
- generated_response = tokenizer.decode(output[0])
30
- print("Generated response:", generated_response)
 
31
 
32
- print("First response to 'hi user first':", "你好,我是你的黑丝御姐?")
33
- """
34
  def slow_echo(system_message, user_message):
35
  try:
36
  messages = [
@@ -38,9 +26,9 @@ def slow_echo(system_message, user_message):
38
  {"role": "user", "content": user_message},
39
  ]
40
 
41
- input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, return_tensors="pt")
42
  output = model.generate(
43
- inputs=input_ids.to("cpu"),
44
  do_sample=True,
45
  temperature=0.3,
46
  top_p=0.5,
@@ -64,7 +52,7 @@ iface = gr.Interface(
64
  gr.Textbox(label="User Message")
65
  ],
66
  outputs=gr.Textbox(label="Generated Response"),
67
- title="roleplay Chatbot"
68
  )
69
 
70
  if __name__ == "__main__":
 
7
 
8
  model_name_or_path = "ClosedCharacter/Peach-9B-8k-Roleplay"
9
  tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=True)
 
 
 
 
 
 
 
 
10
 
11
+ # Check if GPU is available
12
+ if torch.cuda.is_available():
13
+ device = torch.device("cuda")
14
+ else:
15
+ device = torch.device("cpu")
16
+ print("GPU not available, using CPU.")
 
 
 
17
 
18
+ model = AutoModelForCausalLM.from_pretrained(
19
+ model_name_or_path, torch_dtype=torch.bfloat16,
20
+ trust_remote_code=True).to(device)
21
 
 
 
22
  def slow_echo(system_message, user_message):
23
  try:
24
  messages = [
 
26
  {"role": "user", "content": user_message},
27
  ]
28
 
29
+ input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, return_tensors="pt").to(device)
30
  output = model.generate(
31
+ inputs=input_ids,
32
  do_sample=True,
33
  temperature=0.3,
34
  top_p=0.5,
 
52
  gr.Textbox(label="User Message")
53
  ],
54
  outputs=gr.Textbox(label="Generated Response"),
55
+ title="Roleplay Chatbot"
56
  )
57
 
58
  if __name__ == "__main__":
bak2.txt ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ import time
6
+ import traceback
7
+
8
+ model_name_or_path = "ClosedCharacter/Peach-9B-8k-Roleplay"
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=True)
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ model_name_or_path, torch_dtype=torch.bfloat16,
12
+ trust_remote_code=True)
13
+ """
14
+ messages = [
15
+ {"role": "system", "content": "你是黑丝御姐"},
16
+ {"role": "user", "content": "你好,你是谁"},
17
+ ]
18
+
19
+ input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, return_tensors="pt")
20
+ output = model.generate(
21
+ inputs=input_ids.to("cpu"),
22
+ do_sample=True,
23
+ temperature=0.3,
24
+ top_p=0.5,
25
+ no_repeat_ngram_size=6,
26
+ repetition_penalty=1.1,
27
+ max_new_tokens=512)
28
+
29
+ generated_response = tokenizer.decode(output[0])
30
+ print("Generated response:", generated_response)
31
+
32
+ print("First response to 'hi user first':", "你好,我是你的黑丝御姐?")
33
+ """
34
+ def slow_echo(system_message, user_message):
35
+ try:
36
+ messages = [
37
+ {"role": "system", "content": system_message},
38
+ {"role": "user", "content": user_message},
39
+ ]
40
+
41
+ input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, return_tensors="pt")
42
+ output = model.generate(
43
+ inputs=input_ids.to("cpu"),
44
+ do_sample=True,
45
+ temperature=0.3,
46
+ top_p=0.5,
47
+ no_repeat_ngram_size=6,
48
+ repetition_penalty=1.1,
49
+ max_new_tokens=512)
50
+
51
+ generated_response = tokenizer.decode(output[0])
52
+
53
+ for i in range(len(generated_response)):
54
+ time.sleep(0.05)
55
+ yield generated_response[: i + 1]
56
+ except Exception as e:
57
+ error_message = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
58
+ yield error_message
59
+
60
+ iface = gr.Interface(
61
+ fn=slow_echo,
62
+ inputs=[
63
+ gr.Textbox(label="System Message"),
64
+ gr.Textbox(label="User Message")
65
+ ],
66
+ outputs=gr.Textbox(label="Generated Response"),
67
+ title="roleplay Chatbot"
68
+ )
69
+
70
+ if __name__ == "__main__":
71
+ iface.launch()