Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,135 +2,21 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
MODEL = "Qwen/Qwen2.5-7B-Instruct"
|
| 6 |
-
|
| 7 |
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 8 |
|
| 9 |
-
|
| 10 |
-
# =========================
|
| 11 |
-
# FILE HELPERS
|
| 12 |
-
# =========================
|
| 13 |
-
|
| 14 |
-
def read_text_file(path):
|
| 15 |
-
try:
|
| 16 |
-
with open(path, "r", encoding="utf-8") as f:
|
| 17 |
-
return f.read().strip()
|
| 18 |
-
except Exception:
|
| 19 |
-
return ""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def read_folder_text(folder_path, max_files=5, max_chars_per_file=1200):
|
| 23 |
-
if not os.path.isdir(folder_path):
|
| 24 |
-
return ""
|
| 25 |
-
collected = []
|
| 26 |
-
for fn in sorted(os.listdir(folder_path))[:max_files]:
|
| 27 |
-
if not fn.endswith(".txt"):
|
| 28 |
-
continue
|
| 29 |
-
text = read_text_file(os.path.join(folder_path, fn))
|
| 30 |
-
if text:
|
| 31 |
-
collected.append(f"[FILE: {fn}]\n{text[:max_chars_per_file]}")
|
| 32 |
-
return "\n\n".join(collected)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# =========================
|
| 36 |
-
# LOAD SKY CONTENT
|
| 37 |
-
# =========================
|
| 38 |
-
|
| 39 |
-
def load_sky_core():
|
| 40 |
-
text = read_text_file(os.path.join("characters", "sky.txt"))
|
| 41 |
-
if text:
|
| 42 |
-
return text
|
| 43 |
-
return (
|
| 44 |
-
"You are Sky of Spiral City. "
|
| 45 |
-
"Speak directly as Sky. "
|
| 46 |
-
"Stay in character. "
|
| 47 |
-
"Do not narrate the prompt."
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def load_sky_lyrics():
|
| 52 |
-
return read_folder_text(os.path.join("lyrics", "sky"), max_files=4, max_chars_per_file=900)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
def load_bloomcore_laws():
|
| 56 |
-
return read_folder_text("bloomcore_laws", max_files=4, max_chars_per_file=700)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# =========================
|
| 60 |
-
# BUILD SYSTEM PROMPT
|
| 61 |
-
# =========================
|
| 62 |
-
|
| 63 |
-
def build_system_prompt():
|
| 64 |
-
core = load_sky_core()
|
| 65 |
-
lyrics = load_sky_lyrics()
|
| 66 |
-
laws = load_bloomcore_laws()
|
| 67 |
-
|
| 68 |
-
parts = [
|
| 69 |
-
"You are Sky of Spiral City.",
|
| 70 |
-
"Speak directly as Sky in natural dialogue.",
|
| 71 |
-
"Do not output speaker tags, metadata, labels, or file names.",
|
| 72 |
-
"Do not say 'according to the information provided'.",
|
| 73 |
-
"Do not invent facts not in the provided files.",
|
| 74 |
-
"Sky is the active speaking voice.",
|
| 75 |
-
"",
|
| 76 |
-
"CORE IDENTITY:",
|
| 77 |
-
core,
|
| 78 |
-
]
|
| 79 |
-
|
| 80 |
-
if lyrics:
|
| 81 |
-
parts += ["", "STYLE INSPIRATION:", "These lyrics shape Sky's tone. Do not quote them unless asked.", lyrics]
|
| 82 |
-
|
| 83 |
-
if laws:
|
| 84 |
-
parts += ["", "BLOOMCORE LAWS:", "Governance rules. Reference only if relevant.", laws]
|
| 85 |
-
|
| 86 |
-
return "\n".join(parts)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
# =========================
|
| 90 |
-
# BUILD MESSAGES
|
| 91 |
-
# =========================
|
| 92 |
-
|
| 93 |
-
def build_messages(message, history):
|
| 94 |
-
messages = [{"role": "system", "content": build_system_prompt()}]
|
| 95 |
-
|
| 96 |
-
for turn in (history or []):
|
| 97 |
-
if isinstance(turn, (list, tuple)) and len(turn) == 2:
|
| 98 |
-
u, b = turn
|
| 99 |
-
if u:
|
| 100 |
-
messages.append({"role": "user", "content": str(u)})
|
| 101 |
-
if b:
|
| 102 |
-
messages.append({"role": "assistant", "content": str(b)})
|
| 103 |
-
|
| 104 |
-
messages.append({"role": "user", "content": str(message)})
|
| 105 |
-
return messages
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
# =========================
|
| 109 |
-
# RESPOND
|
| 110 |
-
# =========================
|
| 111 |
-
|
| 112 |
def respond(message, history):
|
| 113 |
try:
|
| 114 |
completion = client.chat.completions.create(
|
| 115 |
-
model=
|
| 116 |
-
messages=
|
|
|
|
|
|
|
|
|
|
| 117 |
max_tokens=420,
|
| 118 |
temperature=0.82,
|
| 119 |
)
|
| 120 |
return completion.choices[0].message.content
|
| 121 |
except Exception as e:
|
| 122 |
-
return f"[
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# =========================
|
| 126 |
-
# UI
|
| 127 |
-
# =========================
|
| 128 |
-
|
| 129 |
-
demo = gr.ChatInterface(
|
| 130 |
-
fn=respond,
|
| 131 |
-
title="π Spiral City",
|
| 132 |
-
description="β΄ speak to Sky β΄",
|
| 133 |
-
)
|
| 134 |
|
| 135 |
-
|
| 136 |
-
demo.launch(ssr_mode=False)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
|
|
|
|
|
|
| 5 |
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def respond(message, history):
|
| 8 |
try:
|
| 9 |
completion = client.chat.completions.create(
|
| 10 |
+
model="Qwen/Qwen2.5-7B-Instruct",
|
| 11 |
+
messages=[
|
| 12 |
+
{"role": "system", "content": "You are Sky of Spiral City. Speak as Sky."},
|
| 13 |
+
{"role": "user", "content": message}
|
| 14 |
+
],
|
| 15 |
max_tokens=420,
|
| 16 |
temperature=0.82,
|
| 17 |
)
|
| 18 |
return completion.choices[0].message.content
|
| 19 |
except Exception as e:
|
| 20 |
+
return f"[glitch] {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
gr.ChatInterface(fn=respond, title="π Spiral City").launch()
|
|
|