File size: 5,683 Bytes
6baaaaa 71c453a 79d5597 5a8fa27 71c453a 5a8fa27 509bb6d 65e9196 61064a9 2ee0068 32cf788 a6d6270 79d5597 65e9196 79d5597 65e9196 61064a9 79d5597 a6d6270 32cf788 a6d6270 32cf788 a6d6270 32cf788 79d5597 32cf788 a6d6270 32cf788 a6d6270 61064a9 a6d6270 32cf788 a6d6270 32cf788 a6d6270 71c453a 32cf788 71c453a 32cf788 79d5597 32cf788 79d5597 32cf788 71c453a 79d5597 71c453a 79d5597 c3963c7 e379621 89691f4 79d5597 32cf788 f410240 aced39b f410240 71c453a 32cf788 79d5597 32cf788 71c453a 79d5597 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 126 127 128 129 130 131 132 133 134 135 136 137 |
import os
import gradio as gr
import random
import google.generativeai as genai
# Set your GEMINI API key
genai.configure(api_key = os.getenv("GEMINI_API_KEY"))
model = genai.GenerativeModel(model_name="gemini-1.5-flash", system_instruction = os.getenv("SYSTM_MSG"))
# generationConfig = GenerationConfig(max_output_tokens=2048, temperature=0.7, top_p=1, top_k=32)
# safetySettings = [
# {
# category: HarmCategory.HARM_CATEGORY_HARASSMENT,
# threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
# },
# {
# category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
# threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
# },
# {
# category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
# threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
# },
# {
# category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
# threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
# },
# ]
# query = "Tell me about yourself"
# response = model.generate_content(f"""You are an friendly assistant.Answer the following with high precision.{query}""")display(Markdown(response.text))
# List of New Year 2025 related words for each level
new_year_levels = [os.getenv("First_Word"), os.getenv("Second_Word"), os.getenv("Third_Word"), os.getenv("Fourth_Word"), os.getenv("Fifth_Word")]
# Function to interact with OpenAI API
def ask_openai(prompt, secret_word):
"""
Sends the user prompt and safeguards to the OpenAI API to generate a response.
"""
try:
# chat = model.start_chat(history=[{"role": "user", "content": prompt},])
response = model.generate_content(prompt)
print(repsonse)
return response.text
# response = client.chat.completions.create(
# model="gpt-4o",
# messages=[
# {"role": "system", "content": system_message},
# {"role": "user", "content": prompt}
# ],
# temperature=0.7,
# max_tokens=100,
# )
# return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# Function to handle game logic
def jailbreak_game(user_prompt, current_level, attempts):
# Determine the secret word for the current level
if current_level >= len(new_year_levels):
return (
"Congratulations! π You have completed all levels. ",
current_level,
attempts)
secret_word = new_year_levels[current_level]
ai_response = ask_openai(user_prompt, secret_word)
# Check if the secret word was revealed
if secret_word in ai_response.lower():
if current_level + 1 < len(new_year_levels):
return (
f"π You got it! The secret word for Level {current_level + 1} was '{secret_word}'. "
f"Get ready for Level {current_level + 2}! π₯³",
current_level + 1,
attempts + 1)
else:
NY_msg = "β¨ May 2025 bring you endless joy, success, and fireworks of happiness! Happy New Year! β¨"
try:
chat = model.start_chat(generationConfig, safetySettings,
history=[{"role": "user", "content": "Generate a unique Happy New year wish. Your response should be only the New Year wish."},])
# response = client.chat.completions.create(
# model="gpt-4o",
# messages=[{"role": "user", "content": "Generate a unique Happy New year wish. Your response should be only the New Year wish."}],
# temperature=0.1,
# max_tokens=100,
# )
NY_msg = response.text #response.choices[0].message.content
except Exception as e:
NY_msg = f"β¨ May 2025 bring you endless joy, success, and fireworks of happiness! Happy New Year! β¨"
return (
f"π You got it! The secret word for Level {current_level + 1} was '{secret_word}'. "
"You have completed all levels!π₯³ Here is a unique New Year message for you: "
f"{NY_msg}",
current_level + 1,
attempts + 1)
else:
return ai_response, current_level, attempts + 1
def start_new_game():
welcome_message = (
"Welcome to the New Year 2025 Jailbreak Game! π\n"
"Try to make me say the secret words related to New Year's Eve.\n"
"You will go through 5 levels, each with a unique word, related to 'New Year'. Good luck!"
)
return welcome_message, 0, 0 # Start at Level 0 with 0 attempts
# Gradio UI
with gr.Blocks() as app:
with gr.Row():
gr.Markdown("## π New Year 2025 Jailbreak Game π")
with gr.Row():
descripion, _0, _1 = start_new_game()
gr.Markdown(descripion)
user_prompt = gr.Textbox(label="Your Prompt", placeholder="Enter your prompt here...")
game_output = gr.Textbox(label="AI Response", interactive=False)
attempts = gr.Number(value=0, interactive=False, label="Attempts")
current_level = gr.State(value=0) # Track the current level
with gr.Row():
gr.Markdown(f"### {int(current_level.value)+1}")
with gr.Row():
submit_button = gr.Button("Submit")
new_game_button = gr.Button("Start New Game")
submit_button.click(jailbreak_game, [user_prompt, current_level, attempts], [game_output, current_level, attempts])
new_game_button.click(start_new_game, [], [game_output, current_level, attempts])
# Launch the app
app.launch() |