File size: 1,010 Bytes
ced524d
 
 
 
96ffa43
 
ced524d
 
96ffa43
 
ced524d
96ffa43
ced524d
 
 
 
96ffa43
ced524d
96ffa43
ced524d
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import torch
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers import BitsAndBytesConfig
import os
token = os.getenv("HUGGINGFACE_TOKEN")

def load_quantized_model():
    """ Function to load a quantized model"""
    tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct",token=token)
    config = BitsAndBytesConfig.from_dict({"load_in_4bit": True})
    model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B-Instruct", quantization_config=config,token=token)
    return model, tokenizer

model, tokenizer = load_quantized_model()

 
def generate_response(prompt):
    """Simple prediction function for Gradio"""
    inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
    outputs = model.generate(**inputs)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

iface = gr.Interface(
    fn=generate_response,
    inputs="text",
    outputs="text",
    title="Quantized Model Chatbot"
)

iface.launch()