frc123456789 commited on
Commit
f64f234
·
verified ·
1 Parent(s): 281ade8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -27
app.py CHANGED
@@ -1,30 +1,83 @@
1
  import gradio as gr
2
- import joblib
3
  import numpy as np
 
 
 
4
 
5
- # Load the trained model
6
- model = joblib.load("model.pkl")
7
-
8
- # Define prediction function
9
- def predict_efficiency(sulfur, gcv):
10
- try:
11
- input_data = np.array([[sulfur, gcv]])
12
- prediction = model.predict(input_data)
13
- return f"Predicted Combustion Efficiency: {prediction[0]:.2f}%"
14
- except Exception as e:
15
- return f"Error: {e}"
16
-
17
- # Gradio UI
18
- demo = gr.Interface(
19
- fn=predict_efficiency,
20
- inputs=[
21
- gr.Number(label="Total Sulfur (%)"),
22
- gr.Number(label="GCV (kcal/kg)")
23
- ],
24
- outputs="text",
25
- title="Smart FuelTech: Combustion Efficiency Predictor",
26
- description="Enter sulfur and GCV to predict combustion efficiency of coal."
27
- )
28
-
29
- if __name__ == "__main__":
30
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()