mikeee commited on
Commit
8d072c1
1 Parent(s): d9a1b97
Files changed (1) hide show
  1. app.py +23 -1
app.py CHANGED
@@ -12,7 +12,7 @@ from transformers import AutoModel, AutoTokenizer
12
  import gradio as gr
13
  import mdtex2html
14
 
15
- # from loguru import logger
16
 
17
  model_name = "THUDM/chatglm2-6b"
18
  model_name = "THUDM/chatglm2-6b-int4"
@@ -96,6 +96,23 @@ def predict(input, chatbot, max_length, top_p, temperature, history, past_key_va
96
  yield chatbot, history, past_key_values
97
 
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  def reset_user_input():
100
  return gr.update(value='')
101
 
@@ -147,6 +164,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
147
 
148
  emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
149
 
 
 
 
 
 
150
  # demo.queue().launch(share=False, inbrowser=True)
151
  # demo.queue().launch(share=True, inbrowser=True, debug=True)
152
 
 
12
  import gradio as gr
13
  import mdtex2html
14
 
15
+ from loguru import logger
16
 
17
  model_name = "THUDM/chatglm2-6b"
18
  model_name = "THUDM/chatglm2-6b-int4"
 
96
  yield chatbot, history, past_key_values
97
 
98
 
99
+ def trans_api(input, max_length=4096, top_p=0.8, temperature=0.2):
100
+ try:
101
+ res = model.stream_chat(
102
+ tokenizer,
103
+ input,
104
+ history=[],
105
+ past_key_values=None,
106
+ return_past_key_values=False,
107
+ max_length=max_length,
108
+ top_p=top_p,
109
+ temperature=temperature,
110
+ )
111
+ logger.debug(f"{res=}")
112
+ except Exception as exc:
113
+ logger.error(exc)
114
+
115
+
116
  def reset_user_input():
117
  return gr.update(value='')
118
 
 
164
 
165
  emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
166
 
167
+ with gr.Accordion("For Translation API", open=False):
168
+ input_text = gr.Text()
169
+ tr_btn = gr.Button("Go", variant="primary")
170
+ tr_btn.click(trans_api, [input_text, max_length, top_p, temperature], [], show_progress=True, api_name="tr")
171
+
172
  # demo.queue().launch(share=False, inbrowser=True)
173
  # demo.queue().launch(share=True, inbrowser=True, debug=True)
174