Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -15,22 +15,49 @@ model, tokenizer = load_model(
|
|
15 |
finetuned="chansung/alpaca-lora-13b"
|
16 |
)
|
17 |
|
18 |
-
|
19 |
|
20 |
def chat_stream(
|
21 |
context,
|
22 |
instruction,
|
23 |
state_chatbot,
|
24 |
):
|
25 |
-
if len(context) >
|
26 |
raise gr.Error("context or prompt is too long!")
|
27 |
-
|
|
|
28 |
# user input should be appropriately formatted (don't be confused by the function name)
|
29 |
instruction_display = common_post_process(instruction)
|
30 |
-
instruction_prompt = generate_prompt(instruction, state_chatbot, context)
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
instruction_prompt,
|
33 |
-
max_tokens=
|
34 |
temperature=1,
|
35 |
top_p=0.9
|
36 |
)
|
@@ -60,7 +87,7 @@ def chat_stream(
|
|
60 |
instruction_display,
|
61 |
processed_response
|
62 |
)
|
63 |
-
yield (state_chatbot, state_chatbot, context)
|
64 |
break
|
65 |
else:
|
66 |
agg_tokens = ""
|
@@ -69,7 +96,7 @@ def chat_stream(
|
|
69 |
if agg_tokens == "":
|
70 |
processed_response, to_exit = post_process_stream(tokens)
|
71 |
state_chatbot[-1] = (instruction_display, processed_response)
|
72 |
-
yield (state_chatbot, state_chatbot, context)
|
73 |
|
74 |
if to_exit:
|
75 |
break
|
@@ -79,7 +106,7 @@ def chat_stream(
|
|
79 |
yield (
|
80 |
state_chatbot,
|
81 |
state_chatbot,
|
82 |
-
|
83 |
)
|
84 |
|
85 |
def chat_batch(
|
|
|
15 |
finetuned="chansung/alpaca-lora-13b"
|
16 |
)
|
17 |
|
18 |
+
stream_model = StreamModel(model, tokenizer)
|
19 |
|
20 |
def chat_stream(
|
21 |
context,
|
22 |
instruction,
|
23 |
state_chatbot,
|
24 |
):
|
25 |
+
if len(context) > 500 or len(instruction) > 150:
|
26 |
raise gr.Error("context or prompt is too long!")
|
27 |
+
|
28 |
+
bot_summarized_response = ''
|
29 |
# user input should be appropriately formatted (don't be confused by the function name)
|
30 |
instruction_display = common_post_process(instruction)
|
31 |
+
instruction_prompt, conv_length = generate_prompt(instruction, state_chatbot, context)
|
32 |
+
|
33 |
+
if conv_length > num_of_characters_to_keep:
|
34 |
+
instruction_prompt = generate_prompt(SPECIAL_STRS["summarize"], state_chatbot, context)[0]
|
35 |
+
|
36 |
+
state_chatbot = state_chatbot + [
|
37 |
+
(
|
38 |
+
None,
|
39 |
+
" too long conversations, so let's summarize..."
|
40 |
+
)
|
41 |
+
]
|
42 |
+
yield (state_chatbot, state_chatbot, context)
|
43 |
+
|
44 |
+
bot_summarized_response = get_output_batch(
|
45 |
+
model, tokenizer, [instruction_prompt], generation_config
|
46 |
+
)[0]
|
47 |
+
bot_summarized_response = bot_summarized_response.split("### Response:")[-1].strip()
|
48 |
+
|
49 |
+
state_chatbot[-1] = (
|
50 |
+
None,
|
51 |
+
"β
summarization is done and set as context"
|
52 |
+
)
|
53 |
+
print(f"bot_summarized_response: {bot_summarized_response}")
|
54 |
+
yield (state_chatbot, state_chatbot, f"{context}. {bot_summarized_response}")
|
55 |
+
|
56 |
+
instruction_prompt = generate_prompt(instruction, state_chatbot, f"{context} {bot_summarized_response}")[0]
|
57 |
+
|
58 |
+
bot_response = stream_model(
|
59 |
instruction_prompt,
|
60 |
+
max_tokens=256,
|
61 |
temperature=1,
|
62 |
top_p=0.9
|
63 |
)
|
|
|
87 |
instruction_display,
|
88 |
processed_response
|
89 |
)
|
90 |
+
yield (state_chatbot, state_chatbot, f"{context} {bot_summarized_response}")
|
91 |
break
|
92 |
else:
|
93 |
agg_tokens = ""
|
|
|
96 |
if agg_tokens == "":
|
97 |
processed_response, to_exit = post_process_stream(tokens)
|
98 |
state_chatbot[-1] = (instruction_display, processed_response)
|
99 |
+
yield (state_chatbot, state_chatbot, f"{context} {bot_summarized_response}")
|
100 |
|
101 |
if to_exit:
|
102 |
break
|
|
|
106 |
yield (
|
107 |
state_chatbot,
|
108 |
state_chatbot,
|
109 |
+
f"{context} {bot_summarized_response}"
|
110 |
)
|
111 |
|
112 |
def chat_batch(
|