Spaces:
Runtime error
Runtime error
Trying to use model output
Browse files
app.py
CHANGED
@@ -1,48 +1,59 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
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 |
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
messages,
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
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,6 +70,5 @@ demo = gr.ChatInterface(
|
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from unsloth import FastLanguageModel
|
3 |
+
from transformers import TextStreamer
|
4 |
+
import torch
|
5 |
|
6 |
+
# Initialize the model and tokenizer
|
7 |
+
def initialize_model(model_name, max_seq_length, dtype, load_in_4bit):
|
8 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
9 |
+
model_name=model_name, # Your Lora model name
|
10 |
+
max_seq_length=max_seq_length,
|
11 |
+
dtype=dtype,
|
12 |
+
load_in_4bit=load_in_4bit,
|
13 |
+
)
|
14 |
+
FastLanguageModel.for_inference(model) # Enable 2x faster inference
|
15 |
+
return model, tokenizer
|
16 |
|
17 |
+
# Load model and tokenizer
|
18 |
+
model_name = "DominusDeorum/llama-3.2-lora_model" # Replace with your model
|
19 |
+
max_seq_length = 2048 # Adjust as needed
|
20 |
+
dtype = torch.float16 # Set dtype (can also use torch.bfloat16, etc.)
|
21 |
+
load_in_4bit = True # Set to True if using 4-bit inference
|
22 |
|
23 |
+
model, tokenizer = initialize_model(model_name, max_seq_length, dtype, load_in_4bit)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
26 |
+
# Prepare the chat history and system message
|
27 |
+
messages = [{"role": "system", "content": system_message}]
|
28 |
+
|
29 |
for val in history:
|
30 |
if val[0]:
|
31 |
messages.append({"role": "user", "content": val[0]})
|
32 |
if val[1]:
|
33 |
messages.append({"role": "assistant", "content": val[1]})
|
34 |
+
|
35 |
+
# Add the user's new message
|
36 |
messages.append({"role": "user", "content": message})
|
37 |
+
|
38 |
+
# Prepare inputs for the model
|
39 |
+
inputs = tokenizer.apply_chat_template(
|
|
|
40 |
messages,
|
41 |
+
tokenize=True,
|
42 |
+
add_generation_prompt=True,
|
43 |
+
return_tensors="pt",
|
44 |
+
).to("cuda")
|
45 |
+
|
46 |
+
# Generate response with streaming
|
47 |
+
text_streamer = TextStreamer(tokenizer, skip_prompt=True)
|
48 |
+
response = ""
|
49 |
+
|
50 |
+
for output in model.generate(input_ids=inputs, streamer=text_streamer, max_new_tokens=max_tokens,
|
51 |
+
use_cache=True, temperature=temperature, top_p=top_p):
|
52 |
+
token = tokenizer.decode(output, skip_special_tokens=True)
|
53 |
response += token
|
54 |
yield response
|
55 |
|
56 |
+
# Set up Gradio interface
|
|
|
|
|
|
|
57 |
demo = gr.ChatInterface(
|
58 |
respond,
|
59 |
additional_inputs=[
|
|
|
70 |
],
|
71 |
)
|
72 |
|
|
|
73 |
if __name__ == "__main__":
|
74 |
+
demo.launch()
|