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