Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Configuração da API do Manus/OpenAI (será passada como secret no Space)
|
| 8 |
+
client = OpenAI()
|
| 9 |
+
|
| 10 |
+
def load_examples():
|
| 11 |
+
examples = []
|
| 12 |
+
try:
|
| 13 |
+
with open("dataset.jsonl", "r", encoding="utf-8") as f:
|
| 14 |
+
for line in f:
|
| 15 |
+
examples.append(json.loads(line))
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Erro ao carregar dataset: {e}")
|
| 18 |
+
return examples
|
| 19 |
+
|
| 20 |
+
dataset_examples = load_examples()
|
| 21 |
+
|
| 22 |
+
def reality_engine(instruction, category, premise_strength, physics_mode, output_style):
|
| 23 |
+
prompt = f"""
|
| 24 |
+
Tu és o Reality Engine. O teu objetivo é simular realidades alternativas e cenários especulativos.
|
| 25 |
+
|
| 26 |
+
PREMISSA: {instruction}
|
| 27 |
+
CATEGORIA: {category}
|
| 28 |
+
FORÇA DA PREMISSA: {premise_strength}
|
| 29 |
+
MODO DE FÍSICA: {physics_mode}
|
| 30 |
+
ESTILO DE SAÍDA: {output_style}
|
| 31 |
+
|
| 32 |
+
REGRAS DE RACIOCÍNIO: "aceitar premissa, definir regras, calcular consequências, evitar contradições"
|
| 33 |
+
|
| 34 |
+
Por favor, gera uma simulação detalhada baseada nestes parâmetros.
|
| 35 |
+
Distingue claramente entre realidade factual e esta especulação.
|
| 36 |
+
Mantém a coerência interna, mesmo que a premissa seja impossível.
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
response = client.chat.completions.create(
|
| 41 |
+
model="gpt-4o",
|
| 42 |
+
messages=[
|
| 43 |
+
{"role": "system", "content": "És o Reality Engine, um simulador de realidades alternativas de alta fidelidade."},
|
| 44 |
+
{"role": "user", "content": prompt}
|
| 45 |
+
],
|
| 46 |
+
temperature=0.7
|
| 47 |
+
)
|
| 48 |
+
return response.choices[0].message.content
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Erro na simulação: {str(e)}"
|
| 51 |
+
|
| 52 |
+
def get_random_example():
|
| 53 |
+
if not dataset_examples:
|
| 54 |
+
return "E se a Lua desaparecesse?", "astronomia", "teorica", "realista", "analise_de_consequencias"
|
| 55 |
+
ex = random.choice(dataset_examples)
|
| 56 |
+
return ex['instruction'], ex['category'], ex['premise_strength'], ex['physics_mode'], ex['output_style']
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 59 |
+
gr.Markdown("# 🌌 Reality Engine Bot")
|
| 60 |
+
gr.Markdown("Simulador de realidades alternativas, mundos hipotéticos e cenários especulativos.")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
instruction = gr.Textbox(label="Instrução (E se...)", placeholder="Ex: E se a biblioteca de Alexandria nunca tivesse ardido?")
|
| 65 |
+
category = gr.Dropdown(
|
| 66 |
+
choices=["historia_alternativa", "ficcao_cientifica", "fantasia", "tecnologia", "biologia", "astronomia", "sociedade", "filosofia", "geopolitica", "evolucao", "transhumanismo"],
|
| 67 |
+
label="Categoria"
|
| 68 |
+
)
|
| 69 |
+
premise_strength = gr.Dropdown(
|
| 70 |
+
choices=["historica", "teorica", "especulativa", "alta_especulacao", "ficcao"],
|
| 71 |
+
label="Força da Premissa"
|
| 72 |
+
)
|
| 73 |
+
physics_mode = gr.Dropdown(
|
| 74 |
+
choices=["realista", "cientifico", "nova_fisica", "fantasia_regrada"],
|
| 75 |
+
label="Modo de Física"
|
| 76 |
+
)
|
| 77 |
+
output_style = gr.Dropdown(
|
| 78 |
+
choices=["analise_de_consequencias", "worldbuilding", "simulacao_temporal", "impacto_social", "primeiro_contacto", "evolucao_civilizacional"],
|
| 79 |
+
label="Estilo de Saída"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
btn_random = gr.Button("🎲 Exemplo Aleatório")
|
| 84 |
+
btn_run = gr.Button("🚀 Iniciar Simulação", variant="primary")
|
| 85 |
+
|
| 86 |
+
with gr.Column():
|
| 87 |
+
output = gr.Markdown(label="Resultado da Simulação")
|
| 88 |
+
|
| 89 |
+
btn_random.click(
|
| 90 |
+
fn=get_random_example,
|
| 91 |
+
outputs=[instruction, category, premise_strength, physics_mode, output_style]
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
btn_run.click(
|
| 95 |
+
fn=reality_engine,
|
| 96 |
+
inputs=[instruction, category, premise_strength, physics_mode, output_style],
|
| 97 |
+
outputs=output
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
demo.launch()
|