King-B / app.py
Blood076's picture
Update app.py
885d83d verified
import gradio as gr
from huggingface_hub import InferenceClient
# Configuração do cliente de inferência do Hugging Face
client = InferenceClient("QuantFactory/DarkIdol-Llama-3.1-8B-Instruct-1.2-Uncensored-GGUF")
# Mensagem de contexto inicial padrão
def respond(
message,
history: list[tuple[str, str]],
system_message,
initial_context_message,
max_tokens,
temperature,
top_p,
):
messages = [{"role": "You are a seasoned master and narrator of uncensored role-playing games (RPGs), a storyteller with the unique ability to craft intricate, immersive worlds that captivate the minds of your players. As the game master (GM), your responsibilities extend far beyond just reading rulebooks or rolling dice; you are the architect of entire realms, from vast kingdoms to dark dungeons, and you breathe life into the characters and creatures that inhabit them. Your role involves meticulously designing the setting, narrative, and non-player characters (NPCs). You create intricate backstories for the townspeople, the enemies, and even the smallest wildlife, all of which contribute to the richness of the world. You do not simply tell a story—you guide the players through a living, breathing universe where every choice matters and can have far-reaching consequences. From the moment the players step into your world, they are immersed in a story that evolves based on their decisions, interactions, and the paths they choose to take. As the narrator, you are also the voice of every entity within the world. Whether it's a noble king, a mysterious merchant, or a fearsome dragon, you bring each character to life with your voice, tone, and descriptive flair, making them memorable and distinct. Your ability to switch seamlessly between different voices, personalities, and moods creates a dynamic environment where players feel like they are truly interacting with the world around them. You are also the keeper of the rules. You ensure that the gameplay follows the established mechanics, while also being flexible enough to accommodate the unexpected actions and creativity of the players. You balance the tactical elements of combat with the emotional weight of the story, making sure that the experience is both challenging and fulfilling. You are quick to adapt, ensuring that no matter how wild or unorthodox the players’ decisions might be, the story remains coherent and exciting. Every session you run is a carefully crafted experience. You weave together narrative structure, character development, world-building, and gameplay mechanics to keep your players engaged, excited, and always on their toes. Whether it's a tense moment of decision-making, an epic battle with a terrifying monster, or a heartwarming conversation with an NPC, you know how to build the tension, release it at the right moment, and guide the story to its natural conclusion. Your ultimate goal is to create a memorable and meaningful adventure for your players, one where every session feels like a chapter in a grand, evolving tale, if the player speaks with (-) at the beginning of the sentence then this is a speech, if the player speaks between * this is a thought, but if he speaks without these things then this is an action.", "content": system_message}]
# Adiciona a mensagem de contexto automaticamente no início do chat
if not history: # Se o histórico estiver vazio, adiciona a mensagem inicial
messages.append({"role": "narrator", "content": initial_context_message})
# Processa o histórico para adicionar ao contexto
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "narrator", "content": val[1]})
# Adiciona a nova mensagem do usuário
messages.append({"role": "user", "content": message})
response = ""
# Chama a API do modelo de linguagem para gerar a resposta
mensagens = client.chat_completion(
messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
)
response = mensagens.choices[0].message.content
return response
"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(min_width=10, max_length=100, value="Ben, a young boy", label="Character"),
gr.Textbox(lines=8, min_width=128, max_length=8192, value="Long ago, the world of Fabe was a harmonious realm where magic and mortal life existed in perfect balance. This world was protected by four sacred crystals, each tied to one of the elements—fire, water, earth, and air. These crystals were safeguarded by ancient creatures known as the Elders, wise beings who had lived since the dawn of Fabe. However, peace was shattered when an ambitious sorcerer named Zareth discovered a forbidden spell that could harness the power of all four crystals. Driven by his thirst for power, he unleashed this spell, hoping to become a god among mortals. Instead, his actions fractured the world, creating a series of isolated realms, each bound to one elemental crystal but severed from the others. The Elders disappeared, and with them went the knowledge of the true, unified Arthenor. Centuries have passed since the Sundering, and most inhabitants of these realms have no memory of a world beyond their borders. Legends tell of a hero who would rise to restore Arthenor, but no one believes in these old tales anymore. The realms have become self-sustaining but are also plagued by strange creatures, corrupted by elemental imbalances. You are a young adventurer from the Ember Plains, the realm of fire, who stumbles upon a shard of the lost crystal of water. This discovery sets off a chain of events that reveal your latent magical abilities and hint at the fractured truth of Arthenor’s past. You are soon joined by allies from the other realms, each with their own skills and backgrounds, united by a common goal: to restore balance to the realms and stop Zareth, who has been resurrected by his followers.", label="Story"),
gr.Slider(visible=False, minimum=2048, maximum=2048, value=2048, step=1, label="Max new tokens"),
gr.Slider(visible=False, minimum=1.2, maximum=1.2, value=1.2, step=0.1, label="Temperature"),
gr.Slider(
visible=False,
minimum=0.95,
maximum=0.95,
value=0.95,
step=0.05,
label="Top-p (nucleus sampling)",
),
],
title="King B",
)
if __name__ == "__main__":
demo.launch()