Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -21,39 +21,52 @@ terminators = [
|
|
21 |
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
22 |
]
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
system_message,
|
29 |
-
max_tokens,
|
30 |
-
temperature,
|
31 |
-
top_p,
|
32 |
-
):
|
33 |
-
messages = [{"role": "system", "content": system_message}]
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
messages.append({"role": "user", "content": message})
|
42 |
-
|
43 |
-
response = ""
|
44 |
-
|
45 |
-
for message in client.chat_completion(
|
46 |
-
messages,
|
47 |
-
max_tokens=max_tokens,
|
48 |
-
stream=True,
|
49 |
temperature=temperature,
|
50 |
-
|
51 |
-
)
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
"""
|
58 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
59 |
"""
|
|
|
21 |
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
22 |
]
|
23 |
|
24 |
+
@spaces.GPU(duration=120)
|
25 |
+
def chat_mistral7b_v0dot3(message: str,
|
26 |
+
history: list,
|
27 |
+
temperature: float,
|
28 |
+
max_new_tokens: int
|
29 |
+
) -> str:
|
30 |
+
"""
|
31 |
+
Generate a streaming response using the mistralai/Mistral-7B-Instruct-v0.3 model.
|
32 |
+
Args:
|
33 |
+
message (str): The input message.
|
34 |
+
history (list): The conversation history used by ChatInterface.
|
35 |
+
temperature (float): The temperature for generating the response.
|
36 |
+
max_new_tokens (int): The maximum number of new tokens to generate.
|
37 |
+
Returns:
|
38 |
+
str: The generated response.
|
39 |
+
"""
|
40 |
+
conversation = []
|
41 |
+
for user, assistant in history:
|
42 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
43 |
+
conversation.append({"role": "user", "content": message})
|
44 |
|
45 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
46 |
+
|
47 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
generate_kwargs = dict(
|
50 |
+
input_ids= input_ids,
|
51 |
+
streamer=streamer,
|
52 |
+
max_new_tokens=max_new_tokens,
|
53 |
+
do_sample=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
temperature=temperature,
|
55 |
+
eos_token_id=terminators,
|
56 |
+
)
|
57 |
+
# This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
58 |
+
if temperature == 0:
|
59 |
+
generate_kwargs['do_sample'] = False
|
60 |
+
|
61 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
62 |
+
t.start()
|
63 |
|
64 |
+
outputs = []
|
65 |
+
for text in streamer:
|
66 |
+
outputs.append(text)
|
67 |
+
#print(outputs)
|
68 |
+
yield "".join(outputs)
|
69 |
+
|
70 |
"""
|
71 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
72 |
"""
|