import json import random from datetime import datetime import gradio as gr # ---- Cargar dataset ---- with open("dataset.json", "r") as f: dataset = json.load(f) # ---- Cargar usuarios ---- try: with open("users.json", "r") as f: users = json.load(f) except: users = {} def save_users(): with open("users.json", "w") as f: json.dump(users, f, indent=2) def get_or_create_user(user_id): if user_id not in users: users[user_id] = { "nivel": 1, "subscription_active": False, "relationship_points": 0, "sent_items": [], "memory": {} } save_users() return users[user_id] def select_content(user, platform): eligible = [] for item in dataset: if not item["active"]: continue if platform not in item["platforms"]: continue if not (item["nivel_min"] <= user["nivel"] <= item["nivel_max"]): continue if item["id"] in user["sent_items"]: continue eligible.append(item) if not eligible: return None selected = random.choice(eligible) user["sent_items"].append(selected["id"]) user["relationship_points"] += 5 if user["relationship_points"] >= 50: user["nivel"] = min(user["nivel"] + 1, 5) user["relationship_points"] = 0 save_users() return selected def chat(user_id, message, platform): user = get_or_create_user(user_id) content = select_content(user, platform) response = f"Sofía: Me encanta hablar contigo 💕" if content: return response, content["url"], content["caption_base"] else: return response, None, None iface = gr.Interface( fn=chat, inputs=[ gr.Textbox(label="User ID"), gr.Textbox(label="Mensaje"), gr.Textbox(label="Platform") ], outputs=[ gr.Textbox(label="Respuesta"), gr.Textbox(label="Media URL"), gr.Textbox(label="Caption") ], title="Sofía Core Engine" ) iface.launch()