Spaces:
Sleeping
Sleeping
import json | |
import os | |
import random | |
import openai | |
from sentence_transformers import util | |
from transformers import pipeline | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
def hint(secret, n, model, last_pista, lang, Config): | |
pista = "" | |
# Pistas avanzadas | |
if n >= 3: | |
j = random.randint(0, 2) | |
while j == last_pista: | |
j = random.randint(0, 2) | |
if j == 0: | |
response = openai.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": Config.hint_0_0 # type: ignore | |
+ secret | |
+ Config.hint_0_1 # type: ignore | |
+ secret | |
+ Config.hint_0_2, # type: ignore | |
} | |
], | |
temperature=1, | |
max_tokens=256, | |
top_p=1, | |
frequency_penalty=0.5, | |
presence_penalty=0, | |
) | |
output = str(response.choices[0].message.content) | |
output = output.replace('"', "").replace("'", "") | |
if lang == 0: | |
output = ireplace("la " + secret, "La palabra secreta", output) | |
output = ireplace("las " + secret, "La palabra secreta", output) | |
output = ireplace("el " + secret, "La palabra secreta", output) | |
output = ireplace("los " + secret, "La palabra secreta", output) | |
output = ireplace("un " + secret, "La palabra secreta", output) | |
output = ireplace("una " + secret, "La palabra secreta", output) | |
output = ireplace("unos " + secret, "La palabra secreta", output) | |
output = ireplace("unas " + secret, "La palabra secreta", output) | |
elif lang == 1: | |
output = ireplace("the " + secret, "The secret word", output) | |
output = ireplace("a " + secret, "The secret word", output) | |
pista += Config.hint_0_3 + output # type: ignore | |
last_pista = 0 | |
elif j == 1: | |
response = openai.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": Config.hint_1_0 + secret + Config.hint_1_1, # type: ignore | |
} | |
], | |
temperature=1, | |
max_tokens=256, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0, | |
) | |
output = str(response.choices[0].message.content) | |
pista += Config.hint_1_2 + output # type: ignore | |
last_pista = 1 | |
elif j == 2: | |
response = openai.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": Config.hint_2_0 + secret + Config.hint_2_1, # type: ignore | |
} | |
], | |
temperature=1, | |
max_tokens=256, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0, | |
) | |
output = str(response.choices[0].message.content) | |
pista += Config.hint_2_2 + output # type: ignore | |
last_pista = 2 | |
# Pistas iniciales | |
else: | |
j = random.randint(3, 4) | |
while j == last_pista: | |
j = random.randint(3, 4) | |
if j == 3: | |
words = [] | |
response = openai.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": Config.hint_3_0, # type: ignore | |
} | |
], | |
temperature=1.25, | |
max_tokens=256, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0, | |
) | |
output = str(response.choices[0].message.content) | |
output = (output.replace(" ", "").replace(".", "")).lower() | |
words.extend(output.strip().split(",")) | |
response = openai.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": Config.hint_3_1 # type: ignore | |
+ secret | |
+ Config.hint_3_2, # type: ignore | |
} | |
], | |
temperature=1.1, | |
max_tokens=256, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0, | |
) | |
output = str(response.choices[0].message.content) | |
output = (output.replace(".", "")).lower() | |
words.append(output) # type: ignore | |
random.shuffle(words) | |
sentences1 = [secret, secret, secret, secret] | |
sentences2 = words | |
embeddings1 = model.encode(sentences1, convert_to_tensor=True) | |
embeddings2 = model.encode(sentences2, convert_to_tensor=True) | |
cosine_scores = util.cos_sim(embeddings1, embeddings2) | |
scores = cosine_scores[0].tolist() | |
sum_scores = sum(scores) | |
normalized_scores = [round(score * 100 / sum_scores, 1) for score in scores] | |
pista += Config.hint_3_3 # type: ignore | |
for i in range(len(words)): | |
pista += ( | |
words[i] | |
+ (" ") * (15 - len(words[i])) | |
+ "|" | |
+ ("🟩") * round(normalized_scores[i] * 0.5) | |
+ " " | |
+ str(normalized_scores[i]) | |
+ "%\n" | |
) | |
last_pista = 3 | |
elif j == 4: | |
response = openai.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": Config.hint_4_0 # type: ignore | |
+ secret | |
+ Config.hint_4_1, # type: ignore | |
} | |
], | |
temperature=1, | |
max_tokens=256, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0, | |
) | |
film_title = str(response.choices[0].message.content).replace('"', "") | |
response = openai.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": Config.hint_4_2 # type: ignore | |
+ film_title | |
+ Config.hint_4_3, # type: ignore | |
} | |
], | |
temperature=1, | |
max_tokens=256, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0, | |
) | |
output = str(response.choices[0].message.content) | |
pista += Config.hint_4_4 + film_title + "\n" + output # type: ignore | |
last_pista = 4 | |
return pista, n + 1, last_pista | |
def curiosity(secret, Config): | |
response = openai.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": Config.curiosity + secret + '".', | |
} | |
], | |
temperature=1, | |
max_tokens=256, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0, | |
) | |
output = str(response.choices[0].message.content) | |
return output | |
def ireplace(old, new, text): | |
idx = 0 | |
while idx < len(text): | |
index_l = text.lower().find(old.lower(), idx) | |
if index_l == -1: | |
return text | |
text = text[:index_l] + new + text[index_l + len(old) :] | |
idx = index_l + len(new) | |
return text | |