Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
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("Grandediw/lora_model")
|
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 |
-
|
19 |
-
|
20 |
-
for
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
|
|
28 |
response = ""
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
temperature=temperature,
|
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=[
|
49 |
-
gr.
|
50 |
-
gr.Slider(minimum=1, maximum=
|
51 |
-
gr.Slider(minimum=0.1, maximum=
|
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 |
+
from unsloth import FastLanguageModel
|
3 |
+
from transformers import TextStreamer
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Load the model
|
6 |
+
model_path = "Grandediw/lora_model" # Replace with the actual model path
|
7 |
+
model = FastLanguageModel.for_inference(model_path)
|
8 |
+
tokenizer = model.tokenizer # Use the tokenizer from the model
|
9 |
|
10 |
+
# Define the response function
|
11 |
def respond(
|
12 |
message,
|
13 |
history: list[tuple[str, str]],
|
|
|
14 |
max_tokens,
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
+
# Build chat history
|
19 |
+
messages = [{"role": "user", "content": message}]
|
20 |
+
for user_msg, assistant_msg in history:
|
21 |
+
messages.append({"role": "user", "content": user_msg})
|
22 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
23 |
+
|
24 |
+
# Prepare inputs
|
25 |
+
inputs = tokenizer.apply_chat_template(
|
26 |
+
messages,
|
27 |
+
tokenize=True,
|
28 |
+
add_generation_prompt=True,
|
29 |
+
return_tensors="pt",
|
30 |
+
).to("cuda")
|
31 |
|
32 |
+
# Stream response
|
33 |
response = ""
|
34 |
+
streamer = TextStreamer(tokenizer, skip_prompt=True)
|
35 |
+
model.generate(
|
36 |
+
input_ids=inputs,
|
37 |
+
streamer=streamer,
|
38 |
+
max_new_tokens=max_tokens,
|
39 |
+
use_cache=True,
|
40 |
temperature=temperature,
|
41 |
+
min_p=top_p,
|
42 |
+
)
|
43 |
+
return response
|
|
|
|
|
|
|
44 |
|
45 |
+
# Build the Gradio ChatInterface
|
|
|
|
|
|
|
46 |
demo = gr.ChatInterface(
|
47 |
+
fn=respond,
|
48 |
additional_inputs=[
|
49 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
|
50 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=1.5, step=0.1, label="Temperature"),
|
51 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
],
|
53 |
)
|
54 |
|
|
|
55 |
if __name__ == "__main__":
|
56 |
demo.launch()
|