Spaces:
Runtime error
Runtime error
update steram
Browse files
app.py
CHANGED
@@ -1,74 +1,100 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
import spaces
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
token = os.getenv("HUGGINGFACE_TOKEN")
|
10 |
-
if token =="":
|
11 |
-
raise ValueError("HUGGINGFACE_TOKEN environment variable is not set")
|
12 |
-
print(token)
|
13 |
-
|
14 |
-
client = InferenceClient("google/gemma-2-2b-it",token = token)
|
15 |
-
|
16 |
-
@spaces.GPU(duration=30)
|
17 |
-
def respond(
|
18 |
-
message,
|
19 |
-
history: list[tuple[str, str]],
|
20 |
-
system_message,
|
21 |
-
max_tokens,
|
22 |
-
temperature,
|
23 |
-
top_p,
|
24 |
-
):
|
25 |
-
#messages = [{"role": "system", "content": system_message}] #system not supported
|
26 |
-
messages = []
|
27 |
-
|
28 |
-
for val in history:
|
29 |
-
if val[0]:
|
30 |
-
messages.append({"role": "user", "content": val[0]})
|
31 |
-
if val[1]:
|
32 |
-
messages.append({"role": "assistant", "content": val[1]})
|
33 |
-
|
34 |
-
messages.append({"role": "user", "content": message})
|
35 |
-
response = ""
|
36 |
-
|
37 |
-
# Load model directly
|
38 |
-
|
39 |
-
for message in client.chat_completion(
|
40 |
-
messages,
|
41 |
-
max_tokens=max_tokens,
|
42 |
-
stream=True,
|
43 |
-
temperature=temperature,
|
44 |
-
top_p=top_p,
|
45 |
-
):
|
46 |
-
token = message.choices[0].delta.content
|
47 |
-
|
48 |
-
response += token
|
49 |
-
yield response
|
50 |
-
|
51 |
-
"""
|
52 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
53 |
-
"""
|
54 |
-
demo = gr.ChatInterface(
|
55 |
-
respond,
|
56 |
-
additional_inputs=[
|
57 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
58 |
-
gr.Slider(minimum=1, maximum=512, value=32, step=1, label="Max new tokens"),
|
59 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
60 |
-
gr.Slider(
|
61 |
-
minimum=0.1,
|
62 |
-
maximum=1.0,
|
63 |
-
value=0.95,
|
64 |
-
step=0.05,
|
65 |
-
label="Top-p (nucleus sampling)",
|
66 |
-
),
|
67 |
-
],
|
68 |
-
)
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
if __name__ == "__main__":
|
72 |
-
demo.launch()
|
73 |
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import spaces
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
5 |
+
from transformers import TextIteratorStreamer
|
6 |
+
from threading import Thread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
text_generator = None
|
11 |
+
is_hugging_face = True
|
12 |
+
model_id = "google/gemma-2-9b-it"# too big
|
13 |
+
model_id = "google/gemma-2-2b-it"
|
14 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
15 |
+
device = "auto" # torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
+
device = "cuda"
|
17 |
+
dtype = torch.bfloat16
|
18 |
+
dtype = torch.float16
|
19 |
+
|
20 |
+
if not huggingface_token:
|
21 |
+
pass
|
22 |
+
print("no HUGGINGFACE_TOKEN if you need set secret ")
|
23 |
+
#raise ValueError("HUGGINGFACE_TOKEN environment variable is not set")
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=huggingface_token)
|
33 |
+
|
34 |
+
print(model_id,device,dtype)
|
35 |
+
histories = []
|
36 |
+
#model = None
|
37 |
|
|
|
|
|
38 |
|
39 |
+
|
40 |
+
if not is_hugging_face:
|
41 |
+
model = AutoModelForCausalLM.from_pretrained(
|
42 |
+
model_id, token=huggingface_token ,torch_dtype=dtype,device_map=device
|
43 |
+
)
|
44 |
+
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer,torch_dtype=dtype,device_map=device,stream=True ) #pipeline has not to(device)
|
45 |
+
|
46 |
+
if next(model.parameters()).is_cuda:
|
47 |
+
print("The model is on a GPU")
|
48 |
+
else:
|
49 |
+
print("The model is on a CPU")
|
50 |
+
|
51 |
+
#print(f"text_generator.device='{text_generator.device}")
|
52 |
+
if str(text_generator.device).strip() == 'cuda':
|
53 |
+
print("The pipeline is using a GPU")
|
54 |
+
else:
|
55 |
+
print("The pipeline is using a CPU")
|
56 |
+
|
57 |
+
print("initialized")
|
58 |
+
|
59 |
+
@spaces.GPU(duration=60)
|
60 |
+
def generate_text(messages):
|
61 |
+
if is_hugging_face:#need everytime initialize for ZeroGPU
|
62 |
+
model = AutoModelForCausalLM.from_pretrained(
|
63 |
+
model_id, token=huggingface_token ,torch_dtype=dtype,device_map=device
|
64 |
+
)
|
65 |
+
model.to(device)
|
66 |
+
question = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
67 |
+
question = tokenizer(question, return_tensors="pt").to(device)
|
68 |
+
|
69 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
|
70 |
+
generation_kwargs = dict(question, streamer=streamer, max_new_tokens=200)
|
71 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
72 |
+
|
73 |
+
generated_output = ""
|
74 |
+
thread.start()
|
75 |
+
for new_text in streamer:
|
76 |
+
generated_output += new_text
|
77 |
+
yield generated_output
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
def call_generate_text(message, history):
|
84 |
+
# history.append({"role": "user", "content": message})
|
85 |
+
#print(message)
|
86 |
+
#print(history)
|
87 |
+
|
88 |
+
messages = history+[{"role":"user","content":message}]
|
89 |
+
try:
|
90 |
+
|
91 |
+
for text in generate_text(messages):
|
92 |
+
yield text
|
93 |
+
except RuntimeError as e:
|
94 |
+
print(f"An unexpected error occurred: {e}")
|
95 |
+
yield ""
|
96 |
+
|
97 |
+
demo = gr.ChatInterface(call_generate_text,type="messages")
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
demo.launch(share=True)
|