Omnibus commited on
Commit
785c709
1 Parent(s): 4db2c35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -70
app.py CHANGED
@@ -1,26 +1,33 @@
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  import random
4
- client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
5
 
6
- from prompts import GAME_MASTER, COMPRESS_HISTORY
7
  def format_prompt(message, history):
8
  prompt=""
9
- '''
10
  prompt = "<s>"
11
 
12
  for user_prompt, bot_response in history:
13
  prompt += f"[INST] {user_prompt} [/INST]"
14
  prompt += f" {bot_response}</s> "
15
- '''
16
  prompt += f"[INST] {message} [/INST]"
17
  return prompt
18
 
19
- def compress_history(history,temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
 
 
 
 
 
 
 
 
20
  formatted_prompt=f"{COMPRESS_HISTORY.format(history=history)}"
21
  generate_kwargs = dict(
22
  temperature=temperature,
23
- max_new_tokens=max_new_tokens,
24
  top_p=top_p,
25
  repetition_penalty=repetition_penalty,
26
  do_sample=True,
@@ -34,10 +41,11 @@ def compress_history(history,temperature=0.9, max_new_tokens=256, top_p=0.95, re
34
  return output
35
 
36
  MAX_HISTORY=100
37
-
38
- def generate(
39
- prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
40
- ):
 
41
  temperature = float(temperature)
42
  if temperature < 1e-2:
43
  temperature = 1e-2
@@ -53,6 +61,14 @@ def generate(
53
  #seed=42,
54
  )
55
  cnt=0
 
 
 
 
 
 
 
 
56
  for ea in history:
57
  print (ea)
58
  for l in ea:
@@ -60,79 +76,133 @@ def generate(
60
  cnt+=len(l.split("\n"))
61
  print(f'cnt:: {cnt}')
62
  if cnt > MAX_HISTORY:
63
- history = compress_history(history, temperature, max_new_tokens, top_p, repetition_penalty)
64
- formatted_prompt = format_prompt(f"{GAME_MASTER.format(history=history)}, {prompt}", history)
65
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
66
  output = ""
67
 
68
  for response in stream:
69
  output += response.token.text
70
- yield output
71
-
72
- lines = output.strip().strip("\n").split("\n")
 
 
 
 
 
 
 
 
 
 
73
  #history=""
 
 
 
 
 
 
 
 
 
 
 
74
  for i,line in enumerate(lines):
75
- if line.startswith("1. "):
76
- print(line)
77
- if line.startswith("2. "):
78
- print(line)
79
- if line.startswith("3. "):
80
- print(line)
81
- if line.startswith("4. "):
82
- print(line)
83
- if line.startswith("5. "):
84
- print(line)
85
- return output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
 
 
 
 
 
87
 
88
- additional_inputs=[
89
- gr.Textbox(
90
- label="System Prompt",
91
- max_lines=1,
92
- interactive=True,
93
- ),
94
- gr.Slider(
95
- label="Temperature",
96
- value=0.9,
97
- minimum=0.0,
98
- maximum=1.0,
99
- step=0.05,
100
- interactive=True,
101
- info="Higher values produce more diverse outputs",
102
- ),
103
- gr.Slider(
104
- label="Max new tokens",
105
- value=1048,
106
- minimum=0,
107
- maximum=1048*10,
108
- step=64,
109
- interactive=True,
110
- info="The maximum numbers of new tokens",
111
- ),
112
- gr.Slider(
113
- label="Top-p (nucleus sampling)",
114
- value=0.90,
115
- minimum=0.0,
116
- maximum=1,
117
- step=0.05,
118
- interactive=True,
119
- info="Higher values sample more low-probability tokens",
120
- ),
121
- gr.Slider(
122
- label="Repetition penalty",
123
- value=1.2,
124
- minimum=1.0,
125
- maximum=2.0,
126
- step=0.05,
127
- interactive=True,
128
- info="Penalize repeated tokens",
129
- )
130
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
 
 
 
132
  examples=[["Start the Game", None, None, None, None, None, ],
133
  ["Start a Game based in the year 1322", None, None, None, None, None,],
134
  ]
135
-
136
  gr.ChatInterface(
137
  fn=generate,
138
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
@@ -140,4 +210,5 @@ gr.ChatInterface(
140
  title="Mixtral RPG Game Master",
141
  examples=examples,
142
  concurrency_limit=20,
143
- ).launch(share=True,show_api=True)
 
 
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  import random
 
4
 
5
+ from prompts import GAME_MASTER, COMPRESS_HISTORY, ADJUST_STATS
6
  def format_prompt(message, history):
7
  prompt=""
8
+
9
  prompt = "<s>"
10
 
11
  for user_prompt, bot_response in history:
12
  prompt += f"[INST] {user_prompt} [/INST]"
13
  prompt += f" {bot_response}</s> "
14
+
15
  prompt += f"[INST] {message} [/INST]"
16
  return prompt
17
 
18
+
19
+ temperature=0.99
20
+ top_p=0.95
21
+ repetition_penalty=1.0
22
+
23
+ def compress_history(history,temperature=temperature,top_p=top_p,repetition_penalty=repetition_penalty):
24
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
25
+
26
+ print("COMPRESSING")
27
  formatted_prompt=f"{COMPRESS_HISTORY.format(history=history)}"
28
  generate_kwargs = dict(
29
  temperature=temperature,
30
+ max_new_tokens=1024,
31
  top_p=top_p,
32
  repetition_penalty=repetition_penalty,
33
  do_sample=True,
 
41
  return output
42
 
43
  MAX_HISTORY=100
44
+ opts=[]
45
+ def generate(prompt, history,max_new_tokens,health,temperature=temperature,top_p=top_p,repetition_penalty=repetition_penalty):
46
+ opts.clear()
47
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
48
+
49
  temperature = float(temperature)
50
  if temperature < 1e-2:
51
  temperature = 1e-2
 
61
  #seed=42,
62
  )
63
  cnt=0
64
+ stats=health
65
+ history1=history
66
+ '''
67
+ stats="*******************\n"
68
+ for eac in health:
69
+ stats+=f'{eac}\n'
70
+ stats+="*******************\n"
71
+ '''
72
  for ea in history:
73
  print (ea)
74
  for l in ea:
 
76
  cnt+=len(l.split("\n"))
77
  print(f'cnt:: {cnt}')
78
  if cnt > MAX_HISTORY:
79
+ history1 = compress_history(str(history), temperature, top_p, repetition_penalty)
80
+ formatted_prompt = format_prompt(f"{GAME_MASTER.format(history=history1,stats=stats,dice=random.randint(1,10))}, {prompt}", history)
81
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
82
  output = ""
83
 
84
  for response in stream:
85
  output += response.token.text
86
+ if history:
87
+ yield [(prompt,output)],stats,None,None
88
+ else:
89
+ yield [(prompt,output)],stats,None,None
90
+ generate_kwargs2 = dict(
91
+ temperature=temperature,
92
+ max_new_tokens=128,
93
+ top_p=top_p,
94
+ repetition_penalty=repetition_penalty,
95
+ do_sample=True,
96
+ seed=random.randint(1,99999999999)
97
+ #seed=42,
98
+ )
99
  #history=""
100
+ #formatted_prompt2 = format_prompt(f"{ADJUST_STATS.format(history=output,health=health)}, {prompt}", history)
101
+ #stream2 = client.text_generation(f"{ADJUST_STATS.format(history=output,health=health)}", **generate_kwargs2, stream=True, details=True, return_full_text=False)
102
+ #output2=""
103
+ #for response in stream2:
104
+ # output2 += response.token.text
105
+
106
+ lines = output.strip().strip("\n").split("\n")
107
+ skills=[]
108
+ skill_dict={}
109
+ option_drop=[]
110
+ new_stat="*******************\n"
111
  for i,line in enumerate(lines):
112
+ if "Choices:" in line:
113
+ for z in range(1,5):
114
+ try:
115
+ if f'{z}' in lines[i+z]:
116
+ print(lines[i+z].split(" ",1)[1])
117
+ opts.append(lines[i+z].split(" ",1)[1])
118
+ except Exception:
119
+ pass
120
+ if ": " in line[:12]:
121
+ try:
122
+ lab_1 = line.split(": ")[0]
123
+
124
+ skill_1 = line.split(": ")[1].split(" ")[0].split("<")[0]
125
+ skill_1=int(skill_1)
126
+ skill_dict[lab_1]=skill_1
127
+ #skill ={lab_1:skill_1}
128
+
129
+ new_stat += f'{lab_1}: {skill_1}\n'
130
+
131
+ print(skills)
132
+ except Exception as e:
133
+ print (f'--Error :: {e}')
134
+ print(f'Line:: {line}')
135
+ skills.append(skill_dict)
136
+ new_stat+="*******************\n"
137
+ stats=new_stat
138
+ option_drop=gr.Dropdown(label="Choices", choices=[e for e in opts])
139
 
140
+ if history:
141
+ history.append((prompt,output))
142
+ yield history,stats,skills,option_drop
143
+ else:
144
+ yield [(prompt,output)],stats,skills,option_drop
145
 
146
+ def clear_fn():
147
+ return None,None
148
+
149
+ base_stats=[
150
+ {},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  ]
152
+ text_stats='''*******************
153
+ Health: 100
154
+ Power: 20
155
+ Strength: 24
156
+ Intelligence: 15
157
+ Charisma: 15
158
+ Dexterity: 18
159
+ Wisdom: 15
160
+ Compassion: 15
161
+ Courage: 15
162
+ *******************
163
+ '''
164
+
165
+ with gr.Blocks() as app:
166
+ gr.HTML("""<center><h1>Mixtral 8x7B RPG</h1><h3>Role Playing Game Master</h3>""")
167
+ with gr.Group():
168
+ with gr.Row():
169
+ with gr.Column(scale=3):
170
+ chatbot = gr.Chatbot(label="Mixtral 8x7B Game Master",height=500, layout='panel', show_copy_button=True)
171
+ with gr.Row():
172
+ with gr.Column(scale=3):
173
+ opt=gr.Dropdown(label="Choices",choices=["Start a new game"],allow_custom_value=True, value="Start a new game", interactive=True)
174
+ #prompt=gr.Textbox(label = "Prompt", value="Start a new game")
175
+ with gr.Column(scale=2):
176
+ button=gr.Button()
177
+ #models_dd=gr.Dropdown(choices=[m for m in return_list],interactive=True)
178
+ with gr.Row():
179
+ stop_button=gr.Button("Stop")
180
+ clear_btn = gr.Button("Clear")
181
+ with gr.Row():
182
+ tokens = gr.Slider(label="Max new tokens",value=2096,minimum=0,maximum=1048*10,step=64,interactive=False, visible=False,info="The maximum numbers of new tokens")
183
+ with gr.Column(scale=1):
184
+ json_out=gr.JSON(value=base_stats)
185
+ char_stats=gr.Textbox(value=text_stats)
186
+ textboxes = []
187
+ if opts:
188
+ textboxes.clear()
189
+ for i in range(len(opts)-1):
190
+ t = gr.Button(f"{opts[i]}")
191
+ textboxes.append(t)
192
+ #text=gr.JSON()
193
+ #inp_query.change(search_models,inp_query,models_dd)
194
+ #test_b=test_btn.click(itt,url,e_box)
195
+ clear_btn.click(clear_fn,None,[opt,chatbot])
196
+ go=button.click(generate,[opt,chatbot,tokens,char_stats],[chatbot,char_stats,json_out,opt])
197
+ stop_button.click(None,None,None,cancels=[go])
198
+ app.launch(show_api=False)
199
 
200
+
201
+
202
+ '''
203
  examples=[["Start the Game", None, None, None, None, None, ],
204
  ["Start a Game based in the year 1322", None, None, None, None, None,],
205
  ]
 
206
  gr.ChatInterface(
207
  fn=generate,
208
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
 
210
  title="Mixtral RPG Game Master",
211
  examples=examples,
212
  concurrency_limit=20,
213
+ ).launch(share=True,show_api=True)
214
+ '''