Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,84 +4,58 @@ import torch
|
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
5 |
import gradio as gr
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
model_id = "Kendamarron/Tokara-0.5B-Chat-v0.1"
|
16 |
-
model_id = "Qwen/Qwen2-0.5B-Instruct"
|
17 |
-
|
18 |
-
device = "auto" # torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
19 |
-
dtype = torch.bfloat16
|
20 |
-
|
21 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, token=huggingface_token)
|
22 |
-
|
23 |
-
print(model_id,device,dtype)
|
24 |
-
histories = []
|
25 |
-
#model = None
|
26 |
-
|
27 |
-
|
28 |
-
def call_generate_text(prompt, system_message="You are a helpful assistant."):
|
29 |
-
if prompt =="":
|
30 |
-
print("empty prompt return")
|
31 |
-
return ""
|
32 |
-
|
33 |
-
global histories
|
34 |
-
#global model
|
35 |
-
#if model != None:# and model.is_cuda:
|
36 |
-
# print("Model is alive")
|
37 |
-
#else:
|
38 |
-
# model = AutoModelForCausalLM.from_pretrained(
|
39 |
-
# model_id, token=huggingface_token ,torch_dtype=dtype,device_map=device
|
40 |
-
#)
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
|
52 |
-
|
53 |
-
text = generate_text(messages)
|
54 |
-
histories += [user_message,{"role": "assistant", "content": text}]
|
55 |
-
#model.to("cpu")
|
56 |
-
return text
|
57 |
-
except RuntimeError as e:
|
58 |
-
print(f"An unexpected error occurred: {e}")
|
59 |
-
#model = None
|
60 |
-
|
61 |
-
return ""
|
62 |
-
|
63 |
-
iface = gr.Interface(
|
64 |
-
fn=call_generate_text,
|
65 |
-
inputs=[
|
66 |
-
gr.Textbox(lines=3, label="Input Prompt"),
|
67 |
-
gr.Textbox(lines=2, label="System Message", value="あなたは親切なアシスタントで常に日本語で返答します。"),
|
68 |
-
],
|
69 |
-
outputs=gr.Textbox(label="Generated Text"),
|
70 |
-
title=f"{model_id}",
|
71 |
-
description=f"{model_id} CPU",
|
72 |
-
)
|
73 |
-
print("Initialized")
|
74 |
|
75 |
@spaces.GPU(duration=120)
|
76 |
def generate_text(messages):
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer,torch_dtype=dtype,device_map=device) #pipeline has not to(device)
|
84 |
-
result = text_generator(messages, max_new_tokens=256, do_sample=True, temperature=0.7,repetition_penalty=1.1,top_p=0.95,top_k=40)
|
85 |
|
86 |
generated_output = result[0]["generated_text"]
|
87 |
if isinstance(generated_output, list):
|
@@ -94,6 +68,24 @@ def generate_text(messages):
|
|
94 |
else:
|
95 |
return "Unexpected output format."
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
if __name__ == "__main__":
|
98 |
-
|
99 |
-
|
|
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
5 |
import gradio as gr
|
6 |
|
7 |
+
text_generator = None
|
8 |
+
is_hugging_face = False
|
9 |
+
def init():
|
10 |
+
global text_generator
|
11 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
12 |
+
if not huggingface_token:
|
13 |
+
pass
|
14 |
+
print("no HUGGINGFACE_TOKEN if you need set secret ")
|
15 |
+
#raise ValueError("HUGGINGFACE_TOKEN environment variable is not set")
|
16 |
+
|
17 |
+
model_id = "google/gemma-2-9b-it"
|
18 |
+
model_id = "Qwen/Qwen2-0.5B-Instruct"
|
19 |
+
|
20 |
+
device = "auto" # torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
21 |
+
device = "cuda"
|
22 |
+
dtype = torch.bfloat16
|
23 |
+
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=huggingface_token)
|
25 |
+
|
26 |
+
print(model_id,device,dtype)
|
27 |
+
histories = []
|
28 |
+
#model = None
|
29 |
|
30 |
+
|
31 |
+
|
32 |
+
if not is_hugging_face:
|
33 |
+
model = AutoModelForCausalLM.from_pretrained(
|
34 |
+
model_id, token=huggingface_token ,torch_dtype=dtype,device_map=device
|
35 |
+
)
|
36 |
+
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer,torch_dtype=dtype,device_map=device ) #pipeline has not to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
if next(model.parameters()).is_cuda:
|
39 |
+
print("The model is on a GPU")
|
40 |
+
else:
|
41 |
+
print("The model is on a CPU")
|
42 |
|
43 |
+
#print(f"text_generator.device='{text_generator.device}")
|
44 |
+
if str(text_generator.device).strip() == 'cuda':
|
45 |
+
print("The pipeline is using a GPU")
|
46 |
+
else:
|
47 |
+
print("The pipeline is using a CPU")
|
48 |
|
49 |
+
print("initialized")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
@spaces.GPU(duration=120)
|
52 |
def generate_text(messages):
|
53 |
+
if is_hugging_face:#need everytime initialize for ZeroGPU
|
54 |
+
model = AutoModelForCausalLM.from_pretrained(
|
55 |
+
model_id, token=huggingface_token ,torch_dtype=dtype,device_map=device
|
56 |
+
)
|
57 |
+
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer,torch_dtype=dtype,device_map=device ) #pipeline has not to(device)
|
58 |
+
result = text_generator(messages, max_new_tokens=256, do_sample=True, temperature=0.7)
|
|
|
|
|
59 |
|
60 |
generated_output = result[0]["generated_text"]
|
61 |
if isinstance(generated_output, list):
|
|
|
68 |
else:
|
69 |
return "Unexpected output format."
|
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 |
+
text = generate_text(messages)
|
81 |
+
return text
|
82 |
+
except RuntimeError as e:
|
83 |
+
print(f"An unexpected error occurred: {e}")
|
84 |
+
|
85 |
+
return ""
|
86 |
+
|
87 |
+
demo = gr.ChatInterface(call_generate_text,type="messages")
|
88 |
+
|
89 |
if __name__ == "__main__":
|
90 |
+
init()
|
91 |
+
demo.launch(share=True)
|