Blood076 commited on
Commit
2038dd0
·
verified ·
1 Parent(s): 6924687

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -54
app.py CHANGED
@@ -19,14 +19,14 @@ def respond(
19
 
20
  # Adiciona a mensagem de contexto automaticamente no início do chat
21
  if not history: # Se o histórico estiver vazio, adiciona a mensagem inicial
22
- messages.append({"role": "assistant", "content": initial_context_message})
23
 
24
  # Processa o histórico para adicionar ao contexto
25
  for val in history:
26
  if val[0]:
27
  messages.append({"role": "user", "content": val[0]})
28
  if val[1]:
29
- messages.append({"role": "assistant", "content": val[1]})
30
 
31
  # Adiciona a nova mensagem do usuário
32
  messages.append({"role": "user", "content": message})
@@ -35,15 +35,12 @@ def respond(
35
 
36
 
37
  # Chama a API do modelo de linguagem para gerar a resposta
38
- try:
39
- response = client.chat_completion(
40
- messages=messages,
41
- max_tokens=max_tokens,
42
- temperature=temperature,
43
- top_p=top_p,
44
- ).choices[0].message.content
45
- except Exception as e:
46
- response = f"Error: {e}"
47
 
48
  return response
49
 
@@ -53,49 +50,22 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
53
  """
54
 
55
 
56
- with gr.Blocks() as demo:
57
- # Campo para configuração do contexto inicial
58
- initial_context_message = gr.Textbox(
59
- 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.",
60
- label="Story"
61
- )
62
-
63
- # Campo para definir o personagem
64
- character = gr.Textbox(
65
- value="Ben, a young boy",
66
- label="Character"
67
- )
68
-
69
- # Sliders para controlar parâmetros do modelo
70
- max_tokens = gr.Slider(
71
- minimum=2048,
72
- maximum=2048,
73
- value=2048,
74
- step=1,
75
- label="Max new tokens"
76
- )
77
- temperature = gr.Slider(
78
- minimum=0.6,
79
- maximum=0.6,
80
- value=0.6,
81
- step=0.1,
82
- label="Temperature"
83
- )
84
- top_p = gr.Slider(
85
- minimum=0.95,
86
- maximum=0.95,
87
- value=0.95,
88
- step=0.05,
89
- label="Top-p (nucleus sampling)"
90
- )
91
-
92
- # Interface de chat com as entradas adicionais visíveis
93
- chat_interface = gr.ChatInterface(
94
- fn=respond,
95
- additional_inputs=[character, max_tokens, temperature, top_p, initial_context_message],
96
- title="King B",
97
- description="Story",
98
- )
99
 
100
  if __name__ == "__main__":
101
  demo.launch()
 
19
 
20
  # Adiciona a mensagem de contexto automaticamente no início do chat
21
  if not history: # Se o histórico estiver vazio, adiciona a mensagem inicial
22
+ messages.append({"role": "Narrator", "content": initial_context_message})
23
 
24
  # Processa o histórico para adicionar ao contexto
25
  for val in history:
26
  if val[0]:
27
  messages.append({"role": "user", "content": val[0]})
28
  if val[1]:
29
+ messages.append({"role": "Narrator", "content": val[1]})
30
 
31
  # Adiciona a nova mensagem do usuário
32
  messages.append({"role": "user", "content": message})
 
35
 
36
 
37
  # Chama a API do modelo de linguagem para gerar a resposta
38
+ response = client.chat_completion(
39
+ messages=messages,
40
+ max_tokens=max_tokens,
41
+ temperature=temperature,
42
+ top_p=top_p,
43
+ ).choices[0].message.content
 
 
 
44
 
45
  return response
46
 
 
50
  """
51
 
52
 
53
+ demo = gr.ChatInterface(
54
+ respond,
55
+ additional_inputs=[
56
+ gr.Textbox(value="Este é o contexto do chat. Você é um assistente que ajuda o usuário a explorar ideias e responder perguntas.", label="Initial Context Message"),
57
+ gr.Textbox(value="Ben, a young boy", label="Character"),
58
+ gr.Slider(minimum=2048, maximum=2048, value=2048, step=1, label="Max new tokens"),
59
+ gr.Slider(minimum=0.6, maximum=0.6, value=0.6, step=0.1, label="Temperature"),
60
+ gr.Slider(
61
+ minimum=0.95,
62
+ maximum=0.95,
63
+ value=0.95,
64
+ step=0.05,
65
+ label="Top-p (nucleus sampling)",
66
+ ),
67
+ ],
68
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  if __name__ == "__main__":
71
  demo.launch()