Spaces:
Running
Running
Felipe Maya Muniz
commited on
Commit
·
2c75f8e
1
Parent(s):
53ad013
files 2
Browse files- interface_sage_gradio.py +107 -0
interface_sage_gradio.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# --- interface_sage_gradio.py ---
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import replicate
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import os
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
# === Configurações ===
|
| 11 |
+
replicate_client = replicate.Client(api_token=os.environ["REPLICATE_API_TOKEN"])
|
| 12 |
+
EMBEDDING_MODEL = SentenceTransformer("all-MiniLM-L6-v2")
|
| 13 |
+
GPT = pipeline("text-generation", model="gpt2-large")
|
| 14 |
+
|
| 15 |
+
MODEL = "gnai-creator/sage-two:f236bf1fc94263e266db57a32ea4014aef91c0ca6a34ac0e98ba1b0e83ca09af"
|
| 16 |
+
RESET_PASSWORD = os.environ.get("PASSWORD", "")
|
| 17 |
+
|
| 18 |
+
JUDGMENT_MAP = {
|
| 19 |
+
"reflection": [0, 1, 2],
|
| 20 |
+
"anger": [3, 4],
|
| 21 |
+
"hope": [5, 6],
|
| 22 |
+
"denial": [7],
|
| 23 |
+
"intuition": [10, 11],
|
| 24 |
+
"skepticism": [15],
|
| 25 |
+
"acceptance": [20],
|
| 26 |
+
"despair": [30],
|
| 27 |
+
"justice": [40, 41, 42],
|
| 28 |
+
"transcendence": [60, 61, 62]
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
PROMPTS = {
|
| 32 |
+
"reflection": "As an ancient sage before eternity, poetically reflect on:",
|
| 33 |
+
"anger": "With restrained fury and sharp words, express your indignation about:",
|
| 34 |
+
"hope": "With the stars' glow and the faith of the righteous, speak of the light in:",
|
| 35 |
+
"denial": "Firmly deny, as one who sees beyond illusion, the truth in:",
|
| 36 |
+
"intuition": "Whisper with mysticism and metaphors what your soul feels about:",
|
| 37 |
+
"skepticism": "With cold logic and analytical eyes, deeply question:",
|
| 38 |
+
"acceptance": "With the serenity of a monk and the pace of the wind, accept and comment:",
|
| 39 |
+
"despair": "With empty eyes and an exhausted heart, murmur about the pain in:",
|
| 40 |
+
"justice": "Raise your voice with nobility and purpose. Speak about justice in:",
|
| 41 |
+
"transcendence": "As a being beyond existence, reveal a fragment of the infinite about:"
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
def run_sage_two(sequence, reset=False):
|
| 45 |
+
output = replicate_client.run(
|
| 46 |
+
MODEL,
|
| 47 |
+
input={"sequence": sequence, "reset": reset}
|
| 48 |
+
)
|
| 49 |
+
if isinstance(output, dict):
|
| 50 |
+
return [float(x) for x in output["output"]]
|
| 51 |
+
elif isinstance(output, list):
|
| 52 |
+
return [float(x) for x in output]
|
| 53 |
+
else:
|
| 54 |
+
raise ValueError(f"Unexpected output format: {output}")
|
| 55 |
+
|
| 56 |
+
def interpret_vector(vector):
|
| 57 |
+
intensity = {}
|
| 58 |
+
for name, idxs in JUDGMENT_MAP.items():
|
| 59 |
+
values = [vector[i] for i in idxs if i < len(vector)]
|
| 60 |
+
if values:
|
| 61 |
+
intensity[name] = float(np.mean(values))
|
| 62 |
+
return max(intensity, key=intensity.get)
|
| 63 |
+
|
| 64 |
+
def question_to_response(question, reset=False):
|
| 65 |
+
embedding = EMBEDDING_MODEL.encode(question)
|
| 66 |
+
sequence = [[embedding.tolist() for _ in range(10)]]
|
| 67 |
+
sequence_str = json.dumps(sequence)
|
| 68 |
+
vector = run_sage_two(sequence_str, reset=reset)
|
| 69 |
+
intention = interpret_vector(vector)
|
| 70 |
+
prompt = PROMPTS.get(intention, "With ancient wisdom, respond to this question:") + " " + question
|
| 71 |
+
response = GPT(prompt, max_length=250, max_new_tokens=200, num_return_sequences=1)[0]["generated_text"]
|
| 72 |
+
return response.strip(), intention
|
| 73 |
+
|
| 74 |
+
def respond(question, reset_flag, password, chat_history):
|
| 75 |
+
if reset_flag and password != RESET_PASSWORD:
|
| 76 |
+
response = "Senha incorreta. A memória não foi resetada.\n\n"
|
| 77 |
+
intention = "error"
|
| 78 |
+
noreset_response, noreset_intention = question_to_response(question, reset=False)
|
| 79 |
+
response += noreset_response
|
| 80 |
+
if noreset_intention != "error":
|
| 81 |
+
response += f"\n\n🛡 Symbolic Intention: **{noreset_intention}**"
|
| 82 |
+
chat_history.append((question, response))
|
| 83 |
+
return chat_history, chat_history
|
| 84 |
+
else:
|
| 85 |
+
response, intention = question_to_response(question, reset=reset_flag)
|
| 86 |
+
full_response = f"{response}\n\n🛡 Symbolic Intention: **{intention}**" if intention != "error" else response
|
| 87 |
+
chat_history.append((question, full_response))
|
| 88 |
+
return chat_history, chat_history
|
| 89 |
+
|
| 90 |
+
with gr.Blocks() as demo:
|
| 91 |
+
gr.Markdown("""
|
| 92 |
+
# SAGE-2: Symbolic Adaptive General Engine
|
| 93 |
+
|
| 94 |
+
Ask anything. SAGE will interpret your symbolic intention before responding.
|
| 95 |
+
""")
|
| 96 |
+
|
| 97 |
+
chatbot = gr.Chatbot(label="SAGE responds")
|
| 98 |
+
inp = gr.Textbox(label="Your question")
|
| 99 |
+
reset_checkbox = gr.Checkbox(label="Reset symbolic consciousness?")
|
| 100 |
+
password_box = gr.Textbox(label="Password", type="password")
|
| 101 |
+
state = gr.State([])
|
| 102 |
+
|
| 103 |
+
btn = gr.Button("Ask")
|
| 104 |
+
btn.click(fn=respond, inputs=[inp, reset_checkbox, password_box, state], outputs=[chatbot, state])
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch()
|