Hmjz100 commited on
Commit
51c8bbd
·
1 Parent(s): a653c90

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -2
app.py CHANGED
@@ -7,12 +7,44 @@ import datetime
7
  # 流式端点
8
  API_URL = "https://ai.fakeopen.com/v1/chat/completions" # 用户需要提供自己的 OPENAI_API_KEY
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # 推断函数
11
  def predict(openai_gptapi_key, model, system_msg, inputs, top_p, temperature, max_tokens, presence_penalty, frequency_penalty, chat_counter, chatbot=[], history=[]):
12
 
13
  print(f"——————————————————————————————")
14
  # 获取当前时间
15
- current_time = datetime.datetime.now().strftime("%Y年 - %m月 - %d日 %H时 : %M分 : %S秒")
16
  if inputs.strip() == '':
17
  inputs = "你好呀,使用英语与中文简单介绍下你自己吧!"
18
  if openai_gptapi_key.strip() == '':
@@ -101,13 +133,13 @@ def predict(openai_gptapi_key, model, system_msg, inputs, top_p, temperature, ma
101
  # 将每行解码为响应数据,因为响应数据是以字节形式返回的
102
  if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
103
  partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
104
- print(f"[{current_time}] 聊天:模型回复 - {partial_words}")
105
  if token_counter == 0:
106
  history.append(" " + partial_words)
107
  else:
108
  history[-1] = partial_words
109
  chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # 转换为列表的元组
110
  token_counter+=1
 
111
  yield chat, history, chat_counter, response # 类似于 {chatbot: chat, state: history}
112
 
113
  # 重置文本框
 
7
  # 流式端点
8
  API_URL = "https://ai.fakeopen.com/v1/chat/completions" # 用户需要提供自己的 OPENAI_API_KEY
9
 
10
+ # 时间转换
11
+ def timeswitch(t,sourcetz = "utc",totype="str",targettz = "cn",strformat = "%Y-%m-%d %H:%M:%S"):
12
+ '''
13
+ t: int/str 需要转换的时间表示,秒级时间戳或格式化的字符串
14
+ sourcetz: t所关联的时区别名,在timezone字典里自定义别名,如“cn”表示东八区,"utc"为世界标准时间
15
+ targettz: 输出时间的时区别名
16
+ totype: "str" or "timestamp" 输出时间表示类型
17
+ strformat:输入或输出的字符串格式,如果t为字符串,则strformat需要和它对应。
18
+ '''
19
+ # 常用的两个时区定义在字典里,可以添加其他时区
20
+ timezone = {
21
+ "cn":datetime.timezone(datetime.timedelta(hours=8)),
22
+ "utc":datetime.timezone.utc
23
+ }
24
+ sourcetz = timezone.get(sourcetz) if sourcetz else None
25
+ targettz = timezone.get(targettz) if targettz else sourcetz
26
+
27
+ # transfer to datetime
28
+ if isinstance(t,str):
29
+ ctime = datetime.datetime.strptime(t,strformat).replace(tzinfo = sourcetz)
30
+ elif isinstance(t,int) or isinstance(t,float):
31
+ if t > 1e10: t=t/1000
32
+ ctime = datetime.datetime.fromtimestamp(t).replace(tzinfo = sourcetz)
33
+
34
+ # from datetime to target type
35
+ if totype == "str":
36
+ newtime = ctime.astimezone(targettz).strftime(strformat)
37
+ elif totype == 'timestamp':
38
+ newtime = ctime.astimezone(targettz).timestamp()
39
+ newtime = int(newtime)
40
+ return newtime
41
+
42
  # 推断函数
43
  def predict(openai_gptapi_key, model, system_msg, inputs, top_p, temperature, max_tokens, presence_penalty, frequency_penalty, chat_counter, chatbot=[], history=[]):
44
 
45
  print(f"——————————————————————————————")
46
  # 获取当前时间
47
+ current_time = timeswitch(datetime.datetime.now().timestamp(),sourcetz="utc",targettz="cn",totype='str',strformat="%Y年-%m月-%d日 %H时:%M分:%S秒")
48
  if inputs.strip() == '':
49
  inputs = "你好呀,使用英语与中文简单介绍下你自己吧!"
50
  if openai_gptapi_key.strip() == '':
 
133
  # 将每行解码为响应数据,因为响应数据是以字节形式返回的
134
  if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
135
  partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
 
136
  if token_counter == 0:
137
  history.append(" " + partial_words)
138
  else:
139
  history[-1] = partial_words
140
  chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # 转换为列表的元组
141
  token_counter+=1
142
+ print(f"[{current_time}] 聊天:模型回复 - {partial_words}")
143
  yield chat, history, chat_counter, response # 类似于 {chatbot: chat, state: history}
144
 
145
  # 重置文本框