Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,83 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import joblib
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 4 |
+
import requests
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# === Load API Key from Environment Variable ===
|
| 8 |
+
GROQ_API_KEY = os.environ.get("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", "")
|
| 9 |
+
|
| 10 |
+
# === Train Model on Sample Data ===
|
| 11 |
+
X_train = np.array([
|
| 12 |
+
[35, 10, 1, 6500],
|
| 13 |
+
[40, 20, 2, 3000],
|
| 14 |
+
[20, 15, 2.5, 4000],
|
| 15 |
+
[30, 12, 1.2, 5500],
|
| 16 |
+
[25, 18, 3.0, 4500]
|
| 17 |
+
])
|
| 18 |
+
y_train = np.array([78, 50, 45, 70, 55])
|
| 19 |
+
|
| 20 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
| 21 |
+
model.fit(X_train, y_train)
|
| 22 |
+
|
| 23 |
+
# === Prediction Function ===
|
| 24 |
+
def predict_efficiency(vm, ash, sulfur, gcv):
|
| 25 |
+
if vm == 0 and ash == 0 and sulfur == 0 and gcv == 0:
|
| 26 |
+
return 0.0, ""
|
| 27 |
+
if ash > 50 or gcv < 1000:
|
| 28 |
+
return 5.0, "Ash is too high or GCV is too low."
|
| 29 |
+
|
| 30 |
+
prediction = model.predict([[vm, ash, sulfur, gcv]])[0]
|
| 31 |
+
prediction = max(0.0, min(100.0, round(prediction, 2)))
|
| 32 |
+
|
| 33 |
+
prompt = f"""The following coal sample has:
|
| 34 |
+
- Volatile Matter: {vm}%
|
| 35 |
+
- Ash: {ash}%
|
| 36 |
+
- Total Sulfur: {sulfur}%
|
| 37 |
+
- GCV: {gcv} kcal/kg
|
| 38 |
+
|
| 39 |
+
The predicted combustion efficiency is {prediction}%.
|
| 40 |
+
|
| 41 |
+
Give 1 or 2 short suggestions to improve this combustion efficiency."""
|
| 42 |
+
|
| 43 |
+
suggestion = ""
|
| 44 |
+
if GROQ_API_KEY:
|
| 45 |
+
headers = {
|
| 46 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 47 |
+
"Content-Type": "application/json"
|
| 48 |
+
}
|
| 49 |
+
payload = {
|
| 50 |
+
"model": "llama3-8b-8192",
|
| 51 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 52 |
+
"temperature": 0.7
|
| 53 |
+
}
|
| 54 |
+
try:
|
| 55 |
+
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
|
| 56 |
+
suggestion = response.json()["choices"][0]["message"]["content"]
|
| 57 |
+
except Exception as e:
|
| 58 |
+
suggestion = f"Error: {str(e)}"
|
| 59 |
+
else:
|
| 60 |
+
suggestion = "GROQ_API_KEY not set."
|
| 61 |
+
|
| 62 |
+
return prediction, suggestion
|
| 63 |
+
|
| 64 |
+
# === Gradio UI ===
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
+
gr.Markdown("## 🔥 Smart FuelTech: AI-Powered Combustion Efficiency Monitor")
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
vm = gr.Number(label="Volatile Matter (%)", value=35)
|
| 70 |
+
ash = gr.Number(label="Ash Content (%)", value=10)
|
| 71 |
+
sulfur = gr.Number(label="Total Sulfur (%)", value=1)
|
| 72 |
+
gcv = gr.Number(label="GCV (kcal/kg)", value=6500)
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
efficiency_output = gr.Number(label="Predicted Efficiency (%)")
|
| 76 |
+
suggestion_output = gr.Textbox(label="AI Suggestion")
|
| 77 |
+
|
| 78 |
+
submit = gr.Button("Predict")
|
| 79 |
+
submit.click(fn=predict_efficiency,
|
| 80 |
+
inputs=[vm, ash, sulfur, gcv],
|
| 81 |
+
outputs=[efficiency_output, suggestion_output])
|
| 82 |
+
|
| 83 |
+
demo.launch()
|