mcp server
Browse files
app.py
CHANGED
@@ -1,11 +1,77 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
"""
|
5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -15,6 +81,10 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
|
|
|
|
|
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
for val in history:
|
@@ -35,7 +105,6 @@ def respond(
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
38 |
-
|
39 |
response += token
|
40 |
yield response
|
41 |
|
@@ -61,4 +130,4 @@ demo = gr.ChatInterface(
|
|
61 |
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from mcp import MCP
|
6 |
+
import time
|
7 |
+
import os
|
8 |
+
from datetime import datetime
|
9 |
|
10 |
"""
|
11 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
12 |
"""
|
13 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
14 |
+
mcp = MCP()
|
15 |
|
16 |
+
def detect_fire(frame):
|
17 |
+
# Convert frame to HSV color space
|
18 |
+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
19 |
+
|
20 |
+
# Define range for fire colors (red and orange)
|
21 |
+
lower_fire = np.array([0, 50, 50])
|
22 |
+
upper_fire = np.array([30, 255, 255])
|
23 |
+
|
24 |
+
# Create mask for fire colors
|
25 |
+
mask = cv2.inRange(hsv, lower_fire, upper_fire)
|
26 |
+
|
27 |
+
# Calculate percentage of fire-colored pixels
|
28 |
+
fire_percentage = (np.sum(mask > 0) / (frame.shape[0] * frame.shape[1])) * 100
|
29 |
+
|
30 |
+
return fire_percentage > 5 # Return True if more than 5% of pixels are fire-colored
|
31 |
+
|
32 |
+
def send_alert(detection_type, location):
|
33 |
+
# Get current timestamp
|
34 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
35 |
+
message = f"{detection_type} detected at {location} on {timestamp}"
|
36 |
+
|
37 |
+
# Send SMS using MCP
|
38 |
+
mcp.sms.send(
|
39 |
+
to="YOUR_PHONE_NUMBER", # Replace with actual phone number
|
40 |
+
message=message
|
41 |
+
)
|
42 |
+
|
43 |
+
# Send email using MCP
|
44 |
+
mcp.email.send(
|
45 |
+
to="YOUR_EMAIL", # Replace with actual email
|
46 |
+
subject=f"{detection_type} Alert",
|
47 |
+
body=message
|
48 |
+
)
|
49 |
+
|
50 |
+
def check_for_fire():
|
51 |
+
# Request webcam access
|
52 |
+
cap = cv2.VideoCapture(0)
|
53 |
+
if not cap.isOpened():
|
54 |
+
return "Error: Could not access webcam"
|
55 |
+
|
56 |
+
# Read a frame
|
57 |
+
ret, frame = cap.read()
|
58 |
+
if not ret:
|
59 |
+
cap.release()
|
60 |
+
return "Error: Could not read from webcam"
|
61 |
+
|
62 |
+
# Detect fire
|
63 |
+
fire_detected = detect_fire(frame)
|
64 |
+
|
65 |
+
# Release webcam
|
66 |
+
cap.release()
|
67 |
+
|
68 |
+
if fire_detected:
|
69 |
+
# Get location (you might want to implement a more sophisticated location detection)
|
70 |
+
location = "Webcam Location" # Replace with actual location detection
|
71 |
+
send_alert("Fire", location)
|
72 |
+
return f"Fire detected at {location}! Alerts have been sent."
|
73 |
+
else:
|
74 |
+
return "No fire detected"
|
75 |
|
76 |
def respond(
|
77 |
message,
|
|
|
81 |
temperature,
|
82 |
top_p,
|
83 |
):
|
84 |
+
# Check if user wants to detect fire
|
85 |
+
if "detect fire" in message.lower():
|
86 |
+
return check_for_fire()
|
87 |
+
|
88 |
messages = [{"role": "system", "content": system_message}]
|
89 |
|
90 |
for val in history:
|
|
|
105 |
top_p=top_p,
|
106 |
):
|
107 |
token = message.choices[0].delta.content
|
|
|
108 |
response += token
|
109 |
yield response
|
110 |
|
|
|
130 |
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
+
demo.launch(mcp_server=True)
|