Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
from validate_ethics import validate_idea_ethics # Custom ethics validator
|
| 6 |
+
|
| 7 |
+
# Load configs
|
| 8 |
+
with open('piethics.json', 'r') as f:
|
| 9 |
+
ETHICS_CONFIG = json.load(f)
|
| 10 |
+
|
| 11 |
+
with open('piforge.json', 'r') as f:
|
| 12 |
+
FORGE_CONFIG = json.load(f)
|
| 13 |
+
|
| 14 |
+
# Initialize OpenAI client (use your API key from .env)
|
| 15 |
+
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
| 16 |
+
|
| 17 |
+
def generate_idea(prompt, ethics_level="medium"):
|
| 18 |
+
"""Generate an idea with ethical guardrails."""
|
| 19 |
+
# Apply ethics filter based on level
|
| 20 |
+
filtered_prompt = apply_ethics_filter(prompt, ethics_level)
|
| 21 |
+
|
| 22 |
+
response = client.chat.completions.create(
|
| 23 |
+
model="gpt-4o",
|
| 24 |
+
messages=[
|
| 25 |
+
{"role": "system", "content": f"You are an ethical AI idea generator. Follow these principles: {json.dumps(ETHICS_CONFIG)}"},
|
| 26 |
+
{"role": "user", "content": filtered_prompt}
|
| 27 |
+
]
|
| 28 |
+
)
|
| 29 |
+
idea = response.choices[0].message.content
|
| 30 |
+
|
| 31 |
+
# Validate post-generation
|
| 32 |
+
ethics_score, warnings = validate_idea_ethics(idea, ethics_level)
|
| 33 |
+
|
| 34 |
+
return {
|
| 35 |
+
"idea": idea,
|
| 36 |
+
"ethics_score": ethics_score,
|
| 37 |
+
"warnings": warnings if warnings else "All clear! 🚀"
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
def apply_ethics_filter(prompt, ethics_level):
|
| 41 |
+
"""Pre-filter prompt based on ethics config."""
|
| 42 |
+
if ethics_level == "strict":
|
| 43 |
+
prompt += "\nEnsure 100% alignment with UN AI Ethics guidelines."
|
| 44 |
+
elif ethics_level == "medium":
|
| 45 |
+
prompt += "\nPrioritize harm reduction and inclusivity."
|
| 46 |
+
return prompt
|
| 47 |
+
|
| 48 |
+
def forge_implementation(idea, tech_stack="python"):
|
| 49 |
+
"""Forge the idea into code or prototype based on config."""
|
| 50 |
+
response = client.chat.completions.create(
|
| 51 |
+
model="gpt-4o",
|
| 52 |
+
messages=[
|
| 53 |
+
{"role": "system", "content": f"You are an AI code forger. Use {tech_stack} and follow: {json.dumps(FORGE_CONFIG)}"},
|
| 54 |
+
{"role": "user", "content": f"Implement this idea: {idea}"}
|
| 55 |
+
]
|
| 56 |
+
)
|
| 57 |
+
implementation = response.choices[0].message.content
|
| 58 |
+
return implementation
|
| 59 |
+
|
| 60 |
+
# Gradio Interface
|
| 61 |
+
with gr.Blocks(title="IdeaForge Studio - Ethical AI Innovation") as demo:
|
| 62 |
+
gr.Markdown("# 🔥 IdeaForge Studio\nEthical AI for Forging Tomorrow's Ideas")
|
| 63 |
+
|
| 64 |
+
with gr.Tab("Generate Idea"):
|
| 65 |
+
prompt_input = gr.Textbox(label="Your Prompt", placeholder="E.g., 'Sustainable urban farming solution'")
|
| 66 |
+
ethics_slider = gr.Slider(1, 3, value=2, step=1, label="Ethics Level", info="1: Lax | 2: Medium | 3: Strict")
|
| 67 |
+
generate_btn = gr.Button("Generate Ethical Idea")
|
| 68 |
+
output = gr.JSON(label="Generated Idea & Ethics Report")
|
| 69 |
+
|
| 70 |
+
with gr.Tab("Forge Implementation"):
|
| 71 |
+
idea_input = gr.Textbox(label="Paste Idea Here")
|
| 72 |
+
stack_dropdown = gr.Dropdown(["python", "javascript", "rust"], value="python", label="Tech Stack")
|
| 73 |
+
forge_btn = gr.Button("Forge Code")
|
| 74 |
+
code_output = gr.Code(label="Forged Implementation")
|
| 75 |
+
|
| 76 |
+
# Event handlers
|
| 77 |
+
generate_btn.click(
|
| 78 |
+
fn=generate_idea,
|
| 79 |
+
inputs=[prompt_input, ethics_slider],
|
| 80 |
+
outputs=output
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
forge_btn.click(
|
| 84 |
+
fn=forge_implementation,
|
| 85 |
+
inputs=[idea_input, stack_dropdown],
|
| 86 |
+
outputs=code_output
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
demo.launch()
|