ffreemt commited on
Commit
634ed9b
1 Parent(s): d82d66e

Update layout

Browse files
Files changed (1) hide show
  1. app.py +58 -10
app.py CHANGED
@@ -15,7 +15,6 @@ from transformers import AutoModel, AutoTokenizer
15
  # os.system("pip install torch transformers sentencepiece loguru")
16
 
17
 
18
-
19
  # fix timezone in Linux
20
  os.environ["TZ"] = "Asia/Shanghai"
21
  try:
@@ -42,9 +41,9 @@ if has_cuda:
42
  AutoModel.from_pretrained(model_name, trust_remote_code=True).cuda().half()
43
  )
44
  else:
45
- model = AutoModel.from_pretrained(
46
- model_name, trust_remote_code=True
47
- ).half().float() # .float() .half().float(): must use float for cpu
48
 
49
  model = model.eval()
50
  logger.debug("done load")
@@ -54,7 +53,11 @@ logger.debug("done load")
54
 
55
  # locate model file cache
56
  cache_loc = Path("~/.cache/huggingface/hub").expanduser()
57
- model_cache_path = [elm for elm in Path(cache_loc).rglob("*") if Path(model_name).name in elm.as_posix() and "pytorch_model.bin" in elm.as_posix()]
 
 
 
 
58
 
59
  logger.debug(f"{model_cache_path=}")
60
 
@@ -64,25 +67,70 @@ if model_cache_path:
64
 
65
 
66
  def respond(message, chat_history):
67
- response, chat_history = model.chat(tokenizer, message, history=chat_history, temperature=0.7, repetition_penalty=1.2, max_length=128)
 
 
 
 
 
 
 
 
 
68
  chat_history.append((message, response))
69
- return "", chat_history
 
70
 
71
  theme = gr.themes.Soft(text_size="sm")
72
  with gr.Blocks(theme=theme) as block:
73
  chatbot = gr.Chatbot()
74
 
75
- with gr.Column():
76
  with gr.Column(scale=12):
77
  msg = gr.Textbox()
78
  with gr.Column(scale=1, min_width=16):
79
- with gr.Row():
80
  btn = gr.Button("Send")
 
81
  clear = gr.ClearButton([msg, chatbot])
82
 
83
  # do not clear prompt
84
- msg.submit(lambda x, y: [x] + respond(x, y)[1:], [msg, chatbot], [msg, chatbot])
85
 
86
  btn.click(respond, [msg, chatbot], [msg, chatbot])
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  block.queue().launch()
 
15
  # os.system("pip install torch transformers sentencepiece loguru")
16
 
17
 
 
18
  # fix timezone in Linux
19
  os.environ["TZ"] = "Asia/Shanghai"
20
  try:
 
41
  AutoModel.from_pretrained(model_name, trust_remote_code=True).cuda().half()
42
  )
43
  else:
44
+ model = (
45
+ AutoModel.from_pretrained(model_name, trust_remote_code=True).half().float()
46
+ ) # .float() .half().float(): must use float for cpu
47
 
48
  model = model.eval()
49
  logger.debug("done load")
 
53
 
54
  # locate model file cache
55
  cache_loc = Path("~/.cache/huggingface/hub").expanduser()
56
+ model_cache_path = [
57
+ elm
58
+ for elm in Path(cache_loc).rglob("*")
59
+ if Path(model_name).name in elm.as_posix() and "pytorch_model.bin" in elm.as_posix()
60
+ ]
61
 
62
  logger.debug(f"{model_cache_path=}")
63
 
 
67
 
68
 
69
  def respond(message, chat_history):
70
+ """Gen a response."""
71
+ message = message.strip()
72
+ response, chat_history = model.chat(
73
+ tokenizer,
74
+ message,
75
+ history=chat_history,
76
+ temperature=0.7,
77
+ repetition_penalty=1.2,
78
+ max_length=128,
79
+ )
80
  chat_history.append((message, response))
81
+ return message, chat_history
82
+
83
 
84
  theme = gr.themes.Soft(text_size="sm")
85
  with gr.Blocks(theme=theme) as block:
86
  chatbot = gr.Chatbot()
87
 
88
+ with gr.Row():
89
  with gr.Column(scale=12):
90
  msg = gr.Textbox()
91
  with gr.Column(scale=1, min_width=16):
 
92
  btn = gr.Button("Send")
93
+ with gr.Column(scale=1, min_width=8):
94
  clear = gr.ClearButton([msg, chatbot])
95
 
96
  # do not clear prompt
97
+ msg.submit(lambda x, y: (x,) + respond(x, y)[1:], [msg, chatbot], [msg, chatbot])
98
 
99
  btn.click(respond, [msg, chatbot], [msg, chatbot])
100
 
101
+ with gr.Accordion("Example inputs", open=True):
102
+ etext = """In America, where cars are an important part of the national psyche, a decade ago people had suddenly started to drive less, which had not happened since the oil shocks of the 1970s. """
103
+ examples = gr.Examples(
104
+ examples=[
105
+ ["Explain the plot of Cinderella in a sentence."],
106
+ [
107
+ "How long does it take to become proficient in French, and what are the best methods for retaining information?"
108
+ ],
109
+ ["What are some common mistakes to avoid when writing code?"],
110
+ ["Build a prompt to generate a beautiful portrait of a horse"],
111
+ ["Suggest four metaphors to describe the benefits of AI"],
112
+ ["Write a pop song about leaving home for the sandy beaches."],
113
+ ["Write a summary demonstrating my ability to tame lions"],
114
+ ["鲁迅和周树人什么关系"],
115
+ ["从前有一头牛,这头牛后面有什么?"],
116
+ ["正无穷大加一大于正无穷大吗?"],
117
+ ["正无穷大加正无穷大大于正无穷大吗?"],
118
+ ["-2的平方根等于什么"],
119
+ ["树上有5只鸟,猎人开枪打死了一只。树上还有几只鸟?"],
120
+ ["树上有11只鸟,猎人开枪打死了一只。树上还有几只鸟?提示:需考虑鸟可能受惊吓飞走。"],
121
+ ["鲁迅和周树人什么关系 用英文回答"],
122
+ ["以红楼梦的行文风格写一张委婉的请假条。不少于320字。"],
123
+ [f"{etext} 翻成中文,列出3个版本"],
124
+ [f"{etext} \n 翻成中文,保留原意,但使用文学性的语言。不要写解释。列出3个版本"],
125
+ ["js 判断一个数是不是质数"],
126
+ ["js 实现python 的 range(10)"],
127
+ ["js 实现python 的 [*(range(10)]"],
128
+ ["假定 1 + 2 = 4, 试求 7 + 8"],
129
+ ["Erkläre die Handlung von Cinderella in einem Satz."],
130
+ ["Erkläre die Handlung von Cinderella in einem Satz. Auf Deutsch"],
131
+ ],
132
+ inputs=[msg],
133
+ examples_per_page=60,
134
+ )
135
+
136
  block.queue().launch()