Spaces:
Runtime error
Runtime error
DanielWong76
commited on
Commit
•
ed47413
1
Parent(s):
b2560cc
Added specific use for my model
Browse files- app.py +49 -54
- requirements.txt +5 -1
app.py
CHANGED
@@ -1,63 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
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 |
-
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 |
-
|
40 |
-
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
from peft import PeftModel
|
4 |
+
import torch
|
5 |
|
6 |
+
# Load the base model and LoRA adapter
|
7 |
+
model_name = "unsloth/llama-3-8b-bnb-4bit" # Replace with your base model
|
8 |
+
adapter_model_name = "your-hf-username/your-lora-model" # Replace with your LoRA adapter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Load the base model and tokenizer
|
11 |
+
base_model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
12 |
+
model = PeftModel.from_pretrained(base_model, adapter_model_name)
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
|
15 |
+
# Define the Alpaca-style prompt
|
16 |
+
alpaca_prompt = """### Instruction:
|
17 |
+
{instruction}
|
18 |
|
19 |
+
### Input:
|
20 |
+
{input}
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
### Output:
|
23 |
+
{output}
|
|
|
|
|
|
|
24 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Define the function to generate text
|
27 |
+
def generate_response(instruction, input_text):
|
28 |
+
# Format the prompt with the instruction and input text
|
29 |
+
prompt = alpaca_prompt.format(
|
30 |
+
instruction=instruction,
|
31 |
+
input=input_text,
|
32 |
+
output="" # Leave output blank for generation
|
33 |
+
)
|
34 |
+
|
35 |
+
# Tokenize the prompt and move it to the GPU
|
36 |
+
inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
|
37 |
+
|
38 |
+
# Generate the response from the model
|
39 |
+
outputs = model.generate(**inputs, max_new_tokens=64, use_cache=True)
|
40 |
+
|
41 |
+
# Decode the output into human-readable text
|
42 |
+
generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
43 |
+
|
44 |
+
return generated_text
|
45 |
+
|
46 |
+
# Gradio Interface with two inputs: Instruction and Input Text
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=generate_response,
|
49 |
+
inputs=[
|
50 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter the instruction here..."),
|
51 |
+
gr.inputs.Textbox(lines=5, placeholder="Enter the input text here...")
|
52 |
+
],
|
53 |
+
outputs="text",
|
54 |
+
title="Alpaca-Style Instruction-Input-Output Model"
|
55 |
+
)
|
56 |
|
57 |
+
# Launch the Gradio app
|
58 |
+
iface.launch()
|
requirements.txt
CHANGED
@@ -1 +1,5 @@
|
|
1 |
-
huggingface_hub==0.22.2
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.22.2
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
gradio
|
5 |
+
peft
|