nuttachot
commited on
Commit
·
d0cd438
1
Parent(s):
face026
Add application file
Browse files
app.py
CHANGED
|
@@ -11,37 +11,23 @@ hf_token = os.getenv("HF_TOKEN")
|
|
| 11 |
def load_model():
|
| 12 |
global pipe
|
| 13 |
if pipe is None:
|
| 14 |
-
cache_dir = "./model-cache"
|
| 15 |
-
os.makedirs(cache_dir, exist_ok=True)
|
| 16 |
-
|
| 17 |
pipe = pipeline(
|
| 18 |
"text-generation",
|
| 19 |
model=model_name,
|
| 20 |
trust_remote_code=True,
|
| 21 |
device_map="auto",
|
| 22 |
-
max_new_tokens=128,
|
| 23 |
-
temperature=0.9,
|
| 24 |
-
cache_dir=cache_dir, # บังคับให้เก็บ Model weights ลงใน Space
|
| 25 |
token=hf_token,
|
| 26 |
)
|
| 27 |
return pipe
|
| 28 |
|
| 29 |
@spaces.GPU
|
| 30 |
-
def predict(message
|
| 31 |
generator = load_model()
|
| 32 |
-
short_history = history[-3:] if history else []
|
| 33 |
-
|
| 34 |
-
chat_context = ""
|
| 35 |
-
for user, bot in short_history:
|
| 36 |
-
chat_context += f"User: {user}\nAssistant: {bot}\n"
|
| 37 |
-
chat_context += f"User: {message}\nAssistant:"
|
| 38 |
|
| 39 |
output = generator(
|
| 40 |
-
|
| 41 |
-
max_new_tokens=
|
| 42 |
-
do_sample=True,
|
| 43 |
temperature=0.7,
|
| 44 |
-
stream=False,
|
| 45 |
)
|
| 46 |
|
| 47 |
response = output[0]["generated_text"].split("Assistant:")[-1].strip()
|
|
@@ -51,7 +37,7 @@ def predict(message, history):
|
|
| 51 |
chat_ui = gr.ChatInterface(
|
| 52 |
fn=predict,
|
| 53 |
title="Typhoon 2.5 ZeroGPU Cache Edition",
|
| 54 |
-
description="
|
| 55 |
)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
|
|
|
| 11 |
def load_model():
|
| 12 |
global pipe
|
| 13 |
if pipe is None:
|
|
|
|
|
|
|
|
|
|
| 14 |
pipe = pipeline(
|
| 15 |
"text-generation",
|
| 16 |
model=model_name,
|
| 17 |
trust_remote_code=True,
|
| 18 |
device_map="auto",
|
|
|
|
|
|
|
|
|
|
| 19 |
token=hf_token,
|
| 20 |
)
|
| 21 |
return pipe
|
| 22 |
|
| 23 |
@spaces.GPU
|
| 24 |
+
def predict(message):
|
| 25 |
generator = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
output = generator(
|
| 28 |
+
message,
|
| 29 |
+
max_new_tokens=256,
|
|
|
|
| 30 |
temperature=0.7,
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
response = output[0]["generated_text"].split("Assistant:")[-1].strip()
|
|
|
|
| 37 |
chat_ui = gr.ChatInterface(
|
| 38 |
fn=predict,
|
| 39 |
title="Typhoon 2.5 ZeroGPU Cache Edition",
|
| 40 |
+
description="A chat interface powered by Typhoon 2.5 model with ZeroGPU",
|
| 41 |
)
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|