Files changed (1) hide show
  1. app.py +66 -198
app.py CHANGED
@@ -1,217 +1,85 @@
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,
34
- seed=random.randint(1,99999999999)
35
- #seed=42,
36
- )
37
- stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
38
  output = ""
39
- for response in stream:
40
- output += response.token.text
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
52
- top_p = float(top_p)
53
-
54
- generate_kwargs = dict(
55
- temperature=temperature,
56
- max_new_tokens=max_new_tokens,
57
- top_p=top_p,
58
- repetition_penalty=repetition_penalty,
59
- do_sample=True,
60
- seed=random.randint(1,99999999999)
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:
75
- print (l)
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
-
207
- gr.ChatInterface(
208
- fn=generate,
209
- chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
210
- additional_inputs=additional_inputs,
211
- title="Mixtral RPG Game Master",
212
- examples=examples,
213
- concurrency_limit=20,
214
- ).launch(share=True,show_api=True)
215
- '''
216
-
217
-
 
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  import random
 
 
 
 
4
 
5
+ from prompts import GAME_MASTER, COMPRESS_HISTORY, THREE_D_BASE
6
+
7
+ temperature = 0.92
8
+ top_p = 0.95
9
+ repetition_penalty = 1.05
10
+ MAX_HISTORY = 80
11
 
12
+ def format_prompt(message, history):
13
+ prompt = "<s>"
14
+ for user, bot in history or []:
15
+ prompt += f"[INST] {user} [/INST] {bot} </s> "
16
  prompt += f"[INST] {message} [/INST]"
17
  return prompt
18
 
19
+ def compress_history(history_str):
 
 
 
 
 
20
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
21
+ prompt = COMPRESS_HISTORY.format(history=history_str)
 
 
 
 
 
 
 
 
 
 
 
 
22
  output = ""
23
+ for chunk in client.text_generation(prompt, temperature=0.7, max_new_tokens=1024, top_p=0.9, stream=True):
24
+ output += getattr(chunk, 'token', chunk).text
25
  return output
26
 
27
+ def generate(msg, history, max_tokens=1024, stats=None, world_style="Epic Unreal Engine"):
 
 
 
28
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
29
 
30
+ stats = stats or "Health: 100\nCreativity: 95\nVision: 88"
31
+ dice = random.randint(1, 10)
32
+
33
+ gm_text = GAME_MASTER.format(history=history or "New world creation begins...", stats=stats, dice=dice, prompt=msg)
34
+ full_prompt = format_prompt(gm_text, history)
35
+
36
+ kwargs = {
37
+ "temperature": temperature,
38
+ "max_new_tokens": max_tokens,
39
+ "top_p": top_p,
40
+ "repetition_penalty": repetition_penalty,
41
+ "do_sample": True,
42
+ "seed": random.randint(1, 999999999)
43
+ }
44
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  output = ""
46
+ for chunk in client.text_generation(full_prompt, **kwargs, stream=True, details=True, return_full_text=False):
47
+ output += chunk.token.text
48
+ yield [(msg, output)], stats, None
49
+
50
+ # Append 3D prompts
51
+ three_d = f"\n\n=== 3D GENERATION PROMPTS ===\n{THREE_D_BASE.format(subject='main scene', style=world_style, lighting='volumetric god rays', mood='epic cinematic', camera='wide establishing shot')}\n"
52
+ three_d += f"1. Main World: {output[:250].replace(chr(10), ' ')} ...\n"
53
+ three_d += f"2. Key Asset: Character or object from the scene...\n"
54
+
55
+ final = output + three_d
56
+
57
+ if history is None:
58
+ history = []
59
+ history.append((msg, final))
60
+
61
+ yield history, stats, gr.Dropdown(choices=["Continue", "New Location", "Generate Asset"], value="Continue")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  def clear_fn():
64
+ return None, None, None
65
 
66
+ with gr.Blocks(title="3D World Architect RPG") as demo:
67
+ gr.HTML("<center><h1>🌌 3D World Architect RPG</h1><h3>Mixtral 8x7B • Narrative + Production 3D Prompts</h3></center>")
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ with gr.Row():
70
+ with gr.Column(scale=3):
71
+ chatbot = gr.Chatbot(height=650, label="World Narrative + 3D Prompts", show_copy_button=True)
72
+ msg = gr.Textbox(placeholder="Enter your action or describe a new world...", label="Your Command", lines=3)
73
+ with gr.Row():
74
+ send = gr.Button("Send", variant="primary")
75
+ clear = gr.Button("New World")
76
+
77
+ with gr.Column(scale=1):
78
+ stats = gr.Textbox(value="Health: 100\nCreativity: 95\nVision: 88", label="Architect Stats", lines=12)
79
+ style = gr.Dropdown(["Epic Unreal Engine", "Photorealistic", "Stylized Fantasy", "Cyberpunk Neon", "Low Poly"], value="Epic Unreal Engine", label="3D Style")
80
+ tokens = gr.Slider(512, 2048, 1024, label="Max Tokens")
81
+
82
+ send.click(generate, [msg, chatbot, tokens, stats, style], [chatbot, stats, msg])
83
+ clear.click(clear_fn, outputs=[chatbot, msg, stats])
84
+
85
+ demo.launch(show_api=False)