Spaces:
Paused
Paused
Automated deployment via API
Browse files
app.py
CHANGED
|
@@ -357,15 +357,8 @@ def upload_data():
|
|
| 357 |
logger.error(f"❌ Upload error: {e}")
|
| 358 |
return jsonify({'error': str(e)}), 500
|
| 359 |
|
| 360 |
-
from huggingface_hub import InferenceClient
|
| 361 |
-
import os
|
| 362 |
-
|
| 363 |
-
# Initialize Inference Client
|
| 364 |
-
# Using Zephyr-7B Beta: The gold standard for free-tier Spaces hosting
|
| 365 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=os.environ.get("HF_TOKEN"))
|
| 366 |
-
|
| 367 |
def generate_ai_response(query):
|
| 368 |
-
"""Generate AI response using
|
| 369 |
|
| 370 |
# 1. GATHER LIVE CONTEXT
|
| 371 |
current_temp = app_state.get_temperature()
|
|
@@ -374,40 +367,80 @@ def generate_ai_response(query):
|
|
| 374 |
risk = app_state.anomaly_risk * 100
|
| 375 |
opt_low = CONFIG['TEMP_OPTIMAL_LOW']
|
| 376 |
opt_high = CONFIG['TEMP_OPTIMAL_HIGH']
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
You are the IronGuard Foundry AI.
|
| 382 |
-
LIVE METRICS:
|
| 383 |
-
- Temp: {current_temp:.1f}C (Target: {opt_low}-{opt_high})
|
| 384 |
-
- Energy: {energy:.1f} kWh
|
| 385 |
-
- Anomaly Risk: {risk:.1f}%
|
| 386 |
-
|
| 387 |
-
INSTRUCTIONS:
|
| 388 |
-
- Answer based on LIVE METRICS.
|
| 389 |
-
- If Temp > {CONFIG['TEMP_MAX']}, warn immediately.
|
| 390 |
-
- Keep answers short and professional.
|
| 391 |
-
"""
|
| 392 |
-
|
| 393 |
-
# 3. CALL API
|
| 394 |
try:
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
except Exception as e:
|
| 408 |
-
logger.error(f"❌
|
| 409 |
-
|
| 410 |
-
return f"⚠️ **Connection Error**: {str(e)}"
|
| 411 |
|
| 412 |
@app.route('/api/chat', methods=['POST'])
|
| 413 |
def chat():
|
|
|
|
| 357 |
logger.error(f"❌ Upload error: {e}")
|
| 358 |
return jsonify({'error': str(e)}), 500
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
def generate_ai_response(query):
|
| 361 |
+
"""Generate AI response using local logic - no external API needed"""
|
| 362 |
|
| 363 |
# 1. GATHER LIVE CONTEXT
|
| 364 |
current_temp = app_state.get_temperature()
|
|
|
|
| 367 |
risk = app_state.anomaly_risk * 100
|
| 368 |
opt_low = CONFIG['TEMP_OPTIMAL_LOW']
|
| 369 |
opt_high = CONFIG['TEMP_OPTIMAL_HIGH']
|
| 370 |
+
|
| 371 |
+
query_lower = query.lower().strip()
|
| 372 |
+
|
| 373 |
+
# 2. INTELLIGENT LOCAL RESPONSE ENGINE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
try:
|
| 375 |
+
# Temperature status
|
| 376 |
+
if current_temp < opt_low:
|
| 377 |
+
temp_status = "below optimal"
|
| 378 |
+
temp_advice = "Consider increasing furnace power to reach target range."
|
| 379 |
+
elif current_temp > opt_high:
|
| 380 |
+
temp_status = "above optimal"
|
| 381 |
+
temp_advice = "Reduce heat input or increase cooling to stabilize."
|
| 382 |
+
else:
|
| 383 |
+
temp_status = "within optimal range"
|
| 384 |
+
temp_advice = "Maintain current settings for best results."
|
| 385 |
+
|
| 386 |
+
# Anomaly warning
|
| 387 |
+
anomaly_warning = ""
|
| 388 |
+
if is_anomaly or risk > 50:
|
| 389 |
+
anomaly_warning = f"⚠️ ALERT: Anomaly risk at {risk:.1f}%. Immediate attention recommended. "
|
| 390 |
+
|
| 391 |
+
# Query-based responses
|
| 392 |
+
if any(word in query_lower for word in ['hi', 'hello', 'hey', 'greetings']):
|
| 393 |
+
return f"Hello! I'm your Forge AI assistant. Current furnace temp is {current_temp:.1f}°C ({temp_status}). How can I help you today?"
|
| 394 |
+
|
| 395 |
+
elif any(word in query_lower for word in ['temperature', 'temp', 'heat', 'hot', 'cold']):
|
| 396 |
+
return f"{anomaly_warning}Current temperature: {current_temp:.1f}°C ({temp_status}). Target range: {opt_low}-{opt_high}°C. {temp_advice}"
|
| 397 |
+
|
| 398 |
+
elif any(word in query_lower for word in ['energy', 'power', 'consumption', 'kwh']):
|
| 399 |
+
efficiency = "optimal" if 420 <= energy <= 480 else "needs attention"
|
| 400 |
+
return f"Energy consumption: {energy:.1f} kWh. Status: {efficiency}. Current temp: {current_temp:.1f}°C."
|
| 401 |
+
|
| 402 |
+
elif any(word in query_lower for word in ['anomaly', 'risk', 'alert', 'warning', 'danger']):
|
| 403 |
+
if is_anomaly:
|
| 404 |
+
return f"⚠️ ANOMALY DETECTED! Risk level: {risk:.1f}%. Temperature: {current_temp:.1f}°C. Recommend immediate inspection of furnace parameters."
|
| 405 |
+
else:
|
| 406 |
+
return f"✅ System normal. Risk level: {risk:.1f}%. All parameters within acceptable bounds."
|
| 407 |
+
|
| 408 |
+
elif any(word in query_lower for word in ['status', 'overview', 'summary', 'report']):
|
| 409 |
+
status_icon = "⚠️" if is_anomaly else "✅"
|
| 410 |
+
return f"{status_icon} System Status:\n• Temperature: {current_temp:.1f}°C ({temp_status})\n• Energy: {energy:.1f} kWh\n• Risk Level: {risk:.1f}%\n• {temp_advice}"
|
| 411 |
+
|
| 412 |
+
elif any(word in query_lower for word in ['pour', 'pouring', 'ready', 'readiness', 'cast']):
|
| 413 |
+
if opt_low <= current_temp <= opt_high and risk < 30:
|
| 414 |
+
return f"✅ POURING READY! Temperature {current_temp:.1f}°C is optimal. Risk level low at {risk:.1f}%. Proceed with pour."
|
| 415 |
+
else:
|
| 416 |
+
issues = []
|
| 417 |
+
if current_temp < opt_low:
|
| 418 |
+
issues.append(f"temp too low ({current_temp:.1f}°C)")
|
| 419 |
+
elif current_temp > opt_high:
|
| 420 |
+
issues.append(f"temp too high ({current_temp:.1f}°C)")
|
| 421 |
+
if risk >= 30:
|
| 422 |
+
issues.append(f"elevated risk ({risk:.1f}%)")
|
| 423 |
+
return f"⚠️ NOT READY FOR POUR. Issues: {', '.join(issues)}. Wait for stabilization."
|
| 424 |
+
|
| 425 |
+
elif any(word in query_lower for word in ['optimize', 'efficiency', 'improve', 'better']):
|
| 426 |
+
return f"Optimization tips:\n• Target temp: {opt_low}-{opt_high}°C (current: {current_temp:.1f}°C)\n• Optimal energy: ~450 kWh (current: {energy:.1f} kWh)\n• {temp_advice}"
|
| 427 |
+
|
| 428 |
+
elif any(word in query_lower for word in ['help', 'what can you do', 'commands', 'options']):
|
| 429 |
+
return "I can help with:\n• Temperature monitoring & alerts\n• Energy consumption analysis\n• Anomaly detection & risk assessment\n• Pouring readiness checks\n• System optimization tips\nJust ask about any of these!"
|
| 430 |
+
|
| 431 |
+
elif any(word in query_lower for word in ['safety', 'safe', 'hazard']):
|
| 432 |
+
if is_anomaly or current_temp > CONFIG['TEMP_MAX'] - 20:
|
| 433 |
+
return f"⚠️ SAFETY CONCERN: Temperature at {current_temp:.1f}°C with {risk:.1f}% risk. Monitor closely and ensure safety protocols are active."
|
| 434 |
+
else:
|
| 435 |
+
return f"✅ Safety status: Normal. Temperature {current_temp:.1f}°C within safe limits. Risk level: {risk:.1f}%."
|
| 436 |
+
|
| 437 |
+
else:
|
| 438 |
+
# Default intelligent response
|
| 439 |
+
return f"{anomaly_warning}Current readings - Temp: {current_temp:.1f}°C ({temp_status}), Energy: {energy:.1f} kWh, Risk: {risk:.1f}%. Ask me about temperature, energy, anomalies, pouring readiness, or safety!"
|
| 440 |
|
| 441 |
except Exception as e:
|
| 442 |
+
logger.error(f"❌ Response generation error: {e}")
|
| 443 |
+
return f"System operational. Temp: {current_temp:.1f}°C, Energy: {energy:.1f} kWh. How can I assist?"
|
|
|
|
| 444 |
|
| 445 |
@app.route('/api/chat', methods=['POST'])
|
| 446 |
def chat():
|