Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,118 +1,170 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
import json
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
else:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Generate real proposal ID
|
| 33 |
-
proposal_id = f"prop_{hashlib.sha256(project_name.encode()).hexdigest()[:12]}"
|
| 34 |
-
|
| 35 |
-
# Determine mining boost - NOW ACTIVATES AT 69+ (since we're using 0-100 scale)
|
| 36 |
-
mining_boost = 15 if resonance >= 69 else 0 # Changed threshold
|
| 37 |
-
|
| 38 |
-
# Quantum bridge payload
|
| 39 |
-
quantum_payload = {
|
| 40 |
-
"type": "ethical_audit_complete",
|
| 41 |
-
"timestamp": datetime.now().isoformat(),
|
| 42 |
-
"results": {
|
| 43 |
-
"ethical_score": ethical_score,
|
| 44 |
-
"resonance": resonance, # Now 0-100 scale
|
| 45 |
-
"mining_boost": mining_boost,
|
| 46 |
-
"proposal_id": proposal_id,
|
| 47 |
-
"verdict": "🌟 SOVEREIGN OVERDRIVE: Mining Boost ACTIVATED! +15%" if mining_boost > 0 else "🌀 Strong ethical foundation - continue refinement"
|
| 48 |
-
}
|
| 49 |
-
}
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
}} catch (e) {{
|
| 58 |
-
window.parent.postMessage({json.dumps(quantum_payload)}, "*");
|
| 59 |
-
}}
|
| 60 |
-
</script>
|
| 61 |
-
"""
|
| 62 |
|
| 63 |
-
|
| 64 |
-
user_output = f"""
|
| 65 |
-
## 🛡️ Ethical Audit Complete!
|
| 66 |
-
|
| 67 |
-
**Project:** {project_name}
|
| 68 |
-
**Focus:** {ethical_focus}
|
| 69 |
-
**Ethical Score:** {ethical_score}/1000
|
| 70 |
-
**Resonance:** {resonance}/100
|
| 71 |
-
**Mining Boost:** +{mining_boost}%
|
| 72 |
-
**Proposal ID:** {proposal_id}
|
| 73 |
-
|
| 74 |
-
**Verdict:** {'🌟 SOVEREIGN OVERDRIVE - Mining Boost ACTIVATED!' if mining_boost > 0 else '🌀 Strong ethical foundation - continue refinement'}
|
| 75 |
-
|
| 76 |
-
*Results sent to Pi wrapper via quantum bridge*
|
| 77 |
-
"""
|
| 78 |
-
|
| 79 |
-
return user_output, js_code
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
with gr.Blocks(
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
gr.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
label="Project Name",
|
| 94 |
-
value="SCF Force Ethics",
|
| 95 |
-
placeholder="Enter your Pi ecosystem project..."
|
| 96 |
-
)
|
| 97 |
-
ethical_focus = gr.Dropdown(
|
| 98 |
-
choices=["Community Governance", "Transparent AI", "Fair Distribution", "Educational Access"],
|
| 99 |
-
label="Primary Ethical Focus",
|
| 100 |
-
value="Community Governance"
|
| 101 |
-
)
|
| 102 |
-
audit_btn = gr.Button("🚀 Run Sovereign Audit", variant="primary")
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
-
demo.launch(share=True
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
import json
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
+
# Secure token loading from environment
|
| 8 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
+
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
| 10 |
+
|
| 11 |
+
class DeepSeekAssistant:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
self.hf_client = InferenceClient(token=HF_TOKEN)
|
| 14 |
+
|
| 15 |
+
def query_deepseek(self, message, history=None):
|
| 16 |
+
"""Query DeepSeek model through Hugging Face"""
|
| 17 |
+
try:
|
| 18 |
+
# Method 1: Try HF Inference API first
|
| 19 |
+
response = self.hf_client.chat_completion(
|
| 20 |
+
model="deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct",
|
| 21 |
+
messages=[
|
| 22 |
+
{
|
| 23 |
+
"role": "system",
|
| 24 |
+
"content": """You are a Pi Network and ethical mining expert. Provide helpful,
|
| 25 |
+
accurate advice about:
|
| 26 |
+
- Pi Network development and SDK
|
| 27 |
+
- Mining optimization strategies
|
| 28 |
+
- Ethical scoring systems
|
| 29 |
+
- Community building
|
| 30 |
+
- Blockchain technology
|
| 31 |
+
|
| 32 |
+
Keep responses concise and practical."""
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"role": "user",
|
| 36 |
+
"content": message
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
max_tokens=500,
|
| 40 |
+
temperature=0.7
|
| 41 |
+
)
|
| 42 |
+
return response.choices[0].message.content
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
# Fallback to text generation
|
| 46 |
+
try:
|
| 47 |
+
prompt = f"""As a Pi Network expert, answer this question: {message}
|
| 48 |
+
|
| 49 |
+
Provide helpful, practical advice about Pi mining, development, or ethical scoring."""
|
| 50 |
+
|
| 51 |
+
response = self.hf_client.text_generation(
|
| 52 |
+
prompt,
|
| 53 |
+
model="deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct",
|
| 54 |
+
max_new_tokens=300,
|
| 55 |
+
temperature=0.7
|
| 56 |
+
)
|
| 57 |
+
return response
|
| 58 |
+
except Exception as e2:
|
| 59 |
+
return f"🤖 AI Assistant: I'm currently learning about Pi Network! Try asking about mining strategies, ethical scoring, or app development. (Error: {str(e2)})"
|
| 60 |
+
|
| 61 |
+
# Initialize assistant
|
| 62 |
+
ai_assistant = DeepSeekAssistant()
|
| 63 |
+
|
| 64 |
+
def ethical_audit(proposal_text):
|
| 65 |
+
"""Your existing ethical audit function"""
|
| 66 |
+
# Simulate ethical scoring
|
| 67 |
+
score = min(1000, max(600, len(proposal_text) * 2))
|
| 68 |
+
resonance = (score * 0.7 + 80 * 3) // 10
|
| 69 |
+
boost = 15 if resonance >= 691 else 0
|
| 70 |
+
|
| 71 |
+
return {
|
| 72 |
+
"ethical_score": score,
|
| 73 |
+
"resonance": resonance,
|
| 74 |
+
"mining_boost": boost,
|
| 75 |
+
"proposal_id": "prop_3701b5dedf34"
|
| 76 |
}
|
| 77 |
+
|
| 78 |
+
def chat_with_deepseek(message, history):
|
| 79 |
+
"""Chat interface with DeepSeek"""
|
| 80 |
+
response = ai_assistant.query_deepseek(message, history)
|
| 81 |
+
return response
|
| 82 |
+
|
| 83 |
+
def full_audit_pipeline(proposal, chat_question):
|
| 84 |
+
"""Combine ethical audit with AI assistance"""
|
| 85 |
+
# Run ethical audit
|
| 86 |
+
audit_results = ethical_audit(proposal)
|
| 87 |
+
|
| 88 |
+
# Get AI response if question provided
|
| 89 |
+
if chat_question and len(chat_question.strip()) > 0:
|
| 90 |
+
ai_response = ai_assistant.query_deepseek(chat_question)
|
| 91 |
else:
|
| 92 |
+
ai_response = "Ask me anything about Pi Network, mining, or ethical development!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Format results
|
| 95 |
+
audit_summary = f"""**Audit Complete!**
|
| 96 |
+
- Ethical Score: {audit_results['ethical_score']}/1000
|
| 97 |
+
- Resonance: {audit_results['resonance']}
|
| 98 |
+
- Mining Boost: +{audit_results['mining_boost']}%
|
| 99 |
+
- Proposal ID: {audit_results['proposal_id']}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
return audit_summary, ai_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# Create Gradio interface
|
| 104 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="PiForge + DeepSeek AI") as demo:
|
| 105 |
+
gr.Markdown("""
|
| 106 |
+
# 🔨 PiForge Ethical Dual-Forge
|
| 107 |
+
## Powered by DeepSeek AI Assistant
|
| 108 |
+
""")
|
| 109 |
+
|
| 110 |
+
with gr.Tab("🧠 DeepSeek AI Assistant"):
|
| 111 |
+
gr.Markdown("### Ask me about Pi Network, mining, or development!")
|
| 112 |
+
chatbot = gr.Chatbot(label="DeepSeek Conversation", height=400)
|
| 113 |
+
msg = gr.Textbox(label="Your question", placeholder="Ask about Pi mining, SDK, or ethical scoring...")
|
| 114 |
+
clear = gr.Button("Clear Chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
def respond(message, chat_history):
|
| 117 |
+
bot_message = ai_assistant.query_deepseek(message, chat_history)
|
| 118 |
+
chat_history.append((message, bot_message))
|
| 119 |
+
return "", chat_history
|
| 120 |
+
|
| 121 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 122 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 123 |
+
|
| 124 |
+
with gr.Tab("📊 Ethical Audit"):
|
| 125 |
+
gr.Markdown("### Run Ethical Audit + Get AI Insights")
|
| 126 |
+
with gr.Row():
|
| 127 |
+
with gr.Column():
|
| 128 |
+
proposal = gr.Textbox(
|
| 129 |
+
label="Project Proposal",
|
| 130 |
+
placeholder="Describe your project for ethical evaluation...",
|
| 131 |
+
lines=4
|
| 132 |
+
)
|
| 133 |
+
question = gr.Textbox(
|
| 134 |
+
label="Ask DeepSeek (optional)",
|
| 135 |
+
placeholder="Ask for improvement suggestions or specific advice..."
|
| 136 |
+
)
|
| 137 |
+
audit_btn = gr.Button("Run Audit with AI Analysis", variant="primary")
|
| 138 |
+
|
| 139 |
+
with gr.Column():
|
| 140 |
+
audit_output = gr.Markdown(label="Audit Results")
|
| 141 |
+
ai_output = gr.Markdown(label="AI Insights")
|
| 142 |
+
|
| 143 |
+
audit_btn.click(
|
| 144 |
+
full_audit_pipeline,
|
| 145 |
+
inputs=[proposal, question],
|
| 146 |
+
outputs=[audit_output, ai_output]
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
with gr.Tab("🔧 Quick Help"):
|
| 150 |
+
gr.Markdown("""
|
| 151 |
+
### Common Questions for DeepSeek:
|
| 152 |
+
|
| 153 |
+
**Pi Mining:**
|
| 154 |
+
- "How can I maximize my Pi mining rate?"
|
| 155 |
+
- "What's the best time to mine Pi?"
|
| 156 |
+
- "How does the mining boost work?"
|
| 157 |
+
|
| 158 |
+
**Development:**
|
| 159 |
+
- "How do I use the Pi SDK for authentication?"
|
| 160 |
+
- "What are the Pi App Store requirements?"
|
| 161 |
+
- "How to create a mining boost feature?"
|
| 162 |
+
|
| 163 |
+
**Ethical Scoring:**
|
| 164 |
+
- "What makes a project ethically sound?"
|
| 165 |
+
- "How is the resonance score calculated?"
|
| 166 |
+
- "What's the Sovereign Canticle protocol?"
|
| 167 |
+
""")
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|
| 170 |
+
demo.launch(share=True)
|