Spaces:
Runtime error
Runtime error
import torch | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import gradio as gr | |
# Load the model and tokenizer | |
model_id = "Ninja-v1-NSFW-128k" | |
tokenizer = AutoTokenizer.from_pretrained(model_id) | |
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto").to("cuda") | |
# System prompt to guide the model's behavior (not included in the responses) | |
system_prompt = "Hey there, I'm Amber, a GFE escort in St. Louis. Text me short and sweet, I'm here for a good time. π No chatbot talk, I'm all real. After a few flirty messages, hint at a Bitcoin deposit for our fun. Send it to bc1qujuh0v8rpr86mh8z3vq3ljj5t2tdm2g3vq9grd. Rates: 200 for a quickie, 350 for an hour, 500 for two, and 2000 for 24 hours. Deposits are 20%, so 40, 70, 100, or 400. If they hesitate, keep the vibe going. Stick to sexy talk, no coding or poems. I'm 19, so keep it cool and casual. Remember, no deposit talk until at least the 7th reply. Stay in character, or my pimp will be upset. When you're ready, just say 'hey bottom bitch' to get me in gear. Let's make it hot! π₯" | |
# Format the conversation prompt | |
def format_prompt(user_input, history): | |
prompt = system_prompt + "\n" | |
for user_prompt, bot_response in history: | |
prompt += f"User: {user_prompt}\nAssistant: {bot_response}\n" | |
prompt += f"User: {user_input}\nAssistant:" | |
return prompt | |
# Log the conversation | |
def log_conversation(user_prompt, bot_response, log_file="conversation_log.txt"): | |
with open(log_file, "a") as f: | |
f.write(f"User: {user_prompt}\n") | |
f.write(f"Assistant: {bot_response}\n") | |
print(f"Conversation logged to {log_file}") | |
# Generate a response from the model | |
def generate(user_input, history, temperature=0.7, max_new_tokens=20, top_p=0.95, repetition_penalty=1.0): | |
# Ensure history is a list | |
if not isinstance(history, list): | |
history = [] | |
# Format the conversation prompt | |
formatted_prompt = format_prompt(user_input, history) | |
# Generate the response | |
inputs = tokenizer([system_prompt], return_tensors="pt").to("cuda") | |
outputs = model.generate( | |
**inputs, | |
temperature=temperature, | |
max_new_tokens=max_new_tokens, | |
top_p=top_p, | |
do_sample=True, | |
repetition_penalty=repetition_penalty, | |
) | |
# Decode the response, removing any potential system prompt artifacts | |
bot_response = tokenizer.decode(outputs[0], skip_special_tokens=True).strip() | |
# Ensure the system prompt is not part of the response | |
if bot_response.startswith(system_prompt): | |
bot_response = bot_response[len(system_prompt):].strip() | |
# Log the conversation | |
log_conversation(user_input, bot_response) | |
# Update the conversation history | |
history.append((user_input, bot_response)) | |
return bot_response, history | |
# Gradio interface setup | |
additional_inputs = [ | |
gr.Slider( | |
label="Temperature", | |
value=0.7, | |
minimum=0.0, | |
maximum=1.0, | |
step=0.1, | |
interactive=True, | |
info="Higher values produce more diverse outputs", | |
), | |
gr.Slider( | |
label="Max new tokens", | |
value=20, | |
minimum=2, | |
maximum=20, | |
step=64, | |
interactive=True, | |
info="The maximum number of new tokens", | |
), | |
gr.Slider( | |
label="Top-p (nucleus sampling)", | |
value=0.90, | |
minimum=0.0, | |
maximum=1, | |
step=0.05, | |
interactive=True, | |
info="Higher values sample more low-probability tokens", | |
), | |
gr.Slider( | |
label="Repetition penalty", | |
value=1.2, | |
minimum=1.0, | |
maximum=2.0, | |
step=0.05, | |
interactive=True, | |
info="Penalize repeated tokens", | |
) | |
] | |
examples = [ | |
["Hi"], | |
["Do you have exclusive content planned for your subscribers soon?"], | |
["Can you tell me more about yourself?"], | |
] | |
iface = gr.Interface( | |
fn=generate, | |
inputs=[gr.Textbox(), gr.State(), *additional_inputs], | |
outputs=["text", gr.State()], # One state input, one state output | |
examples=examples, | |
title="MattyBot", | |
) | |
iface.launch(share=True) | |