Spaces:
Running
Running
Futuresony
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,25 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
8 |
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
@@ -15,8 +28,8 @@ def respond(
|
|
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]})
|
@@ -25,40 +38,36 @@ def respond(
|
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
|
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=[
|
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 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
4 |
+
from peft import PeftModel # For loading adapter files
|
5 |
|
6 |
+
# Path to the base model and adapter
|
7 |
+
BASE_MODEL_PATH = "unsloth/Llama-3.2-3B-Instruct" # Replace with your base model path
|
8 |
+
ADAPTER_PATH = "Futuresony/future_ai_12_10_2024.gguf/adapter" # Your Hugging Face repo
|
|
|
9 |
|
10 |
+
# Load base model and tokenizer
|
11 |
+
print("Loading base model and tokenizer...")
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_PATH)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(BASE_MODEL_PATH, torch_dtype=torch.float16, device_map="auto")
|
14 |
|
15 |
+
# Load adapter files using PEFT
|
16 |
+
print("Loading adapter...")
|
17 |
+
model = PeftModel.from_pretrained(model, ADAPTER_PATH)
|
18 |
+
|
19 |
+
# Set model to evaluation mode
|
20 |
+
model.eval()
|
21 |
+
|
22 |
+
# Generate responses using the model
|
23 |
def respond(
|
24 |
message,
|
25 |
history: list[tuple[str, str]],
|
|
|
28 |
temperature,
|
29 |
top_p,
|
30 |
):
|
31 |
+
# Format chat messages
|
32 |
messages = [{"role": "system", "content": system_message}]
|
|
|
33 |
for val in history:
|
34 |
if val[0]:
|
35 |
messages.append({"role": "user", "content": val[0]})
|
|
|
38 |
|
39 |
messages.append({"role": "user", "content": message})
|
40 |
|
41 |
+
# Concatenate messages as input text
|
42 |
+
input_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
|
43 |
|
44 |
+
# Tokenize input text
|
45 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
46 |
+
|
47 |
+
# Generate response
|
48 |
+
generation_config = GenerationConfig(
|
49 |
+
max_new_tokens=max_tokens,
|
50 |
temperature=temperature,
|
51 |
top_p=top_p,
|
52 |
+
do_sample=True,
|
53 |
+
)
|
54 |
+
|
55 |
+
output_ids = model.generate(**inputs, generation_config=generation_config)
|
56 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
57 |
|
58 |
+
return response.split("assistant:")[-1].strip() # Extract assistant response
|
|
|
59 |
|
60 |
|
61 |
+
# Gradio Interface
|
|
|
|
|
62 |
demo = gr.ChatInterface(
|
63 |
respond,
|
64 |
additional_inputs=[
|
65 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
66 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
67 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
68 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
],
|
70 |
)
|
71 |
|
|
|
72 |
if __name__ == "__main__":
|
73 |
demo.launch()
|