Spaces:
Sleeping
Sleeping
Download app.py from frc123456789/Smart-FuelTech: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/spaces/frc123456789/Smart-FuelTech/resolve/main/app.py
- Command line
-
hf download hf://spaces/frc123456789/Smart-FuelTech/app.py
-
curl -L -o app.py https://huggingface.co/spaces/frc123456789/Smart-FuelTech/resolve/main/app.py
2.61 kB
| 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() | |