skzz commited on
Commit
769970e
1 Parent(s): a638ee1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -28,7 +28,7 @@ class ChatClient:
28
  self.model = AutoModelForCausalLM.from_pretrained(model_path).to(self.device)
29
  self.model.eval() # 设置为评估模式
30
 
31
- async def chat_completion(self, messages, max_tokens, stream=False, temperature=1.0, top_p=1.0):
32
  """
33
  生成对话回复。
34
  """
@@ -47,17 +47,15 @@ class ChatClient:
47
  }
48
 
49
  # 使用生成器生成文本
50
- output_sequences = self.model.generate(**inputs, **gen_kwargs)
51
 
52
  # 解码生成的文本
53
  # result_text = self.tokenizer.decode(output_sequences[0], skip_special_tokens=True)
54
  # yield result_text
55
 
56
- # 解码生成的文本
57
- for sequence in output_sequences:
58
- result_text = self.tokenizer.decode(sequence, skip_special_tokens=True)
59
- await anyio.sleep(0) # Yield control, simulating asynchronous operation
60
- yield result_text
61
 
62
  # 创建客户端实例,指定模型路径
63
  model_path = 'model/v3/'
@@ -68,7 +66,7 @@ client = ChatClient(model_path)
68
 
69
 
70
 
71
- async def respond(
72
  message,
73
  history: list[tuple[str, str]],
74
  system_message,
@@ -91,21 +89,19 @@ async def respond(
91
 
92
  response = ""
93
 
94
- async for message in client.chat_completion(
95
  messages,
96
  max_tokens=max_tokens,
97
  stream=True,
98
  temperature=temperature,
99
  top_p=top_p,
100
  ):
101
- # print(message)
102
- # token = message
103
- # #token = message.choices[0].delta.content
104
-
105
- # response += token
106
- # yield response
107
 
108
- yield message
 
109
 
110
  """
111
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
28
  self.model = AutoModelForCausalLM.from_pretrained(model_path).to(self.device)
29
  self.model.eval() # 设置为评估模式
30
 
31
+ def chat_completion(self, messages, max_tokens, stream=False, temperature=1.0, top_p=1.0):
32
  """
33
  生成对话回复。
34
  """
 
47
  }
48
 
49
  # 使用生成器生成文本
50
+ # output_sequences = self.model.generate(**inputs, **gen_kwargs)
51
 
52
  # 解码生成的文本
53
  # result_text = self.tokenizer.decode(output_sequences[0], skip_special_tokens=True)
54
  # yield result_text
55
 
56
+ # claude 3.5
57
+ for token in self.model.generate(**inputs, **gen_kwargs, streamer=None):
58
+ yield self.tokenizer.decode(token, skip_special_tokens=True)
 
 
59
 
60
  # 创建客户端实例,指定模型路径
61
  model_path = 'model/v3/'
 
66
 
67
 
68
 
69
+ def respond(
70
  message,
71
  history: list[tuple[str, str]],
72
  system_message,
 
89
 
90
  response = ""
91
 
92
+ for message in client.chat_completion(
93
  messages,
94
  max_tokens=max_tokens,
95
  stream=True,
96
  temperature=temperature,
97
  top_p=top_p,
98
  ):
99
+ print(message)
100
+ token = message
101
+ #token = message.choices[0].delta.content
 
 
 
102
 
103
+ response += token
104
+ yield response
105
 
106
  """
107
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface