Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,168 +1,149 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import time
|
| 3 |
import spaces
|
| 4 |
-
import
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
-
from
|
| 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 |
-
|
| 33 |
-
|
| 34 |
-
device = "cuda" # for GPU usage or "cpu" for CPU usage
|
| 35 |
-
|
| 36 |
-
quantization_config = BitsAndBytesConfig(
|
| 37 |
-
load_in_4bit=True,
|
| 38 |
-
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 39 |
-
bnb_4bit_use_double_quant=True,
|
| 40 |
-
bnb_4bit_quant_type= "nf4")
|
| 41 |
-
|
| 42 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 43 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 44 |
-
MODEL,
|
| 45 |
-
torch_dtype=torch.bfloat16,
|
| 46 |
-
device_map="auto",
|
| 47 |
-
quantization_config=quantization_config)
|
| 48 |
-
|
| 49 |
-
@spaces.GPU()
|
| 50 |
-
def stream_chat(
|
| 51 |
-
message: str,
|
| 52 |
-
history: list,
|
| 53 |
-
system_prompt: str,
|
| 54 |
-
temperature: float = 0.8,
|
| 55 |
-
max_new_tokens: int = 1024,
|
| 56 |
-
top_p: float = 1.0,
|
| 57 |
-
top_k: int = 20,
|
| 58 |
-
penalty: float = 1.2,
|
| 59 |
):
|
| 60 |
-
|
| 61 |
-
print(f'history: {history}')
|
| 62 |
-
|
| 63 |
-
conversation = [
|
| 64 |
-
{"role": "system", "content": system_prompt}
|
| 65 |
-
]
|
| 66 |
-
for prompt, answer in history:
|
| 67 |
-
conversation.extend([
|
| 68 |
-
{"role": "user", "content": prompt},
|
| 69 |
-
{"role": "assistant", "content": answer},
|
| 70 |
-
])
|
| 71 |
-
|
| 72 |
-
conversation.append({"role": "user", "content": message})
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
),
|
| 148 |
-
gr.Slider(
|
| 149 |
-
minimum=0.0,
|
| 150 |
-
maximum=2.0,
|
| 151 |
-
step=0.1,
|
| 152 |
-
value=1.2,
|
| 153 |
-
label="Repetition penalty",
|
| 154 |
-
render=False,
|
| 155 |
-
),
|
| 156 |
-
],
|
| 157 |
-
examples=[
|
| 158 |
-
["Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option."],
|
| 159 |
-
["What are 5 creative things I could do with my kids' art? I don't want to throw them away, but it's also so much clutter."],
|
| 160 |
-
["Tell me a random fun fact about the Roman Empire."],
|
| 161 |
-
["Show me a code snippet of a website's sticky header in CSS and JavaScript."],
|
| 162 |
-
],
|
| 163 |
-
cache_examples=False,
|
| 164 |
)
|
| 165 |
-
|
| 166 |
|
| 167 |
if __name__ == "__main__":
|
| 168 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import spaces
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
| 6 |
+
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
| 7 |
+
from llama_cpp_agent.chat_history import BasicChatHistory
|
| 8 |
+
from llama_cpp_agent.chat_history.messages import Roles
|
| 9 |
import gradio as gr
|
| 10 |
+
from huggingface_hub import hf_hub_download
|
| 11 |
+
|
| 12 |
+
huggingface_token = os.getenv("HF_TOKEN")
|
| 13 |
+
|
| 14 |
+
# Download the Meta-Llama-3.1-8B-Instruct model
|
| 15 |
+
hf_hub_download(
|
| 16 |
+
repo_id="itaudit/2024-08-14_V3_394_sample",
|
| 17 |
+
filename="unsloth.Q4_K_M.gguf",
|
| 18 |
+
local_dir="./models",
|
| 19 |
+
token=huggingface_token
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
llm = None
|
| 23 |
+
llm_model = None
|
| 24 |
+
|
| 25 |
+
@spaces.GPU(duration=120)
|
| 26 |
+
def respond(
|
| 27 |
+
message,
|
| 28 |
+
history: list[tuple[str, str]],
|
| 29 |
+
model,
|
| 30 |
+
system_message,
|
| 31 |
+
max_tokens,
|
| 32 |
+
temperature,
|
| 33 |
+
top_p,
|
| 34 |
+
top_k,
|
| 35 |
+
repeat_penalty,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
):
|
| 37 |
+
chat_template = MessagesFormatterType.GEMMA_2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
global llm
|
| 40 |
+
global llm_model
|
| 41 |
|
| 42 |
+
# Load model only if it's not already loaded or if a new model is selected
|
| 43 |
+
if llm is None or llm_model != model:
|
| 44 |
+
try:
|
| 45 |
+
llm = Llama(
|
| 46 |
+
model_path=f"models/{model}",
|
| 47 |
+
flash_attn=True,
|
| 48 |
+
n_gpu_layers=81, # Adjust based on available GPU resources
|
| 49 |
+
n_batch=1024,
|
| 50 |
+
n_ctx=8192,
|
| 51 |
+
)
|
| 52 |
+
llm_model = model
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"Error loading model: {str(e)}"
|
| 55 |
+
|
| 56 |
+
provider = LlamaCppPythonProvider(llm)
|
| 57 |
+
|
| 58 |
+
agent = LlamaCppAgent(
|
| 59 |
+
provider,
|
| 60 |
+
system_prompt=f"{system_message}",
|
| 61 |
+
predefined_messages_formatter_type=chat_template,
|
| 62 |
+
debug_output=True
|
| 63 |
)
|
| 64 |
+
|
| 65 |
+
settings = provider.get_provider_default_settings()
|
| 66 |
+
settings.temperature = temperature
|
| 67 |
+
settings.top_k = top_k
|
| 68 |
+
settings.top_p = top_p
|
| 69 |
+
settings.max_tokens = max_tokens
|
| 70 |
+
settings.repeat_penalty = repeat_penalty
|
| 71 |
+
settings.stream = True
|
| 72 |
+
|
| 73 |
+
messages = BasicChatHistory()
|
| 74 |
+
|
| 75 |
+
# Add user and assistant messages to the history
|
| 76 |
+
for msn in history:
|
| 77 |
+
user = {'role': Roles.user, 'content': msn[0]}
|
| 78 |
+
assistant = {'role': Roles.assistant, 'content': msn[1]}
|
| 79 |
+
messages.add_message(user)
|
| 80 |
+
messages.add_message(assistant)
|
| 81 |
+
|
| 82 |
+
# Stream the response
|
| 83 |
+
try:
|
| 84 |
+
stream = agent.get_chat_response(
|
| 85 |
+
message,
|
| 86 |
+
llm_sampling_settings=settings,
|
| 87 |
+
chat_history=messages,
|
| 88 |
+
returns_streaming_generator=True,
|
| 89 |
+
print_output=False
|
| 90 |
+
)
|
| 91 |
|
| 92 |
+
outputs = ""
|
| 93 |
+
for output in stream:
|
| 94 |
+
outputs += output
|
| 95 |
+
yield outputs
|
| 96 |
+
except Exception as e:
|
| 97 |
+
yield f"Error during response generation: {str(e)}"
|
| 98 |
+
|
| 99 |
+
description = """<p align="center">Using the Meta-Llama-3.1-8B-Instruct Model</p>"""
|
| 100 |
+
|
| 101 |
+
demo = gr.ChatInterface(
|
| 102 |
+
respond,
|
| 103 |
+
additional_inputs=[
|
| 104 |
+
gr.Dropdown([
|
| 105 |
+
'Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf'
|
| 106 |
+
],
|
| 107 |
+
value="Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf",
|
| 108 |
+
label="Model"
|
| 109 |
+
),
|
| 110 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
| 111 |
+
gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
|
| 112 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 113 |
+
gr.Slider(
|
| 114 |
+
minimum=0.1,
|
| 115 |
+
maximum=1.0,
|
| 116 |
+
value=0.95,
|
| 117 |
+
step=0.05,
|
| 118 |
+
label="Top-p",
|
| 119 |
+
),
|
| 120 |
+
gr.Slider(
|
| 121 |
+
minimum=0,
|
| 122 |
+
maximum=100,
|
| 123 |
+
value=40,
|
| 124 |
+
step=1,
|
| 125 |
+
label="Top-k",
|
| 126 |
+
),
|
| 127 |
+
gr.Slider(
|
| 128 |
+
minimum=0.0,
|
| 129 |
+
maximum=2.0,
|
| 130 |
+
value=1.1,
|
| 131 |
+
step=0.1,
|
| 132 |
+
label="Repetition penalty",
|
| 133 |
+
),
|
| 134 |
+
],
|
| 135 |
+
retry_btn="Retry",
|
| 136 |
+
undo_btn="Undo",
|
| 137 |
+
clear_btn="Clear",
|
| 138 |
+
submit_btn="Send",
|
| 139 |
+
title="Chat with Meta-Llama-3.1-8B-Instruct using llama.cpp",
|
| 140 |
+
description=description,
|
| 141 |
+
chatbot=gr.Chatbot(
|
| 142 |
+
scale=1,
|
| 143 |
+
likeable=False,
|
| 144 |
+
show_copy_button=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
)
|
| 146 |
+
)
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
+
demo.launch()
|