Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,70 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
|
|
|
|
|
|
|
|
|
|
4 |
|
|
|
5 |
"""
|
6 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
7 |
"""
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
if val[1]:
|
25 |
-
messages.append({"role": "assistant", "content": val[1]})
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
messages,
|
33 |
-
max_tokens=max_tokens,
|
34 |
-
stream=True,
|
35 |
temperature=temperature,
|
36 |
-
|
37 |
-
)
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
import os
|
5 |
+
import spaces
|
6 |
+
from transformers import GemmaTokenizer, AutoModelForCausalLM
|
7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
8 |
+
from threading import Thread
|
9 |
|
10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
11 |
"""
|
12 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
13 |
"""
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained("LLMproj1/mypersona-llama3-8b")
|
15 |
+
model = AutoModelForCausalLM.from_pretrained("LLMproj1/mypersona-llama3-8b", device_map="auto")
|
16 |
+
terminators = [
|
17 |
+
tokenizer.eos_token_id,
|
18 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
19 |
+
]
|
20 |
|
21 |
|
22 |
+
@spaces.GPU(duration=120)
|
23 |
+
def chat_mistral7b_v0dot3(message: str,
|
24 |
+
history: list,
|
25 |
+
temperature: float,
|
26 |
+
max_new_tokens: int
|
27 |
+
) -> str:
|
28 |
+
"""
|
29 |
+
Generate a streaming response using the mistralai/Mistral-7B-Instruct-v0.3 model.
|
30 |
+
Args:
|
31 |
+
message (str): The input message.
|
32 |
+
history (list): The conversation history used by ChatInterface.
|
33 |
+
temperature (float): The temperature for generating the response.
|
34 |
+
max_new_tokens (int): The maximum number of new tokens to generate.
|
35 |
+
Returns:
|
36 |
+
str: The generated response.
|
37 |
+
"""
|
38 |
+
conversation = []
|
39 |
+
for user, assistant in history:
|
40 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
41 |
+
conversation.append({"role": "user", "content": message})
|
42 |
|
43 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
44 |
+
|
45 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
|
|
|
|
46 |
|
47 |
+
generate_kwargs = dict(
|
48 |
+
input_ids= input_ids,
|
49 |
+
streamer=streamer,
|
50 |
+
max_new_tokens=max_new_tokens,
|
51 |
+
do_sample=True,
|
|
|
|
|
|
|
52 |
temperature=temperature,
|
53 |
+
eos_token_id=terminators,
|
54 |
+
)
|
55 |
+
# This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
56 |
+
if temperature == 0:
|
57 |
+
generate_kwargs['do_sample'] = False
|
58 |
+
|
59 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
60 |
+
t.start()
|
61 |
|
62 |
+
outputs = []
|
63 |
+
for text in streamer:
|
64 |
+
outputs.append(text)
|
65 |
+
#print(outputs)
|
66 |
+
yield "".join(outputs)
|
67 |
+
|
68 |
demo = gr.ChatInterface(
|
69 |
respond,
|
70 |
additional_inputs=[
|