mirror of
https://github.com/QwenLM/Qwen.git
synced 2026-05-20 16:35:47 +08:00
fix no history problem
This commit is contained in:
56
web_demo.py
56
web_demo.py
@@ -9,12 +9,23 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|||||||
from transformers.generation import GenerationConfig
|
from transformers.generation import GenerationConfig
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)
|
tokenizer = AutoTokenizer.from_pretrained(
|
||||||
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat", device_map="auto", trust_remote_code=True).eval()
|
"Qwen/Qwen-7B-Chat", trust_remote_code=True, resume_download=True
|
||||||
model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)
|
)
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
"Qwen/Qwen-7B-Chat",
|
||||||
|
device_map="auto",
|
||||||
|
offload_folder="offload",
|
||||||
|
trust_remote_code=True,
|
||||||
|
resume_download=True,
|
||||||
|
).eval()
|
||||||
|
model.generation_config = GenerationConfig.from_pretrained(
|
||||||
|
"Qwen/Qwen-7B-Chat", trust_remote_code=True, resume_download=True
|
||||||
|
)
|
||||||
|
|
||||||
if len(sys.argv) > 1 and sys.argv[1] == "--exit":
|
if len(sys.argv) > 1 and sys.argv[1] == "--exit":
|
||||||
exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
def postprocess(self, y):
|
def postprocess(self, y):
|
||||||
if y is None:
|
if y is None:
|
||||||
@@ -58,21 +69,26 @@ def parse_text(text):
|
|||||||
line = line.replace("(", "(")
|
line = line.replace("(", "(")
|
||||||
line = line.replace(")", ")")
|
line = line.replace(")", ")")
|
||||||
line = line.replace("$", "$")
|
line = line.replace("$", "$")
|
||||||
lines[i] = "<br>"+line
|
lines[i] = "<br>" + line
|
||||||
text = "".join(lines)
|
text = "".join(lines)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def predict(input, chatbot, history, past_key_values):
|
|
||||||
|
task_history = []
|
||||||
|
|
||||||
|
|
||||||
|
def predict(input, chatbot):
|
||||||
print('Q: ' + parse_text(input))
|
print('Q: ' + parse_text(input))
|
||||||
chatbot.append((parse_text(input), ""))
|
chatbot.append((parse_text(input), ""))
|
||||||
fullResponse = "";
|
fullResponse = ""
|
||||||
|
|
||||||
for response in model.chat(tokenizer, input, history=history, stream=True):
|
for response in model.chat(tokenizer, input, history=task_history, stream=True):
|
||||||
chatbot[-1] = (parse_text(input), parse_text(response))
|
chatbot[-1] = (parse_text(input), parse_text(response))
|
||||||
|
|
||||||
yield chatbot, history, past_key_values
|
yield chatbot
|
||||||
fullResponse = parse_text(response);
|
fullResponse = parse_text(response)
|
||||||
|
|
||||||
|
task_history.append((input, fullResponse))
|
||||||
print("A: " + parse_text(fullResponse))
|
print("A: " + parse_text(fullResponse))
|
||||||
|
|
||||||
|
|
||||||
@@ -81,7 +97,8 @@ def reset_user_input():
|
|||||||
|
|
||||||
|
|
||||||
def reset_state():
|
def reset_state():
|
||||||
return [], [], None
|
task_history = []
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
with gr.Blocks() as demo:
|
with gr.Blocks() as demo:
|
||||||
@@ -91,19 +108,16 @@ with gr.Blocks() as demo:
|
|||||||
with gr.Row():
|
with gr.Row():
|
||||||
with gr.Column(scale=4):
|
with gr.Column(scale=4):
|
||||||
with gr.Column(scale=12):
|
with gr.Column(scale=12):
|
||||||
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
|
query = gr.Textbox(
|
||||||
container=False)
|
show_label=False, placeholder="Input...", lines=10
|
||||||
|
).style(container=False)
|
||||||
with gr.Column(min_width=32, scale=1):
|
with gr.Column(min_width=32, scale=1):
|
||||||
submitBtn = gr.Button("Submit", variant="primary")
|
submitBtn = gr.Button("Submit", variant="primary")
|
||||||
with gr.Column(scale=1):
|
with gr.Column(scale=1):
|
||||||
emptyBtn = gr.Button("Clear History")
|
emptyBtn = gr.Button("Clear History")
|
||||||
|
|
||||||
history = gr.State([])
|
submitBtn.click(predict, [query, chatbot], [chatbot], show_progress=True)
|
||||||
past_key_values = gr.State(None)
|
submitBtn.click(reset_user_input, [], [query])
|
||||||
|
emptyBtn.click(reset_state, outputs=[chatbot], show_progress=True)
|
||||||
submitBtn.click(predict, [user_input, chatbot, history, past_key_values],
|
|
||||||
[chatbot, history, past_key_values], show_progress=True)
|
|
||||||
submitBtn.click(reset_user_input, [], [user_input])
|
|
||||||
emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
|
|
||||||
|
|
||||||
demo.queue().launch(share=False, inbrowser=True, server_port=80, server_name="0.0.0.0")
|
demo.queue().launch(share=False, inbrowser=True, server_port=80, server_name="0.0.0.0")
|
||||||
|
|||||||
Reference in New Issue
Block a user