desert
commited on
Commit
·
b2af35c
1
Parent(s):
dd71874
init inference
Browse files- app.py +19 -46
- requirements.txt +0 -1
app.py
CHANGED
|
@@ -1,29 +1,12 @@
|
|
| 1 |
-
import os
|
| 2 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 3 |
-
|
| 4 |
import gradio as gr
|
| 5 |
-
from
|
| 6 |
-
import torch
|
| 7 |
-
|
| 8 |
-
max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
|
| 9 |
-
dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
|
| 10 |
-
load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
# Load model and tokenizer with the device set to "cpu"
|
| 16 |
-
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 17 |
-
model_name="llama_lora_model_1",
|
| 18 |
-
max_seq_length=max_seq_length,
|
| 19 |
-
dtype=dtype,
|
| 20 |
-
load_in_4bit=load_in_4bit,
|
| 21 |
-
)
|
| 22 |
|
| 23 |
-
# Move the model to CPU (even if it was initially loaded with GPU support)
|
| 24 |
-
model.to(device)
|
| 25 |
-
|
| 26 |
-
# Respond function
|
| 27 |
def respond(
|
| 28 |
message,
|
| 29 |
history: list[tuple[str, str]],
|
|
@@ -32,44 +15,34 @@ def respond(
|
|
| 32 |
temperature,
|
| 33 |
top_p,
|
| 34 |
):
|
| 35 |
-
# Prepare the system message
|
| 36 |
messages = [{"role": "system", "content": system_message}]
|
| 37 |
|
| 38 |
-
# Add history to the messages
|
| 39 |
for val in history:
|
| 40 |
if val[0]:
|
| 41 |
messages.append({"role": "user", "content": val[0]})
|
| 42 |
if val[1]:
|
| 43 |
messages.append({"role": "assistant", "content": val[1]})
|
| 44 |
|
| 45 |
-
# Add the current message from the user
|
| 46 |
messages.append({"role": "user", "content": message})
|
| 47 |
|
| 48 |
-
|
| 49 |
-
inputs = tokenizer.apply_chat_template(
|
| 50 |
-
messages,
|
| 51 |
-
tokenize=True,
|
| 52 |
-
add_generation_prompt=True,
|
| 53 |
-
return_tensors="pt",
|
| 54 |
-
)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
temperature=temperature,
|
| 61 |
top_p=top_p,
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
# Decode the generated output
|
| 66 |
-
response = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
demo = gr.ChatInterface(
|
| 74 |
respond,
|
| 75 |
additional_inputs=[
|
|
@@ -86,6 +59,6 @@ demo = gr.ChatInterface(
|
|
| 86 |
],
|
| 87 |
)
|
| 88 |
|
| 89 |
-
if __name__ == "__main__":
|
| 90 |
-
demo.launch()
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 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("Mat17892/llama_lora_G14")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def respond(
|
| 11 |
message,
|
| 12 |
history: list[tuple[str, str]],
|
|
|
|
| 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 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
+
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
for message in client.chat_completion(
|
| 31 |
+
messages,
|
| 32 |
+
max_tokens=max_tokens,
|
| 33 |
+
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
+
):
|
| 37 |
+
token = message.choices[0].delta.content
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
response += token
|
| 40 |
+
yield response
|
| 41 |
|
| 42 |
|
| 43 |
+
"""
|
| 44 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
+
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
|
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1 @@
|
|
| 1 |
huggingface_hub==0.25.2
|
| 2 |
-
unsloth
|
|
|
|
| 1 |
huggingface_hub==0.25.2
|
|
|