File size: 2,614 Bytes
fb2da3e
 
f64f234
 
 
fb2da3e
f64f234
2b57f7a
f64f234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import gradio as gr
import numpy as np
from sklearn.ensemble import RandomForestRegressor
import requests
import os

# === Load API Key from Environment Variable ===
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
# === Train Model on Sample Data ===
X_train = np.array([
    [35, 10, 1, 6500],
    [40, 20, 2, 3000],
    [20, 15, 2.5, 4000],
    [30, 12, 1.2, 5500],
    [25, 18, 3.0, 4500]
])
y_train = np.array([78, 50, 45, 70, 55])

model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# === Prediction Function ===
def predict_efficiency(vm, ash, sulfur, gcv):
    if vm == 0 and ash == 0 and sulfur == 0 and gcv == 0:
        return 0.0, ""
    if ash > 50 or gcv < 1000:
        return 5.0, "Ash is too high or GCV is too low."

    prediction = model.predict([[vm, ash, sulfur, gcv]])[0]
    prediction = max(0.0, min(100.0, round(prediction, 2)))

    prompt = f"""The following coal sample has:
- Volatile Matter: {vm}%
- Ash: {ash}%
- Total Sulfur: {sulfur}%
- GCV: {gcv} kcal/kg

The predicted combustion efficiency is {prediction}%.

Give 1 or 2 short suggestions to improve this combustion efficiency."""

    suggestion = ""
    if GROQ_API_KEY:
        headers = {
            "Authorization": f"Bearer {GROQ_API_KEY}",
            "Content-Type": "application/json"
        }
        payload = {
            "model": "llama3-8b-8192",
            "messages": [{"role": "user", "content": prompt}],
            "temperature": 0.7
        }
        try:
            response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
            suggestion = response.json()["choices"][0]["message"]["content"]
        except Exception as e:
            suggestion = f"Error: {str(e)}"
    else:
        suggestion = "GROQ_API_KEY not set."

    return prediction, suggestion

# === Gradio UI ===
with gr.Blocks() as demo:
    gr.Markdown("## 🔥 Smart FuelTech: AI-Powered Combustion Efficiency Monitor")

    with gr.Row():
        vm = gr.Number(label="Volatile Matter (%)", value=35)
        ash = gr.Number(label="Ash Content (%)", value=10)
        sulfur = gr.Number(label="Total Sulfur (%)", value=1)
        gcv = gr.Number(label="GCV (kcal/kg)", value=6500)

    with gr.Row():
        efficiency_output = gr.Number(label="Predicted Efficiency (%)")
        suggestion_output = gr.Textbox(label="AI Suggestion")

    submit = gr.Button("Predict")
    submit.click(fn=predict_efficiency,
                 inputs=[vm, ash, sulfur, gcv],
                 outputs=[efficiency_output, suggestion_output])

demo.launch()