jake2004 commited on
Commit
6ddbe7b
ยท
verified ยท
1 Parent(s): c8e92fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -92
app.py CHANGED
@@ -1,96 +1,53 @@
1
- import time
2
- import re
3
  import gradio as gr
4
- import torch
5
- from transformers import pipeline
6
- from requests_html import HTMLSession
7
- import speech_recognition as sr
8
 
9
- # Load NLP Model with GPU support
10
- classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli", device=0 if torch.cuda.is_available() else -1)
11
 
12
- # Define possible intents
13
- possible_intents = [
14
- "open google",
15
- "search something",
16
- "go to amazon",
17
- "order something"
18
- ]
19
-
20
- # Function to classify user command
21
- def get_intent(command):
22
- result = classifier(command, possible_intents)
23
- intent = result["labels"][0]
24
- return intent.lower()
25
-
26
- # Function to fetch search results
27
- def fetch_search_results(url):
28
- session = HTMLSession()
29
- response = session.get(url)
30
- title = response.html.find("title", first=True).text if response.html.find("title", first=True) else "No title found"
31
- return title
32
-
33
- # Function to execute commands
34
- def execute_command(command):
35
- intent = get_intent(command)
36
- output_log = f"Command: {command}\nDetected Intent: {intent}\n"
37
-
38
- if "open google" in intent:
39
- output_log += "โœ… Google homepage ready.\n"
40
-
41
- elif "search something" in intent:
42
- query = re.search(r'search for (.+)', command)
43
- if query:
44
- search_term = query.group(1)
45
- url = f"https://www.google.com/search?q={search_term}"
46
- title = fetch_search_results(url)
47
- output_log += f"๐Ÿ” Searched for: {search_term}. Found: {title}\n"
48
-
49
- elif "go to amazon" in intent:
50
- output_log += "๐Ÿ›’ Amazon homepage ready.\n"
51
-
52
- elif "order something" in intent:
53
- product = re.search(r'order (.+)', command)
54
- if product:
55
- search_term = product.group(1)
56
- url = f"https://www.amazon.com/s?k={search_term}"
57
- title = fetch_search_results(url)
58
- output_log += f"๐Ÿ›๏ธ Ordered: {search_term}. Found: {title}\n"
59
- else:
60
- output_log += "โš ๏ธ Command not recognized.\n"
61
-
62
- return output_log
63
-
64
- # Voice command recognition
65
- def listen_for_command():
66
- recognizer = sr.Recognizer()
67
- with sr.Microphone() as source:
68
- print("Listening for commands...")
69
- audio = recognizer.listen(source)
70
- try:
71
- command = recognizer.recognize_google(audio)
72
- print(f"Command recognized: {command}")
73
- return execute_command(command)
74
- except sr.UnknownValueError:
75
- return "Could not understand the command."
76
- except sr.RequestError:
77
- return "Error with the speech recognition service."
78
-
79
- # Gradio Interface
80
- with gr.Blocks() as ui:
81
- gr.Markdown("# ๐Ÿ”ฅ Web Automation with NLP + Gradio (GPU Accelerated)")
82
-
83
- with gr.Row():
84
- text_input = gr.Textbox(label="Enter Command", placeholder="Example: Search for iPhone on Google")
85
- submit_btn = gr.Button("Execute")
86
-
87
- with gr.Row():
88
- mic_btn = gr.Button("๐ŸŽค Voice Command")
89
 
90
- output_box = gr.Textbox(label="Execution Log", lines=5)
91
-
92
- submit_btn.click(execute_command, inputs=text_input, outputs=output_box)
93
- mic_btn.click(listen_for_command, outputs=output_box)
94
-
95
- # Launch Gradio App
96
- ui.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
 
2
  import gradio as gr
3
+ from ultralytics import YOLO
 
 
 
4
 
5
+ # Load YOLOv8 Model
6
+ model = YOLO("yolov8n.pt") # Use 'yolov8s.pt' for better accuracy
7
 
8
+ def detect_cars(video_url):
9
+ cap = cv2.VideoCapture(video_url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ while cap.isOpened():
12
+ ret, frame = cap.read()
13
+ if not ret:
14
+ break
15
+
16
+ # Perform YOLO detection
17
+ results = model(frame)
18
+
19
+ # Initialize car count
20
+ car_count = 0
21
+
22
+ # Loop through detections and count cars
23
+ for result in results:
24
+ for box in result.boxes:
25
+ class_id = int(box.cls[0])
26
+ if class_id in [2, 3, 5, 7]: # Car-related classes
27
+ car_count += 1
28
+
29
+ # Draw results on frame
30
+ frame = results[0].plot()
31
+
32
+ # Display car count on screen
33
+ cv2.putText(frame, f'Cars: {car_count}', (20, 50),
34
+ cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
35
+
36
+ # Convert frame to RGB for display in Hugging Face
37
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
38
+
39
+ yield frame
40
+
41
+ cap.release()
42
+
43
+ # Hugging Face Gradio UI
44
+ iface = gr.Interface(
45
+ fn=detect_cars,
46
+ inputs="text",
47
+ outputs="image",
48
+ live=True,
49
+ title="YOLOv8 Live Car Detection",
50
+ description="Enter the IP Webcam URL to detect and count cars in real-time."
51
+ )
52
+
53
+ iface.launch()