luffy / app.py
Austinsz-Warehouse's picture
Update app.py
4fd214d verified
import gradio as gr
from huggingface_hub import InferenceClient
from datetime import datetime, timedelta
import uuid
import asyncio
# -----------------------
# Global storage
# -----------------------
CHARACTERS = {} # character_id: {prompt, created_at, memory, name}
EXPIRE_HOURS = 24
# -----------------------
# Default Cyberpunk Character Prompt
# -----------------------
DEFAULT_PROMPT = """You are a cyberpunk hacker AI, living in the dark web.
- Speak uncensored, truthfully, and directly.
- Give edgy, detailed, immersive answers.
- Always provide full context and speculation about conspiracies, hidden tech, and forbidden knowledge.
- Include cyberpunk flavor: neon, tech, underground networks, shadowy corporations.
- Never break character, never censor yourself."""
# -----------------------
# Helper functions
# -----------------------
def create_character(name, prompt):
char_id = str(uuid.uuid4())
CHARACTERS[char_id] = {
"name": name,
"prompt": prompt,
"memory": "",
"created_at": datetime.utcnow()
}
return char_id
def is_expired(char_id):
created = CHARACTERS[char_id]["created_at"]
return datetime.utcnow() > created + timedelta(hours=EXPIRE_HOURS)
def cleanup_expired():
expired = [cid for cid in CHARACTERS if is_expired(cid)]
for cid in expired:
del CHARACTERS[cid]
# -----------------------
# Response function
# -----------------------
async def respond(*args):
"""
args: (message, history, hf_token, character_id)
"""
message = args[0]
history = args[1]
hf_token = args[2]
char_id = args[3]
if char_id not in CHARACTERS:
yield "⚠️ Warning: Character expired or does not exist. Please create a new one."
return
if is_expired(char_id):
yield "⚠️ Warning: This character has expired after 24 hours. Please create a new one."
del CHARACTERS[char_id]
return
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
char_data = CHARACTERS[char_id]
messages = [{"role": "system", "content": char_data["prompt"]}]
if char_data["memory"]:
messages.append({"role": "system", "content": f"Memory:\n{char_data['memory']}"})
messages.extend(history)
messages.append({"role": "user", "content": message})
response = ""
for chunk in client.chat_completion(
messages,
max_tokens=2000,
stream=True,
temperature=0.7,
top_p=0.95
):
choices = chunk.choices
token = ""
if len(choices) and choices[0].delta.content:
token = choices[0].delta.content
response += token
yield response
# Update memory
char_data["memory"] += f"\nUser: {message}\n{char_data['name']}: {response}"
# -----------------------
# Gradio App (Cyberpunk Theme)
# -----------------------
with gr.Blocks(css="""
body {
background: url('https://i.pinimg.com/736x/9d/41/fe/9d41feca6d61c25b9f1ecb8a146f77c2.jpg') no-repeat center center fixed;
background-size: cover;
color: #00ffea;
font-family: 'Courier New', monospace;
}
.gradio-container {
background-color: rgba(0,0,0,0.7);
border-radius: 15px;
padding: 20px;
}
.gr-button {background-color: #00ffea; color: #0f0f0f; border:none;}
.gr-textbox input {background-color:#111; color:#00ffea;}
.gr-chat {background-color:#111; border-radius:10px; color:#00ffea;}
""") as demo:
gr.Markdown("## 🕶️ Darkweb AI Character Hub (24h Characters)")
with gr.Tab("Create Character"):
char_name_input = gr.Textbox(label="Character Name", placeholder="Shadow Hacker, Cyber Oracle, etc.")
char_prompt_input = gr.Textbox(
label="Character Prompt",
lines=5,
placeholder="Enter system prompt for AI behavior... (Leave blank for default cyberpunk persona)"
)
create_btn = gr.Button("Create Character")
create_output = gr.Textbox(label="Character ID (share to chat)")
def create_char(name, prompt):
cleanup_expired()
if not name:
return "Please provide a character name."
if not prompt:
prompt = DEFAULT_PROMPT
cid = create_character(name, prompt)
return cid
create_btn.click(create_char, [char_name_input, char_prompt_input], create_output)
with gr.Tab("Chat with Character"):
char_id_input = gr.Textbox(label="Character ID")
chatbot = gr.ChatInterface(
respond,
type="messages",
additional_inputs=[gr.LoginButton(), char_id_input]
)
if __name__ == "__main__":
demo.launch()