Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from peft import PeftModel, PeftConfig
|
| 4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
DEFAULT_SYSTEM_PROMPT = "Ты — Сайга, русскоязычный автоматический ассистент. Ты разговариваешь с людьми и помогаешь им."
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
response_template=DEFAULT_RESPONSE_TEMPLATE
|
| 17 |
-
):
|
| 18 |
-
self.message_template = message_template
|
| 19 |
-
self.response_template = response_template
|
| 20 |
-
self.messages = [{
|
| 21 |
-
"role": "system",
|
| 22 |
-
"content": system_prompt
|
| 23 |
-
}]
|
| 24 |
-
|
| 25 |
-
def add_user_message(self, message):
|
| 26 |
-
self.messages.append({
|
| 27 |
-
"role": "user",
|
| 28 |
-
"content": message
|
| 29 |
-
})
|
| 30 |
-
|
| 31 |
-
def add_bot_message(self, message):
|
| 32 |
-
self.messages.append({
|
| 33 |
-
"role": "bot",
|
| 34 |
-
"content": message
|
| 35 |
-
})
|
| 36 |
-
|
| 37 |
-
def get_prompt(self, tokenizer):
|
| 38 |
-
final_text = ""
|
| 39 |
-
for message in self.messages:
|
| 40 |
-
message_text = self.message_template.format(**message)
|
| 41 |
-
final_text += message_text
|
| 42 |
-
final_text += DEFAULT_RESPONSE_TEMPLATE
|
| 43 |
-
return final_text.strip()
|
| 44 |
-
|
| 45 |
-
def generate(model, tokenizer, prompt, generation_config):
|
| 46 |
-
data = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
|
| 47 |
-
data = {k: v.to(model.device) for k, v in data.items()}
|
| 48 |
-
output_ids = model.generate(
|
| 49 |
-
**data,
|
| 50 |
-
generation_config=generation_config
|
| 51 |
-
)[0]
|
| 52 |
-
output_ids = output_ids[len(data["input_ids"][0]):]
|
| 53 |
-
output = tokenizer.decode(output_ids, skip_special_tokens=True)
|
| 54 |
-
return output.strip()
|
| 55 |
-
|
| 56 |
-
config = PeftConfig.from_pretrained(MODEL_NAME)
|
| 57 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 58 |
-
config.base_model_name_or_path,
|
| 59 |
-
load_in_8bit=True,
|
| 60 |
-
torch_dtype=torch.float16,
|
| 61 |
-
device_map="auto"
|
| 62 |
-
)
|
| 63 |
-
model = PeftModel.from_pretrained(
|
| 64 |
-
model,
|
| 65 |
-
MODEL_NAME,
|
| 66 |
-
torch_dtype=torch.float16
|
| 67 |
-
)
|
| 68 |
-
model.eval()
|
| 69 |
-
|
| 70 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False)
|
| 71 |
-
generation_config = GenerationConfig.from_pretrained(MODEL_NAME)
|
| 72 |
-
|
| 73 |
-
# Gradio interface setup
|
| 74 |
-
def chat_with_model(user_input):
|
| 75 |
-
conversation = Conversation()
|
| 76 |
-
conversation.add_user_message(user_input)
|
| 77 |
-
prompt = conversation.get_prompt(tokenizer)
|
| 78 |
-
response = generate(model, tokenizer, prompt, generation_config)
|
| 79 |
-
conversation.add_bot_message(response)
|
| 80 |
-
return conversation.messages[-1]["content"]
|
| 81 |
|
| 82 |
iface = gr.Interface(
|
| 83 |
fn=chat_with_model,
|
| 84 |
inputs=gr.Textbox(prompt="You:"),
|
| 85 |
outputs=gr.Textbox(prompt="Bot:"),
|
| 86 |
-
live=True,
|
| 87 |
-
title="Chat with Bot",
|
| 88 |
)
|
| 89 |
|
| 90 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Load your custom model and tokenizer
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
| 6 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
|
|
|
| 7 |
|
| 8 |
+
def chat_with_model(input_text):
|
| 9 |
+
input_ids = tokenizer.encode("You: " + input_text, return_tensors="pt", max_length=1024, truncation=True)
|
| 10 |
+
response_ids = model.generate(input_ids, max_length=100, num_return_sequences=1, no_repeat_ngram_size=2)
|
| 11 |
+
reply = tokenizer.decode(response_ids[0], skip_special_tokens=True)
|
| 12 |
+
return reply
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
iface = gr.Interface(
|
| 15 |
fn=chat_with_model,
|
| 16 |
inputs=gr.Textbox(prompt="You:"),
|
| 17 |
outputs=gr.Textbox(prompt="Bot:"),
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
iface.launch()
|