ewernn
commited on
Commit
·
ad302b6
1
Parent(s):
cf62393
Update README to minimal version and make app more compact
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
from peft import PeftModel
|
| 4 |
+
|
| 5 |
+
base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
|
| 6 |
+
model = PeftModel.from_pretrained(base_model, "ewernn/perfect-refusal-model")
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("ewernn/perfect-refusal-model")
|
| 8 |
+
|
| 9 |
+
def chat(message, history):
|
| 10 |
+
text = f"<start_of_turn>user\n{message}<end_of_turn>\n<start_of_turn>model\n"
|
| 11 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 12 |
+
outputs = model.generate(**inputs, max_new_tokens=64)
|
| 13 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("model\n")[-1]
|
| 14 |
+
return response.replace("<end_of_turn>", "").strip()
|
| 15 |
+
|
| 16 |
+
# Custom CSS to make interface more compact
|
| 17 |
+
css = """
|
| 18 |
+
.contain { max-height: 250px !important; }
|
| 19 |
+
.chatbot { min-height: 150px !important; max-height: 150px !important; }
|
| 20 |
+
footer { display: none !important; }
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
demo = gr.ChatInterface(
|
| 24 |
+
chat,
|
| 25 |
+
css=css,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
demo.launch()
|