Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
model_id = "
|
| 6 |
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
}
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
def chat(prompt, history=[], mode="Standard Mode"):
|
| 23 |
-
settings = modes[mode]
|
| 24 |
-
system_prompt = settings["prompt"]
|
| 25 |
-
temp = settings["temperature"]
|
| 26 |
-
|
| 27 |
-
full_prompt = system_prompt + "\n"
|
| 28 |
for user, bot in history:
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
output = model.generate(
|
| 35 |
**inputs,
|
| 36 |
max_new_tokens=300,
|
|
|
|
| 37 |
do_sample=True,
|
| 38 |
-
|
| 39 |
-
top_p=0.95,
|
| 40 |
pad_token_id=tokenizer.eos_token_id
|
| 41 |
)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
history.append((
|
| 46 |
-
return
|
| 47 |
-
|
| 48 |
-
with gr.Blocks() as demo:
|
| 49 |
-
mode_dropdown = gr.Dropdown(choices=list(modes.keys()), value="Standard Mode", label="Select Mode")
|
| 50 |
-
chat_interface = gr.ChatInterface(fn=lambda message, history: chat(message, history, mode_dropdown.value))
|
| 51 |
-
|
| 52 |
-
mode_dropdown.change(
|
| 53 |
-
fn=lambda mode: None,
|
| 54 |
-
inputs=mode_dropdown,
|
| 55 |
-
outputs=[],
|
| 56 |
-
queue=False
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
demo.add(mode_dropdown)
|
| 60 |
-
demo.add(chat_interface)
|
| 61 |
|
| 62 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
model_id = "openchat/openchat-3.5-1210"
|
| 6 |
|
| 7 |
+
# Load model
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_id,
|
| 11 |
+
device_map="auto",
|
| 12 |
+
torch_dtype=torch.float16
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# System prompt
|
| 16 |
+
def build_prompt(history, user_input):
|
| 17 |
+
system_prompt = "<|system|>\nYou are a helpful AI assistant.\n"
|
| 18 |
+
messages = system_prompt
|
| 19 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
for user, bot in history:
|
| 21 |
+
messages += f"<|user|>\n{user}\n<|assistant|>\n{bot}\n"
|
| 22 |
+
messages += f"<|user|>\n{user_input}\n<|assistant|>\n"
|
| 23 |
+
return messages
|
| 24 |
|
| 25 |
+
def chat(user_input, history=[]):
|
| 26 |
+
prompt = build_prompt(history, user_input)
|
| 27 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 28 |
|
| 29 |
output = model.generate(
|
| 30 |
**inputs,
|
| 31 |
max_new_tokens=300,
|
| 32 |
+
temperature=0.7,
|
| 33 |
do_sample=True,
|
| 34 |
+
top_p=0.9,
|
|
|
|
| 35 |
pad_token_id=tokenizer.eos_token_id
|
| 36 |
)
|
| 37 |
|
| 38 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 39 |
+
answer = response.split("<|assistant|>")[-1].strip()
|
| 40 |
+
history.append((user_input, answer))
|
| 41 |
+
return answer, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
gr.ChatInterface(chat, title="OpenChat AI Assistant").launch()
|