Kolumbus Lindh
changes
377072f
import gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
# Define a function to load the model from the Hugging Face Hub
def load_model():
repo_id = "forestav/gguf_lora_model" # Your Hugging Face repo
model_file = "unsloth.F16.gguf" # Model file in GGUF format
# Download the model file
local_path = hf_hub_download(repo_id=repo_id, filename=model_file)
print(f"Model loaded from: {local_path}")
# Load the model using llama_cpp
model = Llama(model_path=local_path, n_ctx=2048, n_threads=8, use_metal=False)
return model
# Initialize the model
model = load_model()
# Define the response function for chat interaction
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
):
try:
# Prepare the system message and chat history
messages = [{"role": "system", "content": system_message}]
# Add the history of the conversation
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
# Add the current message from the user
messages.append({"role": "user", "content": message})
# Make the model prediction
response = model.create_chat_completion(
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
)
return response["choices"][0]["message"]["content"]
except Exception as e:
# Return error message if something goes wrong
return f"Error: {e}"
# Define the Gradio interface
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p (nucleus sampling)",
),
],
)
# Launch the app
if __name__ == "__main__":
demo.launch()