Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
from transformers import BitsAndBytesConfig
|
| 5 |
+
|
| 6 |
+
# Function to load a quantized model
|
| 7 |
+
def load_quantized_model():
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
|
| 9 |
+
config = BitsAndBytesConfig.from_dict({"load_in_4bit": True})
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B-Instruct", quantization_config=config)
|
| 11 |
+
return model, tokenizer
|
| 12 |
+
|
| 13 |
+
model, tokenizer = load_quantized_model()
|
| 14 |
+
|
| 15 |
+
# Simple prediction function for Gradio
|
| 16 |
+
def generate_response(prompt):
|
| 17 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 18 |
+
outputs = model.generate(**inputs)
|
| 19 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 20 |
+
|
| 21 |
+
# Gradio interface
|
| 22 |
+
iface = gr.Interface(
|
| 23 |
+
fn=generate_response,
|
| 24 |
+
inputs="text",
|
| 25 |
+
outputs="text",
|
| 26 |
+
title="Quantized Model Chatbot"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
iface.launch()
|