Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,137 +1,209 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
-
import gc
|
|
|
|
|
5 |
|
6 |
# --- Configuration ---
|
7 |
MODEL_ID = "naver-hyperclovax/HyperCLOVAX-SEED-Text-Instruct-0.5B"
|
8 |
-
MAX_NEW_TOKENS = 512 # Limit output length for faster response on CPU
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# --- Model Loading ---
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
try:
|
|
|
16 |
model = AutoModelForCausalLM.from_pretrained(
|
17 |
MODEL_ID,
|
18 |
torch_dtype=torch.float32, # Use float32 for CPU compatibility
|
19 |
-
device_map="cpu"
|
|
|
20 |
)
|
21 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
except Exception as e:
|
26 |
-
print(f"Error loading model: {e}")
|
27 |
-
#
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
# Define stop tokens based on the example, get their IDs
|
31 |
-
stop_token_strings = ["<|endofturn|>", "<|stop|>"]
|
32 |
-
stop_token_ids = [tokenizer.convert_tokens_to_ids(token) for token in stop_token_strings]
|
33 |
-
# Also include the standard EOS token if it's different
|
34 |
-
if tokenizer.eos_token_id not in stop_token_ids:
|
35 |
-
stop_token_ids.append(tokenizer.eos_token_id)
|
36 |
|
37 |
# --- Inference Function ---
|
38 |
def predict(message, history):
|
39 |
"""
|
40 |
Generates a response using the HyperCLOVAX model based on user message and chat history.
|
|
|
41 |
"""
|
42 |
-
|
|
|
|
|
43 |
chat_history_formatted = [
|
44 |
-
{"role": "tool_list", "content": ""}, #
|
45 |
-
{"role": "system", "content":
|
46 |
]
|
47 |
for user_msg, ai_msg in history:
|
48 |
chat_history_formatted.append({"role": "user", "content": user_msg})
|
49 |
-
#
|
50 |
-
|
|
|
51 |
|
52 |
-
# Add the
|
53 |
chat_history_formatted.append({"role": "user", "content": message})
|
54 |
|
55 |
-
# Apply the chat template
|
56 |
try:
|
57 |
inputs = tokenizer.apply_chat_template(
|
58 |
chat_history_formatted,
|
59 |
-
add_generation_prompt=True, # Crucial for
|
60 |
return_dict=True,
|
61 |
return_tensors="pt"
|
62 |
-
).to(model.device) # Ensure inputs are on the
|
63 |
-
|
64 |
-
print(f"
|
65 |
-
|
66 |
|
67 |
-
|
|
|
|
|
|
|
68 |
|
69 |
-
# Generate response
|
|
|
70 |
try:
|
|
|
|
|
71 |
with torch.no_grad():
|
72 |
output_ids = model.generate(
|
73 |
**inputs,
|
74 |
max_new_tokens=MAX_NEW_TOKENS,
|
75 |
-
eos_token_id=
|
76 |
-
pad_token_id=tokenizer.eos_token_id, #
|
77 |
-
do_sample=True,
|
78 |
-
temperature=0.7,
|
79 |
-
top_p=0.9,
|
80 |
-
#
|
81 |
-
#
|
82 |
-
# If specific stopping behavior is needed beyond EOS, StoppingCriteria can be used.
|
83 |
)
|
|
|
|
|
84 |
except Exception as e:
|
85 |
-
print(f"Error during model generation: {e}")
|
86 |
-
# Clean up
|
|
|
|
|
87 |
gc.collect()
|
88 |
-
|
89 |
-
return f"Error during generation: {e}"
|
90 |
|
91 |
-
# Decode
|
92 |
-
|
93 |
new_tokens = output_ids[0, input_length:]
|
94 |
response = tokenizer.decode(new_tokens, skip_special_tokens=True)
|
95 |
|
96 |
-
print(f"Output tokens: {len(new_tokens)}")
|
97 |
-
print(f"Raw response: {response}")
|
98 |
|
99 |
-
# Clean up memory
|
100 |
del inputs
|
101 |
del output_ids
|
102 |
-
|
103 |
-
|
|
|
104 |
|
105 |
return response
|
106 |
|
107 |
# --- Gradio Interface ---
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
|
|
111 |
demo = gr.ChatInterface(
|
112 |
-
fn=predict,
|
113 |
-
chatbot=chatbot
|
114 |
-
title="π°π· HyperCLOVA X SEED (0.5B)
|
115 |
description=(
|
116 |
-
f"
|
117 |
-
f"
|
118 |
-
f"
|
119 |
-
f"
|
120 |
),
|
121 |
-
examples=
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
cache_examples=False, # Caching might consume too much memory/disk on free tier
|
128 |
-
theme="soft",
|
129 |
-
retry_btn=None,
|
130 |
-
undo_btn="Delete Previous Turn",
|
131 |
-
clear_btn="Clear Conversation",
|
132 |
)
|
133 |
|
134 |
# --- Launch the App ---
|
135 |
if __name__ == "__main__":
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import gc
|
5 |
+
import os
|
6 |
+
import datetime
|
7 |
|
8 |
# --- Configuration ---
|
9 |
MODEL_ID = "naver-hyperclovax/HyperCLOVAX-SEED-Text-Instruct-0.5B"
|
10 |
+
MAX_NEW_TOKENS = 512 # Limit output length for faster response on CPU (adjust as needed)
|
11 |
+
CPU_THREAD_COUNT = 4 # Limit threads torch uses on CPU if needed (adjust based on Space CPU core count)
|
12 |
+
|
13 |
+
# Set PyTorch CPU thread count (optional, might help prevent resource exhaustion)
|
14 |
+
# torch.set_num_threads(CPU_THREAD_COUNT)
|
15 |
+
# os.environ["OMP_NUM_THREADS"] = str(CPU_THREAD_COUNT)
|
16 |
+
# os.environ["MKL_NUM_THREADS"] = str(CPU_THREAD_COUNT)
|
17 |
+
|
18 |
+
print("--- Environment Setup ---")
|
19 |
+
print(f"PyTorch version: {torch.__version__}")
|
20 |
+
print(f"Running on device: cpu") # Explicitly state we expect CPU
|
21 |
+
print(f"Torch Threads: {torch.get_num_threads()}") # Check default threads
|
22 |
|
23 |
# --- Model Loading ---
|
24 |
+
print(f"--- Loading Model: {MODEL_ID} ---")
|
25 |
+
print("This might take a few minutes, especially on the first launch...")
|
26 |
+
|
27 |
try:
|
28 |
+
# Load model explicitly onto CPU with float32 (standard for CPU compatibility)
|
29 |
model = AutoModelForCausalLM.from_pretrained(
|
30 |
MODEL_ID,
|
31 |
torch_dtype=torch.float32, # Use float32 for CPU compatibility
|
32 |
+
device_map="cpu" # Explicitly map to CPU
|
33 |
+
# low_cpu_mem_usage=True # Can sometimes help on low RAM, but might slow down loading
|
34 |
)
|
35 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
36 |
+
model.eval() # Set model to evaluation mode
|
37 |
+
print("--- Model and Tokenizer Loaded Successfully on CPU ---")
|
38 |
+
|
39 |
+
# --- Stop Token Configuration ---
|
40 |
+
# Get IDs for specified stop tokens and the standard EOS token
|
41 |
+
stop_token_strings = ["<|endofturn|>", "<|stop|>"]
|
42 |
+
stop_token_ids_list = [tokenizer.convert_tokens_to_ids(token) for token in stop_token_strings]
|
43 |
+
|
44 |
+
# Ensure the official EOS token is also included if not already present
|
45 |
+
if tokenizer.eos_token_id not in stop_token_ids_list:
|
46 |
+
stop_token_ids_list.append(tokenizer.eos_token_id)
|
47 |
+
|
48 |
+
# Remove None values if any token wasn't found (though they should be in this vocab)
|
49 |
+
stop_token_ids_list = [tid for tid in stop_token_ids_list if tid is not None]
|
50 |
+
|
51 |
+
if not stop_token_ids_list:
|
52 |
+
print("Warning: Could not find any stop token IDs. Using default EOS only.")
|
53 |
+
stop_token_ids_list = [tokenizer.eos_token_id]
|
54 |
+
|
55 |
+
print(f"Using Stop Token IDs: {stop_token_ids_list}")
|
56 |
+
|
57 |
except Exception as e:
|
58 |
+
print(f"!!! Error loading model: {e}")
|
59 |
+
# Clean up memory if partial loading occurred
|
60 |
+
del model
|
61 |
+
del tokenizer
|
62 |
+
gc.collect()
|
63 |
+
# Raise a Gradio error to make it visible in the UI
|
64 |
+
raise gr.Error(f"Failed to load the model {MODEL_ID}. Please check the Space logs. Error: {e}")
|
65 |
+
|
66 |
+
|
67 |
+
# --- System Prompt ---
|
68 |
+
# Use a dynamic date and the correct model name as per the card example structure
|
69 |
+
def get_system_prompt():
|
70 |
+
# current_date = datetime.datetime.now().strftime("%Yλ
%mμ %dμΌ(%a)") # Korean date format
|
71 |
+
current_date = datetime.datetime.now().strftime("%Y-%m-%d (%A)") # English date format is safer for consistency
|
72 |
+
return (
|
73 |
+
f"- AI μΈμ΄λͺ¨λΈμ μ΄λ¦μ \"CLOVA X\" μ΄λ©° λ€μ΄λ²μμ λ§λ€μλ€.\n"
|
74 |
+
# f"- μ€λμ {current_date}μ΄λ€.\n" # Dynamic date can be added if desired
|
75 |
+
f"- μ¬μ©μμ μ§λ¬Έμ λν΄ μΉμ νκ³ μμΈνκ² νκ΅μ΄λ‘ λ΅λ³ν΄μΌ νλ€."
|
76 |
+
)
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# --- Inference Function ---
|
80 |
def predict(message, history):
|
81 |
"""
|
82 |
Generates a response using the HyperCLOVAX model based on user message and chat history.
|
83 |
+
Handles chat formatting, generation, decoding, and memory management.
|
84 |
"""
|
85 |
+
system_prompt = get_system_prompt()
|
86 |
+
|
87 |
+
# 1. Format conversation history according to the model's expected template
|
88 |
chat_history_formatted = [
|
89 |
+
{"role": "tool_list", "content": ""}, # Required by the model card example
|
90 |
+
{"role": "system", "content": system_prompt}
|
91 |
]
|
92 |
for user_msg, ai_msg in history:
|
93 |
chat_history_formatted.append({"role": "user", "content": user_msg})
|
94 |
+
# Ensure assistant response is included correctly, potentially adding endofturn if needed by template logic,
|
95 |
+
# but apply_chat_template usually handles this.
|
96 |
+
chat_history_formatted.append({"role": "assistant", "content": ai_msg}) # Append the actual AI response
|
97 |
|
98 |
+
# Add the latest user message
|
99 |
chat_history_formatted.append({"role": "user", "content": message})
|
100 |
|
101 |
+
# 2. Apply the chat template
|
102 |
try:
|
103 |
inputs = tokenizer.apply_chat_template(
|
104 |
chat_history_formatted,
|
105 |
+
add_generation_prompt=True, # Crucial for instruction-following models
|
106 |
return_dict=True,
|
107 |
return_tensors="pt"
|
108 |
+
).to(model.device) # Ensure inputs are on the correct device (CPU)
|
109 |
+
input_length = inputs['input_ids'].shape[1]
|
110 |
+
print(f"\nInput tokens: {input_length}")
|
111 |
+
# print(f"Formatted input text (approx): {tokenizer.decode(inputs['input_ids'][0])}") # For debugging
|
112 |
|
113 |
+
except Exception as e:
|
114 |
+
print(f"!!! Error applying chat template: {e}")
|
115 |
+
# Provide feedback to the user
|
116 |
+
return f"μ€λ₯: μ
λ ₯ νμμ μ²λ¦¬νλ μ€ λ¬Έμ κ° λ°μνμ΅λλ€. ({e})"
|
117 |
|
118 |
+
# 3. Generate response using the model
|
119 |
+
output_ids = None # Initialize output_ids
|
120 |
try:
|
121 |
+
print("Generating response...")
|
122 |
+
# Use torch.no_grad() to reduce memory footprint during inference
|
123 |
with torch.no_grad():
|
124 |
output_ids = model.generate(
|
125 |
**inputs,
|
126 |
max_new_tokens=MAX_NEW_TOKENS,
|
127 |
+
eos_token_id=stop_token_ids_list, # Use the list of stop token IDs
|
128 |
+
pad_token_id=tokenizer.eos_token_id, # Set pad token ID to EOS token ID
|
129 |
+
do_sample=True, # Enable sampling for less repetitive output
|
130 |
+
temperature=0.7, # Control randomness (lower = more focused)
|
131 |
+
top_p=0.9, # Use nucleus sampling
|
132 |
+
# num_beams=1, # Use 1 for sampling (greedy is default if do_sample=False)
|
133 |
+
# early_stopping=True # Stop generation early if EOS is reached
|
|
|
134 |
)
|
135 |
+
print("Generation complete.")
|
136 |
+
|
137 |
except Exception as e:
|
138 |
+
print(f"!!! Error during model generation: {e}")
|
139 |
+
# Clean up potentially large tensors in case of error
|
140 |
+
del inputs
|
141 |
+
if output_ids is not None: del output_ids
|
142 |
gc.collect()
|
143 |
+
return f"μ€λ₯: μλ΅μ μμ±νλ μ€ λ¬Έμ κ° λ°μνμ΅λλ€. ({e})"
|
|
|
144 |
|
145 |
+
# 4. Decode the response
|
146 |
+
# We need to decode only the newly generated tokens, excluding the input tokens
|
147 |
new_tokens = output_ids[0, input_length:]
|
148 |
response = tokenizer.decode(new_tokens, skip_special_tokens=True)
|
149 |
|
150 |
+
print(f"Output tokens: {len(new_tokens)}")
|
151 |
+
# print(f"Raw response: '{response}'") # Log the raw decoded output
|
152 |
|
153 |
+
# 5. Clean up memory
|
154 |
del inputs
|
155 |
del output_ids
|
156 |
+
del new_tokens
|
157 |
+
gc.collect() # Explicitly run garbage collection
|
158 |
+
print("Memory cleaned.")
|
159 |
|
160 |
return response
|
161 |
|
162 |
# --- Gradio Interface ---
|
163 |
+
print("--- Setting up Gradio Interface ---")
|
164 |
+
|
165 |
+
# Use ChatInterface for a user-friendly chat experience
|
166 |
+
chatbot_component = gr.Chatbot(
|
167 |
+
label="HyperCLOVA X SEED (0.5B) λν",
|
168 |
+
bubble_full_width=False,
|
169 |
+
height=600
|
170 |
+
)
|
171 |
+
|
172 |
+
# Define examples relevant to the model's strengths (Korean)
|
173 |
+
examples = [
|
174 |
+
["λ€μ΄λ² ν΄λ‘λ°Xλ 무μμΈκ°μ?"],
|
175 |
+
["μλ’°λ©κ±° λ°©μ μκ³Ό μμμνμ κ΄κ³λ₯Ό μ€λͺ
ν΄μ£ΌμΈμ."],
|
176 |
+
["λ₯λ¬λ λͺ¨λΈ νμ΅ κ³Όμ μ λ¨κ³λ³λ‘ μλ €μ€."],
|
177 |
+
["μ μ£Όλ μ¬ν κ³νμ μΈμ°κ³ μλλ°, 3λ° 4μΌ μΆμ² μ½μ€ μ’ μ§μ€λ?"],
|
178 |
+
]
|
179 |
|
180 |
+
# Create the Gradio ChatInterface
|
181 |
demo = gr.ChatInterface(
|
182 |
+
fn=predict, # The function to call for generating responses
|
183 |
+
chatbot=chatbot_component, # The chatbot display component
|
184 |
+
title="π°π· λ€μ΄λ² HyperCLOVA X SEED (0.5B) λ°λͺ¨",
|
185 |
description=(
|
186 |
+
f"**λͺ¨λΈ:** {MODEL_ID}\n"
|
187 |
+
f"**νκ²½:** Hugging Face λ¬΄λ£ CPU (16GB RAM)\n"
|
188 |
+
f"**μ£Όμ:** CPUμμ μ€νλλ―λ‘ μλ΅ μμ±μ λ€μ μκ°μ΄ 걸릴 μ μμ΅λλ€ (νΉν 첫 μλ΅). "
|
189 |
+
f"μ΅λ μμ± ν ν° μλ {MAX_NEW_TOKENS}κ°λ‘ μ νλ©λλ€."
|
190 |
),
|
191 |
+
examples=examples,
|
192 |
+
cache_examples=False, # Disable caching on free tier to save disk/memory
|
193 |
+
theme="soft", # Use a soft theme
|
194 |
+
retry_btn="λ€μ μλ",
|
195 |
+
undo_btn="μ΄μ ν΄ μμ ",
|
196 |
+
clear_btn="λν μ΄κΈ°ν",
|
|
|
|
|
|
|
|
|
|
|
197 |
)
|
198 |
|
199 |
# --- Launch the App ---
|
200 |
if __name__ == "__main__":
|
201 |
+
print("--- Launching Gradio App ---")
|
202 |
+
# queue() is important for handling multiple users, especially with slow inference on CPU
|
203 |
+
# Use concurrency_count=1 if resource exhaustion occurs, otherwise default might be okay.
|
204 |
+
demo.queue(
|
205 |
+
# default_concurrency_limit=1 # Limit concurrent requests if needed
|
206 |
+
).launch(
|
207 |
+
# share=False # Set to True to get a public link (requires login)
|
208 |
+
# server_name="0.0.0.0" # To make it accessible on the network if running locally
|
209 |
+
)
|