Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,48 +2,46 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
|
5 |
-
|
6 |
title = "EZChat"
|
7 |
description = "A State-of-the-Art Large-scale Pretrained Response generation model Qwen's 7B-Chat"
|
8 |
examples = [["How are you?"]]
|
9 |
|
10 |
-
|
11 |
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)
|
12 |
-
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat",
|
|
|
|
|
13 |
|
14 |
-
def predict(input, history=
|
15 |
-
# Check if input is not None and eos_token is not None
|
16 |
if input is not None and tokenizer.eos_token is not None:
|
17 |
combined_input = input + tokenizer.eos_token
|
18 |
-
|
19 |
-
else:
|
20 |
-
# Handle the case where input or tokenizer.eos_token is None
|
21 |
-
print("Input or eos_token is None. Cannot concatenate.")
|
22 |
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
# print('decoded_response-->>'+str(response))
|
34 |
-
response = [
|
35 |
-
(response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
|
36 |
-
] # convert to tuples of list
|
37 |
-
# print('response-->>'+str(response))
|
38 |
-
return response, history
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
gr.Interface(
|
42 |
fn=predict,
|
43 |
title=title,
|
44 |
description=description,
|
45 |
examples=examples,
|
46 |
-
inputs=["text", "
|
47 |
-
outputs=["
|
48 |
theme="ParityError/Anime",
|
49 |
-
).launch()
|
|
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
|
|
|
5 |
title = "EZChat"
|
6 |
description = "A State-of-the-Art Large-scale Pretrained Response generation model Qwen's 7B-Chat"
|
7 |
examples = [["How are you?"]]
|
8 |
|
|
|
9 |
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True).eval()
|
11 |
+
|
12 |
+
history = [] # Initialize chat history
|
13 |
|
14 |
+
def predict(input, history=history):
|
|
|
15 |
if input is not None and tokenizer.eos_token is not None:
|
16 |
combined_input = input + tokenizer.eos_token
|
17 |
+
new_user_input_ids = tokenizer.encode(combined_input, return_tensors="pt")
|
|
|
|
|
|
|
18 |
|
19 |
+
# Append the new user input tokens to the chat history
|
20 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
21 |
|
22 |
+
# Generate a response
|
23 |
+
generated_response_ids = model.generate(
|
24 |
+
bot_input_ids, max_length=20, pad_token_id=tokenizer.eos_token_id
|
25 |
+
)
|
26 |
|
27 |
+
# Convert the generated response tokens to text
|
28 |
+
response = tokenizer.decode(generated_response_ids[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# Append the user input and generated response to the chat history
|
31 |
+
history.extend(new_user_input_ids[0].tolist())
|
32 |
+
history.extend(generated_response_ids[0].tolist())
|
33 |
+
|
34 |
+
return response, history
|
35 |
+
|
36 |
+
else:
|
37 |
+
print("Input or eos_token is None. Cannot concatenate.")
|
38 |
|
39 |
gr.Interface(
|
40 |
fn=predict,
|
41 |
title=title,
|
42 |
description=description,
|
43 |
examples=examples,
|
44 |
+
inputs=["text", "text"],
|
45 |
+
outputs=["text", "text"],
|
46 |
theme="ParityError/Anime",
|
47 |
+
).launch()
|