Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import random
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Cargar dataset
|
| 7 |
+
with open("dataset.json", "r") as f:
|
| 8 |
+
dataset = json.load(f)
|
| 9 |
+
|
| 10 |
+
# Cargar usuarios
|
| 11 |
+
try:
|
| 12 |
+
with open("users.json", "r") as f:
|
| 13 |
+
users = json.load(f)
|
| 14 |
+
except:
|
| 15 |
+
users = {}
|
| 16 |
+
|
| 17 |
+
def save_users():
|
| 18 |
+
with open("users.json", "w") as f:
|
| 19 |
+
json.dump(users, f, indent=2)
|
| 20 |
+
|
| 21 |
+
def get_or_create_user(user_id):
|
| 22 |
+
if user_id not in users:
|
| 23 |
+
users[user_id] = {
|
| 24 |
+
"nivel": 1,
|
| 25 |
+
"subscription_active": False,
|
| 26 |
+
"relationship_points": 0,
|
| 27 |
+
"sent_items": [],
|
| 28 |
+
"memory": {}
|
| 29 |
+
}
|
| 30 |
+
return users[user_id]
|
| 31 |
+
|
| 32 |
+
def select_content(user, platform):
|
| 33 |
+
eligible = []
|
| 34 |
+
|
| 35 |
+
for item in dataset:
|
| 36 |
+
if not item["active"]:
|
| 37 |
+
continue
|
| 38 |
+
if platform not in item["platforms"]:
|
| 39 |
+
continue
|
| 40 |
+
if not (item["nivel_min"] <= user["nivel"] <= item["nivel_max"]):
|
| 41 |
+
continue
|
| 42 |
+
if item["id"] in user["sent_items"]:
|
| 43 |
+
continue
|
| 44 |
+
|
| 45 |
+
eligible.append(item)
|
| 46 |
+
|
| 47 |
+
if not eligible:
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
selected = random.choice(eligible)
|
| 51 |
+
selected["global_usage_count"] += 1
|
| 52 |
+
user["sent_items"].append(selected["id"])
|
| 53 |
+
user["relationship_points"] += 5
|
| 54 |
+
|
| 55 |
+
if user["relationship_points"] > 50:
|
| 56 |
+
user["nivel"] = min(user["nivel"] + 1, 5)
|
| 57 |
+
|
| 58 |
+
save_users()
|
| 59 |
+
|
| 60 |
+
return selected
|
| 61 |
+
|
| 62 |
+
def chat(user_id, message, platform):
|
| 63 |
+
user = get_or_create_user(user_id)
|
| 64 |
+
|
| 65 |
+
content = select_content(user, platform)
|
| 66 |
+
|
| 67 |
+
response_text = f"Sofía: {message} 💕"
|
| 68 |
+
|
| 69 |
+
if content:
|
| 70 |
+
return response_text, content["url"], content["caption_base"]
|
| 71 |
+
else:
|
| 72 |
+
return response_text, None, None
|
| 73 |
+
|
| 74 |
+
iface = gr.Interface(
|
| 75 |
+
fn=chat,
|
| 76 |
+
inputs=["text", "text", "text"],
|
| 77 |
+
outputs=["text", "text", "text"],
|
| 78 |
+
title="Sofía Core Engine"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
iface.launch()
|