|
import spaces |
|
import gradio as gr |
|
from huggingface_hub import InferenceClient, login |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
from peft import PeftModel |
|
import os |
|
import torch |
|
import time |
|
import bitsandbytes |
|
|
|
@spaces.GPU |
|
def dummy(): |
|
pass |
|
|
|
|
|
def wait_for_gpu(timeout=60): |
|
print("Waiting for ZeroGPU to initialize...") |
|
start_time = time.time() |
|
while not torch.cuda.is_available(): |
|
if time.time() - start_time > timeout: |
|
raise RuntimeError("Timeout: ZeroGPU did not initialize within the expected time.") |
|
time.sleep(2) |
|
print("ZeroGPU is ready! Proceeding with model loading...") |
|
|
|
|
|
wait_for_gpu() |
|
|
|
print(f"Is CUDA available: {torch.cuda.is_available()}") |
|
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}") |
|
|
|
|
|
base_model_name = "meta-llama/Llama-3.1-8B" |
|
|
|
|
|
lora_model_name = "starnernj/Early-Christian-Church-Fathers-LLaMA-3.1-Fine-Tuned" |
|
|
|
|
|
login(token=os.getenv("HuggingFaceFineGrainedReadToken")) |
|
|
|
|
|
|
|
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
base_model_name, |
|
load_in_4bit=True, |
|
torch_dtype=torch.float16, |
|
device_map="auto" |
|
) |
|
|
|
|
|
model = PeftModel.from_pretrained(model, lora_model_name) |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(base_model_name) |
|
|
|
|
|
def chatbot_response(user_input): |
|
inputs = tokenizer(user_input, return_tensors="pt") |
|
outputs = model.generate(**inputs, max_length=400) |
|
return tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
interface = gr.Interface( |
|
fn=chatbot_response, |
|
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."), |
|
outputs="text", |
|
title="Early Christian Church Fathers Fine-Tuned LLaMA 3.1 8B with LoRA", |
|
description="A chatbot using my fine-tuned LoRA adapter on LLaMA 3.1 8B, tuned on thousands of writings of the early Christian Church Fathers.", |
|
) |
|
|
|
time.sleep(10) |
|
|
|
interface.launch() |