Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -15,6 +15,46 @@ import warnings
|
|
15 |
import webbrowser
|
16 |
import spaces
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Constants
|
19 |
INPUT_DIRECTORY = 'input'
|
20 |
OUTPUT_DIRECTORY = 'output'
|
|
|
15 |
import webbrowser
|
16 |
import spaces
|
17 |
|
18 |
+
@spaces.GPU()
|
19 |
+
def stream_chat(
|
20 |
+
message: str,
|
21 |
+
history: list,
|
22 |
+
system_prompt: str,
|
23 |
+
temperature: float = 0.8,
|
24 |
+
max_new_tokens: int = 1024,
|
25 |
+
top_p: float = 1.0,
|
26 |
+
top_k: int = 20,
|
27 |
+
penalty: float = 1.2,
|
28 |
+
):
|
29 |
+
print(f'message: {message}')
|
30 |
+
print(f'history: {history}')
|
31 |
+
|
32 |
+
conversation = [
|
33 |
+
{"role": "system", "content": system_prompt}
|
34 |
+
]
|
35 |
+
for prompt, answer in history:
|
36 |
+
conversation.extend([
|
37 |
+
{"role": "user", "content": prompt},
|
38 |
+
{"role": "assistant", "content": answer},
|
39 |
+
])
|
40 |
+
|
41 |
+
conversation.append({"role": "user", "content": message})
|
42 |
+
|
43 |
+
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
44 |
+
|
45 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
46 |
+
|
47 |
+
generate_kwargs = dict(
|
48 |
+
input_ids=input_ids,
|
49 |
+
max_new_tokens = max_new_tokens,
|
50 |
+
do_sample = False if temperature == 0 else True,
|
51 |
+
top_p = top_p,
|
52 |
+
top_k = top_k,
|
53 |
+
temperature = temperature,
|
54 |
+
eos_token_id=[128001,128008,128009],
|
55 |
+
streamer=streamer,
|
56 |
+
)
|
57 |
+
|
58 |
# Constants
|
59 |
INPUT_DIRECTORY = 'input'
|
60 |
OUTPUT_DIRECTORY = 'output'
|