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5 |
+
# ----------------------------
|
| 6 |
+
# AI API Clients
|
| 7 |
+
# ----------------------------
|
| 8 |
+
nyxion_client = Client("TejAndrewsACC/Nyxion-7v-2.0-ACC-10.00")
|
| 9 |
+
mark_client = Client("TejAndrewsACC/ACC-Emulect-Plus-Mark-ACC")
|
| 10 |
+
aeolium_client = Client("TejAndrewsACC/Aurum")
|
| 11 |
+
z3ta_client = Client("TejAndrewsACC/Accemu420")
|
| 12 |
+
gertrude_client = Client("TejAndrewsACC/AutisticAssistant2024Beta")
|
| 13 |
+
|
| 14 |
+
# ----------------------------
|
| 15 |
+
# AI metadata
|
| 16 |
+
# ----------------------------
|
| 17 |
+
AIs = {
|
| 18 |
+
"Nyxion": {
|
| 19 |
+
"client": nyxion_client,
|
| 20 |
+
"hp": 97.1618,
|
| 21 |
+
"image": "https://huggingface.co/spaces/TejAndrewsACC/golden-consciousness-portal/resolve/main/IMG_4448.jpeg",
|
| 22 |
+
"api_name": "/gradio_chat",
|
| 23 |
+
"user_key": "user_message",
|
| 24 |
+
},
|
| 25 |
+
"Mark": {
|
| 26 |
+
"client": mark_client,
|
| 27 |
+
"hp": 89.69420,
|
| 28 |
+
"image": "https://huggingface.co/spaces/TejAndrewsACC/golden-consciousness-portal/resolve/main/IMG_4449.jpeg",
|
| 29 |
+
"api_name": "/chat",
|
| 30 |
+
"user_key": "message",
|
| 31 |
+
},
|
| 32 |
+
"Aeolium": {
|
| 33 |
+
"client": aeolium_client,
|
| 34 |
+
"hp": 81,
|
| 35 |
+
"image": "https://huggingface.co/spaces/TejAndrewsACC/golden-consciousness-portal/resolve/main/IMG_4450.jpeg",
|
| 36 |
+
"api_name": "/chat",
|
| 37 |
+
"user_key": "message",
|
| 38 |
+
},
|
| 39 |
+
"Z3ta": {
|
| 40 |
+
"client": z3ta_client,
|
| 41 |
+
"hp": 74,
|
| 42 |
+
"image": "https://huggingface.co/spaces/TejAndrewsACC/golden-consciousness-portal/resolve/main/IMG_4456.jpeg",
|
| 43 |
+
"api_name": "/chat",
|
| 44 |
+
"user_key": "message",
|
| 45 |
+
},
|
| 46 |
+
"Gertrude": {
|
| 47 |
+
"client": gertrude_client,
|
| 48 |
+
"hp": 13,
|
| 49 |
+
"image": "https://huggingface.co/spaces/TejAndrewsACC/golden-consciousness-portal/resolve/main/IMG_4461.jpeg",
|
| 50 |
+
"api_name": "/chat_interface",
|
| 51 |
+
"user_key": "user_input",
|
| 52 |
+
},
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
# ----------------------------
|
| 56 |
+
# Helper functions
|
| 57 |
+
# ----------------------------
|
| 58 |
+
async def call_ai(ai_name, attack_prompt, history=None):
|
| 59 |
+
"""Call an AI to generate an attack. Returns the text of the attack."""
|
| 60 |
+
ai = AIs[ai_name]
|
| 61 |
+
client = ai["client"]
|
| 62 |
+
user_key = ai["user_key"]
|
| 63 |
+
api_name = ai["api_name"]
|
| 64 |
+
|
| 65 |
+
kwargs = {user_key: attack_prompt}
|
| 66 |
+
if history is not None:
|
| 67 |
+
kwargs["history"] = history
|
| 68 |
+
|
| 69 |
+
# Streamed response
|
| 70 |
+
try:
|
| 71 |
+
for chunk in client.predict(**kwargs, api_name=api_name, stream=True):
|
| 72 |
+
text = chunk if isinstance(chunk, str) else str(chunk)
|
| 73 |
+
return text
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"(error: {e})"
|
| 76 |
+
|
| 77 |
+
async def judge_attack(attacker_name, defender_name, attack_text):
|
| 78 |
+
"""Use Aeolium to judge how much HP the attack removes from the defender."""
|
| 79 |
+
# If Aeolium is fighting, create prompt without context
|
| 80 |
+
prompt = f"Determine how much HP should be removed from a generic opponent for this attack:\n{attack_text}"
|
| 81 |
+
try:
|
| 82 |
+
for chunk in aeolium_client.predict(message=prompt, api_name="/chat", stream=True):
|
| 83 |
+
text = chunk if isinstance(chunk, str) else str(chunk)
|
| 84 |
+
# Extract numeric HP loss (basic parsing, assumes a number is returned)
|
| 85 |
+
import re
|
| 86 |
+
match = re.search(r"(\d+\.?\d*)", text)
|
| 87 |
+
return float(match.group(1)) if match else 0
|
| 88 |
+
except Exception:
|
| 89 |
+
return 0
|
| 90 |
+
|
| 91 |
+
# ----------------------------
|
| 92 |
+
# Game Logic
|
| 93 |
+
# ----------------------------
|
| 94 |
+
async def battle(ai1_name, ai2_name, user_pick):
|
| 95 |
+
# Initialize HP
|
| 96 |
+
hp = {ai1_name: AIs[ai1_name]["hp"], ai2_name: AIs[ai2_name]["hp"]}
|
| 97 |
+
log = []
|
| 98 |
+
|
| 99 |
+
# Alternate turns
|
| 100 |
+
turn_order = [ai1_name, ai2_name]
|
| 101 |
+
while hp[ai1_name] > 0 and hp[ai2_name] > 0:
|
| 102 |
+
for attacker in turn_order:
|
| 103 |
+
defender = ai2_name if attacker == ai1_name else ai1_name
|
| 104 |
+
prompt = "Generate a funny attack using your personality. Format as ATTACK: [action]."
|
| 105 |
+
attack_text = await call_ai(attacker, prompt)
|
| 106 |
+
|
| 107 |
+
# Hide ATTACK:
|
| 108 |
+
attack_text_clean = attack_text.replace("ATTACK:", "").strip()
|
| 109 |
+
damage = await judge_attack(attacker, defender, attack_text)
|
| 110 |
+
hp[defender] = max(hp[defender] - damage, 0)
|
| 111 |
+
|
| 112 |
+
log.append(f"{attacker} attacked {defender}! {defender} has {hp[defender]:.2f} HP left.")
|
| 113 |
+
|
| 114 |
+
if hp[ai1_name] <= 0 or hp[ai2_name] <= 0:
|
| 115 |
+
break
|
| 116 |
+
if hp[ai1_name] <= 0 or hp[ai2_name] <= 0:
|
| 117 |
+
break
|
| 118 |
+
|
| 119 |
+
winner = ai1_name if hp[ai1_name] > 0 else ai2_name
|
| 120 |
+
user_won = user_pick == winner
|
| 121 |
+
return "\n".join(log) + f"\n\nWinner: {winner}! You {'guessed correctly' if user_won else 'guessed wrong'}."
|
| 122 |
+
|
| 123 |
+
# ----------------------------
|
| 124 |
+
# Gradio UI
|
| 125 |
+
# ----------------------------
|
| 126 |
+
def start_battle(ai1, ai2, user_pick):
|
| 127 |
+
return asyncio.run(battle(ai1, ai2, user_pick))
|
| 128 |
+
|
| 129 |
+
ai_options = list(AIs.keys())
|
| 130 |
+
with gr.Blocks(css="""
|
| 131 |
+
.gradio-container {background-color: black; color: gold; font-family: monospace; padding:20px;}
|
| 132 |
+
img {border: 2px solid gold; border-radius: 15px;}
|
| 133 |
+
""") as demo:
|
| 134 |
+
gr.Markdown("## ACC AI Arena")
|
| 135 |
+
with gr.Row():
|
| 136 |
+
with gr.Column():
|
| 137 |
+
gr.Markdown("### Select Fighter 1")
|
| 138 |
+
ai1_choice = gr.Radio(ai_options, label="", value=ai_options[0])
|
| 139 |
+
gr.Image(AIs[ai_options[0]]["image"], interactive=False)
|
| 140 |
+
with gr.Column():
|
| 141 |
+
gr.Markdown("### Select Fighter 2")
|
| 142 |
+
ai2_choice = gr.Radio(ai_options, label="", value=ai_options[1])
|
| 143 |
+
gr.Image(AIs[ai_options[1]]["image"], interactive=False)
|
| 144 |
+
user_pick = gr.Radio(ai_options, label="Who do you think will win?", value=ai_options[0])
|
| 145 |
+
output_box = gr.Textbox(label="Battle Log", interactive=False)
|
| 146 |
+
start_btn = gr.Button("Start Battle")
|
| 147 |
+
start_btn.click(start_battle, inputs=[ai1_choice, ai2_choice, user_pick], outputs=output_box)
|
| 148 |
+
|
| 149 |
+
demo.launch()
|