Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +53 -0
- app.py +171 -0
- code_flow +15 -0
- prasunethon.html +143 -0
Dockerfile
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9-slim
|
2 |
+
|
3 |
+
# Install necessary system dependencies
|
4 |
+
RUN apt-get update && apt-get install -y \
|
5 |
+
libgl1-mesa-glx \
|
6 |
+
libglib2.0-0 \
|
7 |
+
wget \
|
8 |
+
unzip \
|
9 |
+
curl \
|
10 |
+
ca-certificates \
|
11 |
+
gnupg \
|
12 |
+
fonts-liberation \
|
13 |
+
libnss3 \
|
14 |
+
libxss1 \
|
15 |
+
libappindicator1 \
|
16 |
+
libgbm-dev \
|
17 |
+
libgtk-3-0 \
|
18 |
+
gcc \
|
19 |
+
libffi-dev \
|
20 |
+
libxml2-dev \
|
21 |
+
libxslt1-dev \
|
22 |
+
libjpeg-dev \
|
23 |
+
zlib1g-dev \
|
24 |
+
&& rm -rf /var/lib/apt/lists/*
|
25 |
+
|
26 |
+
# Set the working directory
|
27 |
+
WORKDIR /code
|
28 |
+
|
29 |
+
# Create necessary directories
|
30 |
+
RUN mkdir -p /code/uploads
|
31 |
+
|
32 |
+
# Add and use a non-root user
|
33 |
+
RUN useradd -ms /bin/sh myuser
|
34 |
+
|
35 |
+
# Set ownership and permissions
|
36 |
+
RUN chown -R myuser:myuser /code && \
|
37 |
+
|
38 |
+
chmod -R 775 /code/uploads
|
39 |
+
|
40 |
+
RUN apt-get update && apt-get install -y
|
41 |
+
|
42 |
+
# Switch to non-root user
|
43 |
+
USER myuser
|
44 |
+
|
45 |
+
# Copy and install Python dependencies
|
46 |
+
COPY --chown=myuser:myuser ./requirements.txt /code/requirements.txt
|
47 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
48 |
+
|
49 |
+
# Copy the application code
|
50 |
+
COPY --chown=myuser:myuser . /code
|
51 |
+
|
52 |
+
# Default command to run the application
|
53 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify, send_from_directory
|
2 |
+
import pymysql
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
+
from gtts import gTTS
|
7 |
+
import os
|
8 |
+
import random
|
9 |
+
from ai71 import AI71
|
10 |
+
|
11 |
+
app = Flask(__name__)
|
12 |
+
'''db = pymysql.connect(host="localhost", user="root", password="1234", database="tea_farm")
|
13 |
+
cursor = db.cursor()'''
|
14 |
+
|
15 |
+
UPLOAD_FOLDER = 'uploads'
|
16 |
+
OUTPUT_DIRECTORY = 'audio'
|
17 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
18 |
+
os.makedirs(UPLOAD_FOLDER)
|
19 |
+
if not os.path.exists(OUTPUT_DIRECTORY):
|
20 |
+
os.makedirs(OUTPUT_DIRECTORY)
|
21 |
+
|
22 |
+
# Text for pest and disease
|
23 |
+
pest_text = '''tea mosquito bugs,to get rid,spray with neem oil and garlic,use sticky traps, and trap crops like castor . Expect results in 2 to 4 weeks.চা মশার বাগ, পরিত্রাণ পেতে, নিম তেল এবং রসুন দিয়ে স্প্রে করুন, আঠালো ফাঁদ ব্যবহার করুন এবং ক্যাস্টরের মতো ফসল ফাঁদ করুন। 2-4 সপ্তাহের মধ্যে ফলাফল আশা করুন।'''
|
24 |
+
disease_text = '''Detected Disease: Brown Blight is a fungal disease To treat it spray a mix of baking soda, water and apply organic mulch to keep soil healthy.ব্রাউন ব্লাইট একটি ছত্রাকজনিত রোগের চিকিৎসার জন্য বেকিং সোডা, পানির মিশ্রণ স্প্রে করুন এবং মাটি সুস্থ রাখতে জৈব মালচ প্রয়োগ করুন'''
|
25 |
+
|
26 |
+
@app.route('/home')
|
27 |
+
def home():
|
28 |
+
return render_template('index.html')
|
29 |
+
|
30 |
+
@app.route('/notification')
|
31 |
+
def notification():
|
32 |
+
return render_template('notification.html')
|
33 |
+
|
34 |
+
@app.route('/', methods=['GET', 'POST'])
|
35 |
+
def upload_file():
|
36 |
+
pest_filename = None
|
37 |
+
disease_filename = None
|
38 |
+
pest_audio_file = "pest.mp3"
|
39 |
+
disease_audio_file = "disease.mp3"
|
40 |
+
|
41 |
+
if request.method == 'POST':
|
42 |
+
if 'pest_file' in request.files:
|
43 |
+
pest_file = request.files['pest_file']
|
44 |
+
if pest_file.filename != '':
|
45 |
+
pest_filename = pest_file.filename
|
46 |
+
pest_file.save(os.path.join(UPLOAD_FOLDER, pest_filename))
|
47 |
+
tts_pest = gTTS(text=pest_text, lang='en', slow=False)
|
48 |
+
tts_pest.save(os.path.join(OUTPUT_DIRECTORY, pest_audio_file))
|
49 |
+
|
50 |
+
if 'disease_file' in request.files:
|
51 |
+
disease_file = request.files['disease_file']
|
52 |
+
if disease_file.filename != '':
|
53 |
+
disease_filename = disease_file.filename
|
54 |
+
disease_file.save(os.path.join(UPLOAD_FOLDER, disease_filename))
|
55 |
+
tts_disease = gTTS(text=disease_text, lang='en', slow=False)
|
56 |
+
tts_disease.save(os.path.join(OUTPUT_DIRECTORY, disease_audio_file))
|
57 |
+
|
58 |
+
return render_template('index.html', pest_filename=pest_filename, disease_filename=disease_filename,
|
59 |
+
pest_audio_file=pest_audio_file, disease_audio_file=disease_audio_file)
|
60 |
+
|
61 |
+
AI71_API_KEY = "api71-api-20725a9d-46d6-4baf-9e26-abfca35ab242"
|
62 |
+
@app.route('/chat', methods=['POST'])
|
63 |
+
def chat():
|
64 |
+
message = request.json['message']
|
65 |
+
ai71 = AI71(AI71_API_KEY)
|
66 |
+
response = ""
|
67 |
+
for chunk in ai71.chat.completions.create(
|
68 |
+
model="tiiuae/falcon-180b-chat",
|
69 |
+
messages=[
|
70 |
+
{"role": "system", "content": "You are a helpful assistant for a farming system.Guide the user properly to increase the yield."},
|
71 |
+
{"role": "user", "content": message},
|
72 |
+
],
|
73 |
+
stream=True,
|
74 |
+
):
|
75 |
+
if chunk.choices[0].delta.content:
|
76 |
+
response += chunk.choices[0].delta.content
|
77 |
+
return jsonify({'response': response.replace('\n','<br>')})
|
78 |
+
@app.route('/uploads/<filename>')
|
79 |
+
def uploaded_file(filename):
|
80 |
+
return send_from_directory(UPLOAD_FOLDER, filename)
|
81 |
+
|
82 |
+
@app.route('/audio/<filename>')
|
83 |
+
def audio_file(filename):
|
84 |
+
return send_from_directory(OUTPUT_DIRECTORY, filename)
|
85 |
+
|
86 |
+
@app.route('/sensors')
|
87 |
+
def sensors():
|
88 |
+
# Fetch a random row from the database
|
89 |
+
sensor_data=False
|
90 |
+
|
91 |
+
if sensor_data:
|
92 |
+
sensor_dict = {
|
93 |
+
'id': sensor_data[0],
|
94 |
+
'temperature': sensor_data[1],
|
95 |
+
'humidity': sensor_data[2],
|
96 |
+
'irValue': sensor_data[3],
|
97 |
+
'distance': sensor_data[4],
|
98 |
+
'timestamp': sensor_data[5]
|
99 |
+
}
|
100 |
+
else:
|
101 |
+
sensor_dict = {
|
102 |
+
'temperature': None,
|
103 |
+
'humidity': None,
|
104 |
+
'irValue': None,
|
105 |
+
'distance': None,
|
106 |
+
'timestamp': None
|
107 |
+
}
|
108 |
+
|
109 |
+
return render_template('sensors.html', sensor_data=sensor_dict)
|
110 |
+
|
111 |
+
@app.route('/zones')
|
112 |
+
def zones():
|
113 |
+
return render_template('zones.html')
|
114 |
+
|
115 |
+
@app.route('/activity-log')
|
116 |
+
def activity_log():
|
117 |
+
|
118 |
+
# Fetch data for IR value distribution plot
|
119 |
+
cursor.execute("SELECT ir_value FROM sensor_data")
|
120 |
+
ir_values = cursor.fetchall()
|
121 |
+
count_0 = ir_values.count((0,))
|
122 |
+
count_1 = ir_values.count((1,))
|
123 |
+
labels = ['Pest Swarm Detected', 'No Pest Swarm Detected']
|
124 |
+
sizes = [count_0, count_1]
|
125 |
+
plt.figure(figsize=(6, 6))
|
126 |
+
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
|
127 |
+
plt.title('IR Value Distribution')
|
128 |
+
plt.axis('equal')
|
129 |
+
ir_buffer = io.BytesIO()
|
130 |
+
plt.savefig(ir_buffer, format='png')
|
131 |
+
ir_buffer.seek(0)
|
132 |
+
ir_plot_data = base64.b64encode(ir_buffer.getvalue()).decode('utf-8')
|
133 |
+
plt.close()
|
134 |
+
|
135 |
+
# Fetch data for average closeness of pests plot
|
136 |
+
cursor.execute("SELECT timestamp, distance FROM sensor_data WHERE distance < 500")
|
137 |
+
data = cursor.fetchall()
|
138 |
+
timestamps = [row[0] for row in data]
|
139 |
+
distances = [row[1] for row in data]
|
140 |
+
average_distance = sum(distances) / len(distances)
|
141 |
+
plt.figure(figsize=(10, 6))
|
142 |
+
plt.plot(timestamps, distances, marker='o', linestyle='-')
|
143 |
+
plt.xlabel('Timestamp')
|
144 |
+
plt.ylabel('Distance (cm)')
|
145 |
+
plt.title('Average Closeness of Pests over Time')
|
146 |
+
plt.xticks(rotation=45)
|
147 |
+
plt.grid(True)
|
148 |
+
distance_buffer = io.BytesIO()
|
149 |
+
plt.savefig(distance_buffer, format='png')
|
150 |
+
distance_buffer.seek(0)
|
151 |
+
distance_plot_data = base64.b64encode(distance_buffer.getvalue()).decode('utf-8')
|
152 |
+
plt.close()
|
153 |
+
|
154 |
+
return render_template('activity-log.html', ir_plot_data=ir_plot_data, distance_plot_data=distance_plot_data)
|
155 |
+
|
156 |
+
@app.route('/sensor-data', methods=['POST'])
|
157 |
+
def update_sensor_data():
|
158 |
+
data = request.get_json()
|
159 |
+
temperature = data.get('temperature')
|
160 |
+
humidity = data.get('humidity')
|
161 |
+
ir_value = data.get('irValue')
|
162 |
+
distance = data.get('distance')
|
163 |
+
sql = "INSERT INTO sensor_data (temperature, humidity, ir_value, distance) VALUES (%s, %s, %s, %s)"
|
164 |
+
val = (temperature, humidity, ir_value, distance)
|
165 |
+
cursor.execute(sql, val)
|
166 |
+
db.commit()
|
167 |
+
return jsonify(success=True)
|
168 |
+
|
169 |
+
if __name__ == "__main__":
|
170 |
+
|
171 |
+
app.run(host='0.0.0.0', port=7860,debug=1==1)
|
code_flow
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
digraph {
|
2 |
+
node [fillcolor=lightblue shape=box style=filled]
|
3 |
+
line0 [label="def example_function(x):"]
|
4 |
+
line1 [label="if x < 0:"]
|
5 |
+
line1 -> line2 [label=True]
|
6 |
+
line2 [label="return 'Negative'" shape=oval]
|
7 |
+
line2 -> line3
|
8 |
+
line3 [label="elif x == 0:"]
|
9 |
+
line3 -> line4 [label=True]
|
10 |
+
line4 [label="return 'Zero'" shape=oval]
|
11 |
+
line4 -> line5
|
12 |
+
line5 [label="else:"]
|
13 |
+
line5 -> line6 [label=True]
|
14 |
+
line6 [label="return 'Positive'" shape=oval]
|
15 |
+
}
|
prasunethon.html
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Resource Consumption Monitor</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: 'Arial', sans-serif;
|
10 |
+
background-color: #f7f7f7;
|
11 |
+
margin: 0;
|
12 |
+
padding: 20px;
|
13 |
+
color: #333;
|
14 |
+
}
|
15 |
+
h1 {
|
16 |
+
text-align: center;
|
17 |
+
color: #4CAF50;
|
18 |
+
margin-bottom: 40px;
|
19 |
+
}
|
20 |
+
.resource-card {
|
21 |
+
background-color: #fff;
|
22 |
+
padding: 20px;
|
23 |
+
border-radius: 10px;
|
24 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
25 |
+
margin-bottom: 20px;
|
26 |
+
display: flex;
|
27 |
+
align-items: center;
|
28 |
+
transition: transform 0.2s ease;
|
29 |
+
}
|
30 |
+
.resource-card:hover {
|
31 |
+
transform: translateY(-5px);
|
32 |
+
}
|
33 |
+
.resource-card img {
|
34 |
+
width: 60px;
|
35 |
+
height: 60px;
|
36 |
+
margin-right: 20px;
|
37 |
+
}
|
38 |
+
.card-content {
|
39 |
+
flex: 1;
|
40 |
+
display: flex;
|
41 |
+
flex-direction: column;
|
42 |
+
justify-content: center;
|
43 |
+
}
|
44 |
+
.card-title {
|
45 |
+
font-weight: bold;
|
46 |
+
font-size: 1.2em;
|
47 |
+
color: #333;
|
48 |
+
margin-bottom: 5px;
|
49 |
+
}
|
50 |
+
.data-value {
|
51 |
+
font-size: 1em;
|
52 |
+
color: #777;
|
53 |
+
}
|
54 |
+
.chart-container {
|
55 |
+
width: 50%;
|
56 |
+
max-width: 300px;
|
57 |
+
}
|
58 |
+
</style>
|
59 |
+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
60 |
+
</head>
|
61 |
+
<body>
|
62 |
+
<h1>Resource Consumption</h1>
|
63 |
+
<div id="resource-cards"></div>
|
64 |
+
|
65 |
+
<script>
|
66 |
+
function fetchData() {
|
67 |
+
const resources = [
|
68 |
+
{ name: "Electricity", icon: "path/to/electricity.png", yesterday: 20, predicted: 710, unit: "MU(Million Units)", data: [21,24,25,22.5,27,22, 33] },
|
69 |
+
{ name: "Water", icon: "path/to/water.png", yesterday: 192, predicted: 3698, unit: "Million Liters", data: [198,193,215,206,214,200,219]},
|
70 |
+
{ name: "Gas", icon: "path/to/gas.png", yesterday: 179, predicted: 4982, unit: "kilo m³", data: [175,176,174,177,175,147,190] },
|
71 |
+
{ name: "Internet", icon: "path/to/internet.png", yesterday: 1.38, predicted: 34.9, unit: "PB", data: [1.22,1.38,1.24,1.24,1.36,1.35,2.78] },
|
72 |
+
{ name: "Waste", icon: "path/to/waste.png", yesterday: 864, predicted: 21850, unit: "metric tons", data: [874,859,847,841,853,864,845] },
|
73 |
+
// Add more resources here with their details
|
74 |
+
];
|
75 |
+
|
76 |
+
const resourceCards = document.getElementById("resource-cards");
|
77 |
+
|
78 |
+
resources.forEach((resource, index) => {
|
79 |
+
const card = document.createElement("div");
|
80 |
+
card.classList.add("resource-card");
|
81 |
+
|
82 |
+
const img = document.createElement("img");
|
83 |
+
img.src = resource.icon;
|
84 |
+
card.appendChild(img);
|
85 |
+
|
86 |
+
const cardContent = document.createElement("div");
|
87 |
+
cardContent.classList.add("card-content");
|
88 |
+
|
89 |
+
const title = document.createElement("h3");
|
90 |
+
title.classList.add("card-title");
|
91 |
+
title.textContent = resource.name;
|
92 |
+
cardContent.appendChild(title);
|
93 |
+
|
94 |
+
const yesterdayData = document.createElement("p");
|
95 |
+
yesterdayData.textContent = Yesterday: ${resource.yesterday} ${resource.unit};
|
96 |
+
cardContent.appendChild(yesterdayData);
|
97 |
+
|
98 |
+
const predictedData = document.createElement("p");
|
99 |
+
predictedData.textContent = Predicted (Next Month(July)): ${resource.predicted} ${resource.unit};
|
100 |
+
predictedData.classList.add("data-value");
|
101 |
+
cardContent.appendChild(predictedData);
|
102 |
+
|
103 |
+
card.appendChild(cardContent);
|
104 |
+
|
105 |
+
const chartContainer = document.createElement("div");
|
106 |
+
chartContainer.classList.add("chart-container");
|
107 |
+
const canvas = document.createElement("canvas");
|
108 |
+
canvas.id = chart-${index};
|
109 |
+
chartContainer.appendChild(canvas);
|
110 |
+
card.appendChild(chartContainer);
|
111 |
+
|
112 |
+
resourceCards.appendChild(card);
|
113 |
+
|
114 |
+
// Initialize the chart
|
115 |
+
const ctx = canvas.getContext('2d');
|
116 |
+
new Chart(ctx, {
|
117 |
+
type: 'line',
|
118 |
+
data: {
|
119 |
+
labels: ['Monday', 'Tuesday', 'wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
|
120 |
+
datasets: [{
|
121 |
+
label: resource.name,
|
122 |
+
data: resource.data,
|
123 |
+
backgroundColor: 'rgba(75, 192, 192, 0.2)',
|
124 |
+
borderColor: 'rgba(75, 192, 192, 1)',
|
125 |
+
borderWidth: 1,
|
126 |
+
fill: true
|
127 |
+
}]
|
128 |
+
},
|
129 |
+
options: {
|
130 |
+
scales: {
|
131 |
+
y: {
|
132 |
+
beginAtZero: false
|
133 |
+
}
|
134 |
+
}
|
135 |
+
}
|
136 |
+
});
|
137 |
+
});
|
138 |
+
}
|
139 |
+
|
140 |
+
fetchData();
|
141 |
+
</script>
|
142 |
+
</body>
|
143 |
+
</html>
|