Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,208 @@
|
|
| 1 |
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from google import genai
|
| 4 |
-
import json
|
| 5 |
import time
|
| 6 |
import re
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 10 |
if not GEMINI_API_KEY:
|
| 11 |
-
raise ValueError("
|
| 12 |
|
| 13 |
-
#
|
| 14 |
client = genai.Client()
|
| 15 |
|
| 16 |
-
#
|
| 17 |
AVATAR_IMAGES = {
|
| 18 |
"Agent A (Optimist)": "https://i.imgur.com/2GMV9y7.png",
|
| 19 |
"Agent B (Skeptic)": "https://i.imgur.com/KzX3T21.png",
|
| 20 |
"SYSTEM": "https://i.imgur.com/z3uxLzT.png"
|
| 21 |
}
|
| 22 |
|
| 23 |
-
#
|
| 24 |
cyberpunk_css = """
|
| 25 |
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&display=swap');
|
| 26 |
:root {
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
--bg-color-1: #1a001a;
|
| 31 |
-
--bg-color-2: #0d001a;
|
| 32 |
-
--border-glow: 0 0 5px var(--neon-cyan), 0 0 10px var(--neon-cyan), 0 0 20px var(--neon-pink), 0 0 30px var(--neon-pink);
|
| 33 |
-
}
|
| 34 |
-
.gradio-container {
|
| 35 |
-
background: linear-gradient(45deg, var(--bg-color-1), var(--bg-color-2));
|
| 36 |
-
font-family: 'Orbitron', sans-serif;
|
| 37 |
-
}
|
| 38 |
-
h1, h2, .gr-label {
|
| 39 |
-
color: var(--neon-cyan);
|
| 40 |
-
text-shadow: 0 0 5px var(--neon-cyan), 0 0 10px var(--neon-cyan);
|
| 41 |
-
text-align: center;
|
| 42 |
-
}
|
| 43 |
-
.gr-button {
|
| 44 |
-
background: transparent;
|
| 45 |
-
color: var(--neon-green);
|
| 46 |
-
border: 2px solid var(--neon-green);
|
| 47 |
-
border-radius: 5px;
|
| 48 |
-
box-shadow: 0 0 5px var(--neon-green), 0 0 10px var(--neon-green) inset;
|
| 49 |
-
transition: all 0.3s ease;
|
| 50 |
-
}
|
| 51 |
-
.gr-button:hover {
|
| 52 |
-
background: var(--neon-green);
|
| 53 |
-
color: var(--bg-color-2);
|
| 54 |
-
box-shadow: 0 0 10px var(--neon-green), 0 0 20px var(--neon-green), 0 0 30px var(--neon-green);
|
| 55 |
-
}
|
| 56 |
-
.chatbot {
|
| 57 |
-
background-color: rgba(13, 0, 26, 0.8);
|
| 58 |
-
border: 1px solid var(--neon-pink);
|
| 59 |
-
box-shadow: var(--border-glow);
|
| 60 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
"""
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
def parse_list(
|
| 65 |
-
lines =
|
| 66 |
return [re.sub(r'^\d+[.)]\s*', '', line).strip() for line in lines if line.strip()]
|
| 67 |
|
| 68 |
-
#
|
| 69 |
def generate_topics():
|
| 70 |
-
prompt = "Generate 20 specific, debatable topics
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
def run_debate(topic, question):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
agent_a = client.chats.create(model="gemini-2.5-flash")
|
| 86 |
agent_b = client.chats.create(model="gemini-2.5-flash")
|
| 87 |
|
| 88 |
-
agent_a.send_message("You are Agent A, a tech optimist and futurist.
|
| 89 |
-
agent_b.send_message("You are Agent B, a skeptic and ethicist. Argue cautiously.")
|
| 90 |
-
|
| 91 |
-
history = [
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
if __name__ == "__main__":
|
| 126 |
-
demo.launch(debug=True, ssr_mode=False)
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
import re
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from google import genai
|
| 6 |
|
| 7 |
+
# Load API key
|
| 8 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 9 |
if not GEMINI_API_KEY:
|
| 10 |
+
raise ValueError("❌ GEMINI_API_KEY not found. Please set it in Hugging Face Space Secrets.")
|
| 11 |
|
| 12 |
+
# Initialize Gemini client
|
| 13 |
client = genai.Client()
|
| 14 |
|
| 15 |
+
# Avatars
|
| 16 |
AVATAR_IMAGES = {
|
| 17 |
"Agent A (Optimist)": "https://i.imgur.com/2GMV9y7.png",
|
| 18 |
"Agent B (Skeptic)": "https://i.imgur.com/KzX3T21.png",
|
| 19 |
"SYSTEM": "https://i.imgur.com/z3uxLzT.png"
|
| 20 |
}
|
| 21 |
|
| 22 |
+
# Custom CSS
|
| 23 |
cyberpunk_css = """
|
| 24 |
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&display=swap');
|
| 25 |
:root {
|
| 26 |
+
--neon-pink: #ff00ff; --neon-cyan: #00ffff; --neon-green: #39ff14;
|
| 27 |
+
--bg-color-1: #1a001a; --bg-color-2: #0d001a;
|
| 28 |
+
--border-glow: 0 0 5px var(--neon-cyan), 0 0 10px var(--neon-cyan), 0 0 20px var(--neon-pink), 0 0 30px var(--neon-pink);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
+
.gradio-container { background: linear-gradient(45deg, var(--bg-color-1), var(--bg-color-2)); font-family: 'Orbitron', sans-serif; }
|
| 31 |
+
h1, h2, .gr-label { color: var(--neon-cyan); text-shadow: 0 0 5px var(--neon-cyan), 0 0 10px var(--neon-cyan); text-align: center; }
|
| 32 |
+
.gr-button { background: transparent; color: var(--neon-green); border: 2px solid var(--neon-green); border-radius: 5px; box-shadow: 0 0 5px var(--neon-green), 0 0 10px var(--neon-green) inset; transition: all 0.3s ease; }
|
| 33 |
+
.gr-button:hover { background: var(--neon-green); color: var(--bg-color-2); box-shadow: 0 0 10px var(--neon-green), 0 0 20px var(--neon-green), 0 0 30px var(--neon-green); }
|
| 34 |
+
.chatbot { background-color: rgba(13, 0, 26, 0.8); border: 1px solid var(--neon-pink); box-shadow: var(--border-glow); }
|
| 35 |
+
.gr-dropdown, .gr-radio { border: 1px solid var(--neon-cyan) !important; background-color: var(--bg-color-2) !important; color: var(--neon-cyan) !important; }
|
| 36 |
+
footer { display: none !important; }
|
| 37 |
"""
|
| 38 |
|
| 39 |
+
# Utils
|
| 40 |
+
def parse_list(text_response):
|
| 41 |
+
lines = text_response.strip().split('\n')
|
| 42 |
return [re.sub(r'^\d+[.)]\s*', '', line).strip() for line in lines if line.strip()]
|
| 43 |
|
| 44 |
+
# Topic Generator
|
| 45 |
def generate_topics():
|
| 46 |
+
prompt = "Generate a list of exactly 20 specific, debatable topics related to the future of coding, banking, and artificial intelligence. Format as a numbered list."
|
| 47 |
+
try:
|
| 48 |
+
chat = client.chats.create(model="gemini-2.5-flash")
|
| 49 |
+
response = chat.send_message(prompt)
|
| 50 |
+
topics = parse_list(response.text)
|
| 51 |
+
return {
|
| 52 |
+
topics_state: topics,
|
| 53 |
+
topic_dropdown: gr.Dropdown(choices=topics, value=None, label="Step 2: Select a Topic", visible=True),
|
| 54 |
+
generate_topics_btn: gr.Button(visible=False),
|
| 55 |
+
}
|
| 56 |
+
except Exception as e:
|
| 57 |
+
gr.Warning(f"Error generating topics: {e}")
|
| 58 |
+
return {
|
| 59 |
+
topics_state: [],
|
| 60 |
+
topic_dropdown: gr.Dropdown(visible=False),
|
| 61 |
+
generate_topics_btn: gr.Button(visible=True)
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
# Question Generator
|
| 65 |
+
def generate_questions(selected_topic):
|
| 66 |
+
if not selected_topic:
|
| 67 |
+
return {question_dropdown: gr.Dropdown(visible=False)}
|
| 68 |
+
prompt = f"For the debate topic '{selected_topic}', generate a list of exactly 20 provocative questions to start the debate. Format as a numbered list."
|
| 69 |
+
try:
|
| 70 |
+
chat = client.chats.create(model="gemini-2.5-flash")
|
| 71 |
+
response = chat.send_message(prompt)
|
| 72 |
+
questions = parse_list(response.text)
|
| 73 |
+
return {
|
| 74 |
+
questions_state: questions,
|
| 75 |
+
question_dropdown: gr.Dropdown(choices=questions, value=None, label="Step 3: Select a Starting Question", visible=True)
|
| 76 |
+
}
|
| 77 |
+
except Exception as e:
|
| 78 |
+
gr.Warning(f"Error generating questions: {e}")
|
| 79 |
+
return {
|
| 80 |
+
questions_state: [],
|
| 81 |
+
question_dropdown: gr.Dropdown(visible=False)
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
# Debate Enabler
|
| 85 |
+
def enable_debate_button(selected_question):
|
| 86 |
+
return {start_debate_btn: gr.Button(visible=bool(selected_question))}
|
| 87 |
+
|
| 88 |
+
# Debate Runner
|
| 89 |
def run_debate(topic, question):
|
| 90 |
+
if not topic or not question:
|
| 91 |
+
gr.Warning("Select a topic and question first.")
|
| 92 |
+
return [], []
|
| 93 |
+
|
| 94 |
agent_a = client.chats.create(model="gemini-2.5-flash")
|
| 95 |
agent_b = client.chats.create(model="gemini-2.5-flash")
|
| 96 |
|
| 97 |
+
agent_a.send_message("You are Agent A, a tech optimist and futurist. Defend the topic positively.")
|
| 98 |
+
agent_b.send_message("You are Agent B, a skeptic and ethicist. Argue against the topic cautiously.")
|
| 99 |
+
|
| 100 |
+
history = [{
|
| 101 |
+
"role": "user", "name": "SYSTEM",
|
| 102 |
+
"content": f"**DEBATE TOPIC:** {topic}\n\n**STARTING QUESTION:** {question}",
|
| 103 |
+
"avatar_url": AVATAR_IMAGES["SYSTEM"]
|
| 104 |
+
}]
|
| 105 |
+
yield history, history
|
| 106 |
+
|
| 107 |
+
last_message = question
|
| 108 |
+
|
| 109 |
+
for i in range(5): # 5 rounds = 10 exchanges
|
| 110 |
+
try:
|
| 111 |
+
time.sleep(0.7)
|
| 112 |
+
res_a = agent_a.send_message(f"Based on: '{last_message}', make an optimistic argument.")
|
| 113 |
+
history.append({
|
| 114 |
+
"role": "assistant", "name": "Agent A (Optimist)",
|
| 115 |
+
"content": res_a.text,
|
| 116 |
+
"avatar_url": AVATAR_IMAGES["Agent A (Optimist)"]
|
| 117 |
+
})
|
| 118 |
+
last_message = res_a.text
|
| 119 |
+
yield history, history
|
| 120 |
+
|
| 121 |
+
time.sleep(0.7)
|
| 122 |
+
res_b = agent_b.send_message(f"Respond to Agent A: '{last_message}' with a skeptical argument.")
|
| 123 |
+
history.append({
|
| 124 |
+
"role": "assistant", "name": "Agent B (Skeptic)",
|
| 125 |
+
"content": res_b.text,
|
| 126 |
+
"avatar_url": AVATAR_IMAGES["Agent B (Skeptic)"]
|
| 127 |
+
})
|
| 128 |
+
last_message = res_b.text
|
| 129 |
+
yield history, history
|
| 130 |
+
|
| 131 |
+
except Exception as e:
|
| 132 |
+
history.append({
|
| 133 |
+
"role": "user", "name": "SYSTEM",
|
| 134 |
+
"content": f"⚠️ An error occurred: {e}",
|
| 135 |
+
"avatar_url": AVATAR_IMAGES["SYSTEM"]
|
| 136 |
+
})
|
| 137 |
+
yield history, history
|
| 138 |
+
break
|
| 139 |
+
|
| 140 |
+
history.append({
|
| 141 |
+
"role": "user", "name": "SYSTEM",
|
| 142 |
+
"content": "--- DEBATE CONCLUDED ---",
|
| 143 |
+
"avatar_url": AVATAR_IMAGES["SYSTEM"]
|
| 144 |
+
})
|
| 145 |
+
yield history, history, gr.Row(visible=True)
|
| 146 |
+
|
| 147 |
+
# Reset
|
| 148 |
+
def reset_all():
|
| 149 |
+
return {
|
| 150 |
+
chatbox: [], debate_log_state: [], topics_state: [], questions_state: [],
|
| 151 |
+
generate_topics_btn: gr.Button(visible=True),
|
| 152 |
+
topic_dropdown: gr.Dropdown(value=None, choices=[], visible=False),
|
| 153 |
+
question_dropdown: gr.Dropdown(value=None, choices=[], visible=False),
|
| 154 |
+
start_debate_btn: gr.Button(visible=False),
|
| 155 |
+
save_row: gr.Row(visible=False),
|
| 156 |
+
download_file: None,
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
# UI
|
| 160 |
+
with gr.Blocks(css=cyberpunk_css, title="AI DEBATE SIMULATOR") as demo:
|
| 161 |
+
debate_log_state = gr.State([])
|
| 162 |
+
topics_state = gr.State([])
|
| 163 |
+
questions_state = gr.State([])
|
| 164 |
+
|
| 165 |
+
gr.Markdown("# AI DEBATE SIMULATOR")
|
| 166 |
+
gr.Markdown("## A War of Wits Between Two AI Agents")
|
| 167 |
+
|
| 168 |
+
with gr.Row():
|
| 169 |
+
with gr.Column(scale=1):
|
| 170 |
+
generate_topics_btn = gr.Button("Step 1: Generate 20 Debate Topics", variant="primary")
|
| 171 |
+
topic_dropdown = gr.Dropdown(label="Step 2: Select a Topic", visible=False)
|
| 172 |
+
question_dropdown = gr.Dropdown(label="Step 3: Select a Starting Question", visible=False)
|
| 173 |
+
start_debate_btn = gr.Button("Step 4: START THE DEBATE", visible=False)
|
| 174 |
+
with gr.Row(visible=False) as save_row:
|
| 175 |
+
save_row_button = gr.Button("Download Log")
|
| 176 |
+
download_file = gr.File(label="Download File")
|
| 177 |
+
reset_btn = gr.Button("Start New Debate", variant="stop")
|
| 178 |
+
with gr.Column(scale=3):
|
| 179 |
+
chatbox = gr.Chatbot(label="Debate Arena", height=700, type="messages")
|
| 180 |
+
|
| 181 |
+
# Button logic
|
| 182 |
+
generate_topics_btn.click(fn=generate_topics, inputs=None,
|
| 183 |
+
outputs=[topics_state, topic_dropdown, generate_topics_btn])
|
| 184 |
+
topic_dropdown.change(fn=generate_questions, inputs=topic_dropdown,
|
| 185 |
+
outputs=[questions_state, question_dropdown])
|
| 186 |
+
question_dropdown.change(fn=enable_debate_button, inputs=question_dropdown,
|
| 187 |
+
outputs=[start_debate_btn])
|
| 188 |
+
start_debate_btn.click(
|
| 189 |
+
fn=lambda: {
|
| 190 |
+
generate_topics_btn: gr.Button(visible=False),
|
| 191 |
+
topic_dropdown: gr.Dropdown(interactive=False),
|
| 192 |
+
question_dropdown: gr.Dropdown(interactive=False),
|
| 193 |
+
start_debate_btn: gr.Button(visible=False),
|
| 194 |
+
},
|
| 195 |
+
inputs=None,
|
| 196 |
+
outputs=[generate_topics_btn, topic_dropdown, question_dropdown, start_debate_btn]
|
| 197 |
+
).then(fn=run_debate, inputs=[topic_dropdown, question_dropdown],
|
| 198 |
+
outputs=[chatbox, debate_log_state, save_row])
|
| 199 |
+
|
| 200 |
+
reset_btn.click(fn=reset_all, inputs=None,
|
| 201 |
+
outputs=[chatbox, debate_log_state, topics_state,
|
| 202 |
+
questions_state, generate_topics_btn,
|
| 203 |
+
topic_dropdown, question_dropdown,
|
| 204 |
+
start_debate_btn, save_row, download_file])
|
| 205 |
+
|
| 206 |
+
# Launch app
|
| 207 |
if __name__ == "__main__":
|
| 208 |
+
demo.launch(debug=True, ssr_mode=False)
|