import gradio as gr import json import os import time from datetime import datetime, timedelta DB_PATH = "database.json" # Load or initialize database if not os.path.exists(DB_PATH): with open(DB_PATH, "w") as f: json.dump({"users": {}, "stories": []}, f) def load_db(): with open(DB_PATH, "r") as f: return json.load(f) def save_db(db): with open(DB_PATH, "w") as f: json.dump(db, f, indent=2) def get_time_now(): return int(time.time()) def create_user(username): db = load_db() if username not in db["users"]: db["users"][username] = { "gems": 10000, "last_gem_refresh": get_time_now(), "stories": [] } save_db(db) def login(username): create_user(username) db = load_db() user = db["users"][username] now = get_time_now() last = user["last_gem_refresh"] if now - last >= 86400: user["gems"] += 10000 user["last_gem_refresh"] = now save_db(db) return f"Welcome, {username}! You have {user['gems']} gems.", gr.update(visible=True), gr.update(visible=True), username def save_story(username, title, scenes_json): db = load_db() try: scenes = json.loads(scenes_json) except: return "Invalid scene JSON!" story = { "title": title, "author": username, "scenes": scenes } db["stories"].append(story) db["users"][username]["stories"].append(title) save_db(db) return f"Story '{title}' saved and published!" def list_stories(): db = load_db() return [f"{s['title']} by {s['author']}" for s in db["stories"]] def read_story(title, username): db = load_db() for s in db["stories"]: if f"{s['title']} by {s['author']}" == title: return play_story(s, username) return "Story not found." def play_story(story, username): db = load_db() user = db["users"][username] gems = user["gems"] output = "" for idx, scene in enumerate(story["scenes"]): output += f"\nScene {idx + 1}: {scene['text']}\nChoices:\n" for i, choice in enumerate(scene["choices"]): if choice.get("premium"): output += f" {i + 1}. {choice['text']} (Premium: {choice['cost']} gems)\n" else: output += f" {i + 1}. {choice['text']}\n" return output def deduct_gems(username, amount): db = load_db() db["users"][username]["gems"] -= amount save_db(db) with gr.Blocks() as app: gr.Markdown("# **AI Interactive Story App (Episode Style)**") username_input = gr.Textbox(label="Enter your username") login_btn = gr.Button("Login") login_msg = gr.Markdown() main_ui = gr.Row(visible=False) with main_ui: with gr.Column(): gr.Markdown("### Create and Publish Story") story_title = gr.Textbox(label="Story Title") story_scenes = gr.Textbox(label="Scenes (JSON format)", lines=10, placeholder=""" [ { "text": "You see someone in the hallway.", "choices": [ {"text": "Follow them", "premium": false}, {"text": "Ignore them", "premium": false} ] }, { "text": "They lead you to a rooftop party.", "choices": [ {"text": "Talk to the host", "premium": true, "cost": 3000}, {"text": "Leave quietly", "premium": false} ] } ] """) publish_btn = gr.Button("Publish Story") publish_output = gr.Textbox(label="Publish Status") with gr.Column(): gr.Markdown("### Browse and Read Stories") story_list = gr.Dropdown(choices=list_stories(), label="Select a Story") refresh_btn = gr.Button("Refresh Stories") read_btn = gr.Button("Read Selected Story") story_output = gr.Textbox(label="Story Content", lines=15) hidden_user = gr.Textbox(visible=False) login_btn.click( login, inputs=username_input, outputs=[login_msg, main_ui, main_ui, hidden_user] ) publish_btn.click( save_story, inputs=[hidden_user, story_title, story_scenes], outputs=publish_output ) refresh_btn.click( lambda: gr.update(choices=list_stories()), inputs=None, outputs=story_list ) read_btn.click( read_story, inputs=[story_list, hidden_user], outputs=story_output ) app.launch()