Clean ZeroGPU Deploy
Browse files- .gitattributes +1 -1
- README.md +9 -8
- app.py +49 -40
- requirements.txt +4 -1
.gitattributes
CHANGED
|
@@ -35,4 +35,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
*.gguf filter=lfs diff=lfs merge=lfs -text
|
| 37 |
Qwen_Base_Model_1.7b_GGUF/Qwen3-1.7B-Q8_0.gguf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
-
Qwen_Base_Model_1.7b_GGUF/Qwen3-1.7B-Q8_0.gguf filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
*.gguf filter=lfs diff=lfs merge=lfs -text
|
| 37 |
Qwen_Base_Model_1.7b_GGUF/Qwen3-1.7B-Q8_0.gguf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
Qwen_Base_Model_1.7b_GGUF/Qwen3-1.7B-Q8_0.gguf filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
sdk_version: 6.2.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
title: Llama 3.2 1B Chat
|
| 2 |
+
emoji: 🦙
|
| 3 |
+
colorFrom: blue
|
| 4 |
+
colorTo: indigo
|
| 5 |
+
sdk: gradio
|
| 6 |
+
sdk_version: 5.0.0
|
|
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Llama-3.2-1B Chat (ZeroGPU)
|
| 12 |
+
|
| 13 |
+
This Space runs Llama-3.2-1B-Instruct using Hugging Face ZeroGPU for fast inference.
|
app.py
CHANGED
|
@@ -1,63 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
print(f"Downloading {FILENAME} from {REPO_ID}...")
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
)
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
-
print(f"Error
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
FILENAME = "Llama-3.2-1B-Instruct-Q4_K_M.gguf"
|
| 20 |
-
model_path = hf_hub_download(
|
| 21 |
-
repo_id=REPO_ID,
|
| 22 |
-
filename=FILENAME
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
print(f"Loading model from {model_path}...")
|
| 26 |
-
llm = Llama(
|
| 27 |
-
model_path=model_path,
|
| 28 |
-
n_ctx=4096,
|
| 29 |
-
n_threads=2,
|
| 30 |
-
chat_format="llama-3"
|
| 31 |
-
)
|
| 32 |
|
|
|
|
| 33 |
def predict(message, history):
|
| 34 |
messages = []
|
| 35 |
for human_msg, ai_msg in history:
|
| 36 |
messages.append({"role": "user", "content": human_msg})
|
| 37 |
messages.append({"role": "assistant", "content": ai_msg})
|
| 38 |
-
|
| 39 |
messages.append({"role": "user", "content": message})
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
temperature=0.7,
|
| 46 |
-
top_p=0.
|
| 47 |
)
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
partial_message = ""
|
| 50 |
-
for
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
partial_message += delta['content']
|
| 54 |
-
yield partial_message
|
| 55 |
|
| 56 |
demo = gr.ChatInterface(
|
| 57 |
fn=predict,
|
| 58 |
-
title="Llama 3.2 1B
|
| 59 |
-
description=
|
| 60 |
-
examples=["Hello
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
+
from threading import Thread
|
| 5 |
+
import torch
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Llama 3.2 1B (Requires HF_TOKEN in Space Settings)
|
| 9 |
+
MODEL_ID = "meta-llama/Llama-3.2-1B-Instruct"
|
| 10 |
+
|
| 11 |
+
print(f"Loading {MODEL_ID}...")
|
| 12 |
+
|
| 13 |
+
# Check for token (optional but helpful warning)
|
| 14 |
+
if not os.environ.get("HF_TOKEN"):
|
| 15 |
+
print("WARNING: HF_TOKEN not found. Llama 3.2 is a gated model. This might fail 401.")
|
| 16 |
|
|
|
|
| 17 |
try:
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
MODEL_ID,
|
| 21 |
+
torch_dtype=torch.float16,
|
| 22 |
+
device_map="auto"
|
| 23 |
)
|
| 24 |
+
print("Model loaded successfully.")
|
| 25 |
except Exception as e:
|
| 26 |
+
print(f"Error loading model: {e}")
|
| 27 |
+
print("Did you accept the license at https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct and set HF_TOKEN?")
|
| 28 |
+
raise e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
@spaces.GPU
|
| 31 |
def predict(message, history):
|
| 32 |
messages = []
|
| 33 |
for human_msg, ai_msg in history:
|
| 34 |
messages.append({"role": "user", "content": human_msg})
|
| 35 |
messages.append({"role": "assistant", "content": ai_msg})
|
|
|
|
| 36 |
messages.append({"role": "user", "content": message})
|
| 37 |
+
|
| 38 |
+
# Llama 3.2 uses standard chat template
|
| 39 |
+
text = tokenizer.apply_chat_template(
|
| 40 |
+
messages,
|
| 41 |
+
tokenize=False,
|
| 42 |
+
add_generation_prompt=True
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 46 |
+
|
| 47 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 48 |
+
generate_kwargs = dict(
|
| 49 |
+
model_inputs,
|
| 50 |
+
streamer=streamer,
|
| 51 |
+
max_new_tokens=512,
|
| 52 |
+
do_sample=True,
|
| 53 |
temperature=0.7,
|
| 54 |
+
top_p=0.9
|
| 55 |
)
|
| 56 |
+
|
| 57 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 58 |
+
t.start()
|
| 59 |
|
| 60 |
partial_message = ""
|
| 61 |
+
for new_token in streamer:
|
| 62 |
+
partial_message += new_token
|
| 63 |
+
yield partial_message
|
|
|
|
|
|
|
| 64 |
|
| 65 |
demo = gr.ChatInterface(
|
| 66 |
fn=predict,
|
| 67 |
+
title="Llama 3.2 1B (ZeroGPU)",
|
| 68 |
+
description="Running on standard Hugging Face GPU hardware.",
|
| 69 |
+
examples=["Hello!", "Explain quantum physics.", "Write code for snake game."],
|
| 70 |
)
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -1,2 +1,5 @@
|
|
| 1 |
gradio
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
spaces
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
accelerate
|