Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,46 @@
|
|
| 1 |
-
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
"""
|
| 5 |
-
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
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 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 |
|
|
|
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
| 1 |
+
# app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
from peft import PeftModel
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# Load the model and tokenizer
|
| 9 |
+
adapter_model_name = "acezxn/SOC_Task_Generation_Base"
|
| 10 |
+
base_model_name = "unsloth/Llama-3.2-3B-Instruct-unsloth-bnb-4bit" # You may want to change this to a standard model name
|
| 11 |
+
|
| 12 |
+
# Load the tokenizer and model for CPU use (no bitsandbytes)
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
| 14 |
+
|
| 15 |
+
# Load model for CPU usage without 4-bit quantization
|
| 16 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
base_model_name,
|
| 18 |
+
# Do not use bitsandbytes for quantization, just use the normal model
|
| 19 |
+
load_in_4bit=False, # Ensure not using 4-bit quantization
|
| 20 |
+
device_map=None, # Use CPU (no device mapping needed)
|
| 21 |
+
trust_remote_code=True # If necessary for running with remote code
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# Move model to CPU (explicit, but optional)
|
| 25 |
+
base_model.to('cpu')
|
| 26 |
|
| 27 |
+
# Load the LoRA adapter
|
| 28 |
+
adapter_model = PeftModel.from_pretrained(base_model, adapter_model_name)
|
| 29 |
|
| 30 |
+
# Function to generate a response using the model
|
| 31 |
+
def generate_response(input_text):
|
| 32 |
+
inputs = tokenizer(input_text, return_tensors="pt").to('cpu') # Ensure inputs are on CPU
|
| 33 |
+
outputs = adapter_model.generate(**inputs)
|
| 34 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Create Gradio interface
|
| 38 |
+
iface = gr.Interface(fn=generate_response,
|
| 39 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your input here..."),
|
| 40 |
+
outputs=gr.Textbox(),
|
| 41 |
+
title="Llama LORA Adapter - SOC Task Generation",
|
| 42 |
+
description="This is a Gradio app that uses a Llama LORA adapter (acezxn/SOC_Task_Generation_Base) with the base model Llama-3.2-3B-Instruct-unsloth-bnb-4bit to generate task-related responses.")
|
| 43 |
|
| 44 |
+
# Launch the interface
|
| 45 |
if __name__ == "__main__":
|
| 46 |
+
iface.launch()
|