Upload 3 files
Browse files- README.md +18 -11
- app.py +62 -58
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,17 +1,24 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
|
| 11 |
-
hf_oauth_scopes:
|
| 12 |
-
- inference-api
|
| 13 |
-
license: apache-2.0
|
| 14 |
-
short_description: A chatbot to generate code
|
| 15 |
---
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Mistral Nemo Alpaca Finetune Demo
|
| 3 |
+
emoji: 🦙
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
hardware: t4-medium
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Mistral Nemo Alpaca Finetune Demo
|
| 14 |
+
|
| 15 |
+
This Space provides an interactive chat demo for the [TensorVizion/mistral-nemo-alpaca-finetune](https://huggingface.co/TensorVizion/mistral-nemo-alpaca-finetune) model.
|
| 16 |
+
|
| 17 |
+
## Model Details
|
| 18 |
+
- **Base Model:** Mistral Nemo (12B)
|
| 19 |
+
- **Finetuned by:** TensorVizion
|
| 20 |
+
- **Training:** Fine-tuned 2x faster with Unsloth and Hugging Face's TRL library on Alpaca-style instructions.
|
| 21 |
+
- **Precision:** Loaded in 4-bit quantization (NF4) for efficient inference on consumer-grade GPUs.
|
| 22 |
+
|
| 23 |
+
## Hardware Requirements
|
| 24 |
+
This Space is configured to run on a `t4-medium` GPU (16GB VRAM). The 4-bit quantization ensures the 12B parameter model fits comfortably within this memory limit (~8GB VRAM usage). If you experience out-of-memory errors or want faster generation, consider upgrading to an `a10g-small` hardware tier in the Space settings.
|
app.py
CHANGED
|
@@ -1,69 +1,73 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
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
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 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 |
-
with gr.Blocks() as demo:
|
| 63 |
-
with gr.Sidebar():
|
| 64 |
-
gr.LoginButton()
|
| 65 |
-
chatbot.render()
|
| 66 |
-
|
| 67 |
-
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 4 |
|
| 5 |
+
MODEL_ID = "TensorVizion/mistral-nemo-alpaca-finetune"
|
| 6 |
|
| 7 |
+
# Configure 4-bit quantization to fit the 12B model in a T4 GPU (16GB VRAM)
|
| 8 |
+
bnb_config = BitsAndBytesConfig(
|
| 9 |
+
load_in_4bit=True,
|
| 10 |
+
bnb_4bit_quant_type="nf4",
|
| 11 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 12 |
+
bnb_4bit_use_double_quant=True,
|
| 13 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
print("Loading tokenizer...")
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
print("Loading model in 4-bit...")
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
MODEL_ID,
|
| 21 |
+
quantization_config=bnb_config,
|
| 22 |
+
device_map="auto",
|
| 23 |
+
torch_dtype=torch.bfloat16,
|
| 24 |
+
)
|
| 25 |
|
| 26 |
+
def generate_response(message, history):
|
| 27 |
+
# Format chat history for the model
|
| 28 |
+
chat_history = []
|
| 29 |
+
for user_msg, assistant_msg in history:
|
| 30 |
+
chat_history.append({"role": "user", "content": user_msg})
|
| 31 |
+
chat_history.append({"role": "assistant", "content": assistant_msg})
|
| 32 |
+
|
| 33 |
+
chat_history.append({"role": "user", "content": message})
|
| 34 |
+
|
| 35 |
+
# Apply the model's built-in chat template (handles Mistral Nemo formatting)
|
| 36 |
+
input_ids = tokenizer.apply_chat_template(
|
| 37 |
+
chat_history,
|
| 38 |
+
add_generation_prompt=True,
|
| 39 |
+
return_tensors="pt"
|
| 40 |
+
).to(model.device)
|
| 41 |
+
|
| 42 |
+
# Generate response
|
| 43 |
+
with torch.no_grad():
|
| 44 |
+
output_ids = model.generate(
|
| 45 |
+
input_ids,
|
| 46 |
+
max_new_tokens=512,
|
| 47 |
+
temperature=0.7,
|
| 48 |
+
top_p=0.9,
|
| 49 |
+
do_sample=True,
|
| 50 |
+
pad_token_id=tokenizer.eos_token_id
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Decode only the newly generated tokens
|
| 54 |
+
response = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
|
| 55 |
+
return response
|
| 56 |
|
| 57 |
+
# Create Gradio Chat Interface
|
| 58 |
+
demo = gr.ChatInterface(
|
| 59 |
+
fn=generate_response,
|
| 60 |
+
title="Mistral Nemo 12B Alpaca Finetune",
|
| 61 |
+
description="Chat with the TensorVizion Mistral Nemo 12B model. Runs efficiently in 4-bit quantization.",
|
| 62 |
+
examples=[
|
| 63 |
+
"Explain the concept of quantum entanglement in simple terms.",
|
| 64 |
+
"Write a short Python script to scrape a website's title.",
|
| 65 |
+
"What are the main differences between supervised and unsupervised learning?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
],
|
| 67 |
+
retry_btn=None,
|
| 68 |
+
undo_btn=None,
|
| 69 |
+
clear_btn="Clear Chat"
|
| 70 |
)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
if __name__ == "__main__":
|
| 73 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.4.0
|
| 2 |
+
transformers>=4.44.0
|
| 3 |
+
accelerate>=0.33.0
|
| 4 |
+
bitsandbytes>=0.43.0
|
| 5 |
+
gradio>=4.44.0
|