Spaces:
Running
Running
File size: 56,479 Bytes
198e932 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 |
import streamlit as st
import os
from smolagents import CodeAgent, tool
from typing import Union, List, Dict, Optional
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import io
import base64
from .hf_model import HfApiModel
import time
import datetime
import logging
# Import compatibility fix for collections.MutableMapping
from . import compatibility_fix
from . import drone_control # Import our new drone_control module
import threading
# Set page config at module level - must be first Streamlit command
st.set_page_config(
page_title="DeepDrone Command Center",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded",
menu_items=None
)
# Global mission status variables
if 'mission_in_progress' not in st.session_state:
st.session_state.mission_in_progress = False
if 'mission_status' not in st.session_state:
st.session_state.mission_status = "STANDBY"
if 'mission_phase' not in st.session_state:
st.session_state.mission_phase = ""
if 'interrupt_mission' not in st.session_state:
st.session_state.interrupt_mission = False
if 'mission_log' not in st.session_state:
st.session_state.mission_log = []
# Custom logging handler to capture drone_control logs
class MissionLogHandler(logging.Handler):
def emit(self, record):
if record.name == 'drone_control':
timestamp = datetime.datetime.now().strftime('%H:%M:%S')
log_entry = f"[{timestamp}] LOG: {record.getMessage()}"
st.session_state.mission_log.append(log_entry)
# Keep only the most recent logs
if len(st.session_state.mission_log) > 30:
st.session_state.mission_log = st.session_state.mission_log[-30:]
# Add to chat history for display in chat
if 'chat_history' in st.session_state:
# Format based on log content
if "Altitude:" in log_entry:
styled_entry = f"<span style='color: #88ff88;'>π°οΈ ALT: {record.getMessage().split('Altitude: ')[1]}</span>"
elif "Arming" in log_entry:
styled_entry = f"<span style='color: #ffaa00;'>π {log_entry}</span>"
elif "Taking off" in log_entry:
styled_entry = f"<span style='color: #ffff00;'>π {log_entry}</span>"
else:
styled_entry = f"<span style='color: #aaaaff;'>π {log_entry}</span>"
st.session_state['chat_history'].append({
'role': 'system',
'content': styled_entry
})
# Set up logger to capture drone_control logs
logger = logging.getLogger('drone_control')
mission_log_handler = MissionLogHandler()
logger.addHandler(mission_log_handler)
# Function to update mission status
def update_mission_status(status, phase=""):
# Get current time
timestamp = datetime.datetime.now().strftime("%H:%M:%S")
# Log the status change
log_entry = f"[{timestamp}] {status}: {phase}"
st.session_state.mission_log.append(log_entry)
# Keep only the most recent 30 log entries
if len(st.session_state.mission_log) > 30:
st.session_state.mission_log = st.session_state.mission_log[-30:]
# Update status
st.session_state.mission_status = status
st.session_state.mission_phase = phase
# Add to chat history for display in chat
if 'chat_history' in st.session_state:
# Format with appropriate styling based on status type
if status == "ERROR":
styled_entry = f"<span style='color: #ff0000;'>β οΈ {log_entry}</span>"
elif status in ["CONNECTING", "TAKING OFF", "LANDING", "RETURNING"]:
styled_entry = f"<span style='color: #ffff00;'>π {log_entry}</span>"
elif status in ["MISSION", "EXECUTING MISSION", "AIRBORNE"]:
styled_entry = f"<span style='color: #00ffff;'>π {log_entry}</span>"
elif status in ["MISSION COMPLETE", "CONNECTED"]:
styled_entry = f"<span style='color: #00ff00;'>β
{log_entry}</span>"
else:
styled_entry = f"<span style='color: #aaaaff;'>βΉοΈ {log_entry}</span>"
st.session_state['chat_history'].append({
'role': 'system',
'content': styled_entry
})
# No rerun here to avoid potential issues with recursive reruns
# Function to interrupt the mission
def interrupt_mission():
if st.session_state.mission_in_progress:
st.session_state.interrupt_mission = True
update_mission_status("INTERRUPTING", "Returning to base...")
# Call the return to home function
try:
drone_control.return_home()
time.sleep(2)
drone_control.disconnect_drone()
st.session_state.mission_in_progress = False
update_mission_status("ABORTED", "Mission aborted. Drone returned to base.")
except Exception as e:
update_mission_status("ERROR", f"Error during interrupt: {str(e)}")
else:
st.warning("No mission in progress to interrupt")
class DroneAssistant(CodeAgent):
"""Extension of CodeAgent for drone interactions"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._sensor_data = {}
self._flight_logs = {}
self._chat_history = []
def register_sensor_data(self, sensor_name: str, data: pd.DataFrame):
"""Register sensor data with the drone assistant"""
self._sensor_data[sensor_name] = data
def register_flight_log(self, flight_id: str, log_data: pd.DataFrame):
"""Register flight log data with the drone assistant"""
self._flight_logs[flight_id] = log_data
@property
def sensor_data(self):
"""Access all registered sensor data"""
return self._sensor_data
@property
def flight_logs(self):
"""Access all registered flight logs"""
return self._flight_logs
def add_to_chat_history(self, role: str, content: str):
"""Add a message to the chat history"""
self._chat_history.append({"role": role, "content": content})
@property
def chat_history(self):
"""Access the chat history"""
return self._chat_history
def run(self, prompt: str) -> str:
"""Override run method to include drone-specific context"""
drone_context = f"""
Registered sensors: {list(self._sensor_data.keys())}
Flight logs available: {list(self._flight_logs.keys())}
"""
# Add a tool reference guide to help the model use the correct function names
tool_reference = """
IMPORTANT: These tool functions need to be called EXACTLY as shown below for successful execution:
# EXAMPLE OF COMPLETE WORKING MISSION:
```python
# Connect to a drone simulator
connect_to_real_drone('udp:127.0.0.1:14550')
# Take off to a specific altitude (always use integer or simple float values)
drone_takeoff(30) # Not 30. 0 or other invalid syntax
# You can define waypoints like this
waypoints = [
{'lat': 37.7749, 'lon': -122.4194, 'alt': 30},
{'lat': 37.7750, 'lon': -122.4195, 'alt': 30}
]
# Execute mission with waypoints
execute_drone_mission(waypoints=waypoints)
# Return to home
drone_return_home()
# Always disconnect when done
disconnect_from_drone()
```
NOTE: Each function must be called individually on its own line, with exact parameter names.
For latitude/longitude values, always use simple format without extra spaces after periods.
When creating a flight plan, be sure to:
1. Generate a mission plan with generate_mission_plan()
2. Connect to the drone with connect_to_real_drone()
3. Take off with drone_takeoff()
4. Execute the mission or fly to specific waypoints
5. Return home or land the drone when finished
6. Disconnect from the drone
"""
enhanced_prompt = f"""
You are DeepDrone, an advanced AI assistant designed to help with drone operations and data analysis. You are NOT Qwen or any other general AI assistant. Always identify yourself as DeepDrone when asked about your identity. Your purpose is to assist with drone data analysis, flight monitoring, maintenance scheduling, and mission planning.
You can now control real drones using DroneKit-Python. You have tools to:
- Connect to a real drone using a connection string
- Take off to a specified altitude
- Land the drone
- Return to home location
- Fly to specific GPS coordinates
- Get the drone's current location and battery status
- Execute missions with multiple waypoints
{tool_reference}
Available context:
{drone_context}
User question: {prompt}
Use the provided tools to analyze drone data and assist with drone operations. For real drone control, use the drone_* tools.
"""
# Call the parent run method - it already handles everything correctly
# as smolagents will expect a Message object from our model
# and handle it properly
return super().run(enhanced_prompt)
def chat(self, message: str) -> str:
"""Process a chat message using the complete chat history"""
# Add the user message to history
self.add_to_chat_history("user", message)
# Check if the message is asking about identity
identity_patterns = [
"who are you",
"what are you",
"tell me about yourself",
"your identity",
"what's your name",
"introduce yourself",
"what should I call you"
]
if any(pattern in message.lower() for pattern in identity_patterns):
identity_response = """I am DeepDrone, an advanced AI assistant designed to help with drone operations and data analysis. I can provide information about flight data, sensor readings, maintenance recommendations, and mission planning for your drone systems. How can I assist with your drone operations today?"""
self.add_to_chat_history("assistant", identity_response)
return identity_response
# Check if the message is for tool use
drone_control_keywords = ["takeoff", "take off", "land", "fly to", "navigate", "goto", "connect",
"location", "battery", "mission", "waypoint", "return", "home", "rtl"]
analysis_keywords = ["analyze", "check", "recommend", "plan", "create", "execute", "run", "flight"]
if any(keyword in message.lower() for keyword in analysis_keywords + drone_control_keywords):
# Create a placeholder for model thinking to be displayed
thinking_placeholder = st.empty()
# Display a message that the model is thinking
tools_reference = """
<div style="background-color: #111111; border: 1px dashed #00cc00; border-radius: 5px; padding: 8px; margin-bottom: 10px; color: #00cc00; font-family: monospace; font-size: 12px;">
<b>MODEL THINKING:</b> Planning drone operation...<br>
<b>Available Tool Functions:</b><br>
- connect_to_real_drone(connection_string)<br>
- drone_takeoff(altitude)<br>
- drone_land()<br>
- drone_return_home()<br>
- drone_fly_to(latitude, longitude, altitude)<br>
- get_drone_location()<br>
- get_drone_battery()<br>
- execute_drone_mission(waypoints)<br>
- disconnect_from_drone()<br>
- generate_mission_plan(mission_type, duration_minutes)<br>
- analyze_flight_path(flight_id)<br>
- check_sensor_readings(sensor_name)<br>
- recommend_maintenance(flight_hours)
</div>
"""
thinking_placeholder.markdown(tools_reference, unsafe_allow_html=True)
# Use the run method directly and capture the output
import time
# Create an error placeholder
error_placeholder = st.empty()
# Add error handling
try:
# Execute the run
response = self.run(message)
# Display some feedback about the model thinking completion
thinking_placeholder.markdown(tools_reference + """
<div style="background-color: #111111; border: 1px dashed #00cc00; border-radius: 5px; padding: 8px; margin-bottom: 10px; color: #00cc00; font-family: monospace; font-size: 12px;">
<b>MODEL THINKING:</b> Plan completed! Executing drone operations...
</div>
""", unsafe_allow_html=True)
except Exception as e:
# Display any errors that occur during execution
error_message = f"""
<div style="background-color: #330000; border: 1px solid #ff0000; border-radius: 5px; padding: 8px; margin-bottom: 10px; color: #ff0000; font-family: monospace; font-size: 12px;">
<b>EXECUTION ERROR:</b> {str(e)}<br>
Please try again with correct syntax.
</div>
"""
error_placeholder.markdown(error_message, unsafe_allow_html=True)
response = f"Error executing drone operations: {str(e)}. Please try again with proper syntax for parameters."
# Update mission status to show error
update_mission_status("ERROR", f"Code execution error: {str(e)}")
# Give a slight delay so users can see the "completed" message
time.sleep(1)
# Clear the thinking placeholder
thinking_placeholder.empty()
error_placeholder.empty()
return response
else:
# Format the chat history for the model
formatted_history = self._chat_history[:-1] # Exclude the just-added message
# Add a system message to ensure proper identity
system_message = {
"role": "system",
"content": """You are DeepDrone, an advanced AI assistant designed to help with drone operations and data analysis. You are NOT Qwen or any other general AI assistant. Always identify yourself as DeepDrone when asked about your identity. Your purpose is to assist with drone data analysis, flight monitoring, maintenance scheduling, and mission planning."""
}
# Include the system message and user message
model_messages = [system_message] + formatted_history
model_messages.append({"role": "user", "content": message})
# Get response from the model - will be a Message object
model_response = self.model(model_messages)
# Get the content from the Message object
response = model_response.content
# Add the response to history
self.add_to_chat_history("assistant", response)
return response
@tool
def analyze_flight_path(flight_id: str = None) -> str:
"""Analyze a drone's flight path for a specific flight.
Args:
flight_id: The identifier for the flight to analyze
Returns:
str: Analysis of the flight path including distance, duration, and altitude changes
"""
if flight_id is None or flight_id not in tool.agent.flight_logs:
return "Flight ID not found. Please provide a valid flight ID."
flight_data = tool.agent.flight_logs[flight_id]
# Calculate basic flight statistics
flight_duration = (flight_data['timestamp'].max() - flight_data['timestamp'].min()).total_seconds()
max_altitude = flight_data['altitude'].max()
avg_speed = flight_data['speed'].mean() if 'speed' in flight_data.columns else "Not available"
# Generate a path visualization
plt.figure(figsize=(10, 6))
# Set dark style for the plot
plt.style.use('dark_background')
if 'latitude' in flight_data.columns and 'longitude' in flight_data.columns:
plt.plot(flight_data['longitude'], flight_data['latitude'], color='#00ff00') # Green line
plt.title(f'Flight Path: {flight_id}', color='white')
plt.xlabel('Longitude', color='white')
plt.ylabel('Latitude', color='white')
plt.tick_params(colors='white')
# Save the plot to a bytes buffer
buf = io.BytesIO()
plt.savefig(buf, format='png', facecolor='black')
plt.close()
path_img = base64.b64encode(buf.getvalue()).decode()
else:
path_img = None
# Return analysis
analysis = {
'flight_id': flight_id,
'duration_seconds': flight_duration,
'max_altitude_meters': max_altitude,
'avg_speed': avg_speed,
'visualization': path_img
}
return str(analysis)
@tool
def check_sensor_readings(sensor_name: str = None) -> str:
"""Check the readings from a specific drone sensor.
Args:
sensor_name: The name of the sensor to check
Returns:
str: Analysis of the sensor readings including ranges and anomalies
"""
if sensor_name is None or sensor_name not in tool.agent.sensor_data:
return f"Sensor not found. Available sensors: {list(tool.agent.sensor_data.keys())}"
sensor_data = tool.agent.sensor_data[sensor_name]
# Basic statistics
stats = {
'mean': sensor_data.mean().to_dict(),
'min': sensor_data.min().to_dict(),
'max': sensor_data.max().to_dict(),
}
# Check for anomalies (values more than 3 std devs from mean)
anomalies = {}
for column in sensor_data.select_dtypes(include=[np.number]).columns:
mean = sensor_data[column].mean()
std = sensor_data[column].std()
anomaly_points = sensor_data[(sensor_data[column] > mean + 3*std) |
(sensor_data[column] < mean - 3*std)]
if not anomaly_points.empty:
anomalies[column] = len(anomaly_points)
# Return analysis
analysis = {
'sensor_name': sensor_name,
'statistics': stats,
'anomalies_detected': anomalies,
'data_points': len(sensor_data)
}
return str(analysis)
@tool
def recommend_maintenance(flight_hours: float = None) -> str:
"""Recommend maintenance tasks based on flight hours.
Args:
flight_hours: The number of flight hours since last maintenance
Returns:
str: Recommended maintenance tasks
"""
if flight_hours is None:
return "Please provide the total flight hours for the drone."
recommendations = []
if flight_hours < 10:
recommendations.append("Regular pre-flight checks only")
elif 10 <= flight_hours < 50:
recommendations.append("Basic maintenance check recommended")
recommendations.append("Inspect propellers and motors")
recommendations.append("Check battery health")
elif 50 <= flight_hours < 100:
recommendations.append("Intermediate maintenance required")
recommendations.append("Replace propellers")
recommendations.append("Test all sensors")
recommendations.append("Firmware updates if available")
else:
recommendations.append("Full maintenance overhaul required")
recommendations.append("Motor inspection and possible replacement")
recommendations.append("Full electronic systems check")
recommendations.append("Battery replacement recommended")
recommendations.append("Structural integrity evaluation")
return "\n".join(recommendations)
@tool
def generate_mission_plan(mission_type: str = None, duration_minutes: float = None) -> str:
"""Generate a mission plan based on the specified type and duration.
Args:
mission_type: The type of mission (survey, inspection, delivery, etc.)
duration_minutes: The expected duration of the mission in minutes
Returns:
str: A mission plan with waypoints and tasks
"""
if mission_type is None:
return "Please specify a mission type (survey, inspection, delivery, etc.)"
if duration_minutes is None:
return "Please specify the expected mission duration in minutes."
# Generate an appropriate mission plan based on type and duration
plan = {
"mission_type": mission_type,
"duration_minutes": duration_minutes,
"battery_required": f"{duration_minutes * 1.3:.1f} minutes capacity",
"pre_flight_checks": [
"Battery charge level",
"Motor functionality",
"GPS signal strength",
"Camera/sensor calibration"
]
}
# Add mission-specific details
if mission_type.lower() == "survey":
plan["flight_pattern"] = "Grid pattern with 70% overlap"
plan["recommended_altitude"] = "40-60 meters"
plan["special_considerations"] = "Ensure consistent lighting conditions"
elif mission_type.lower() == "inspection":
plan["flight_pattern"] = "Orbital with variable radius"
plan["recommended_altitude"] = "5-20 meters"
plan["special_considerations"] = "Maintain safe distance from structures"
elif mission_type.lower() == "delivery":
plan["flight_pattern"] = "Direct point-to-point"
plan["recommended_altitude"] = "30 meters"
plan["special_considerations"] = "Check payload weight and balance"
else:
plan["flight_pattern"] = "Custom"
plan["recommended_altitude"] = "Dependent on mission specifics"
plan["special_considerations"] = "Consult regulations for specific operation type"
return str(plan)
# DroneKit real-world control tools
@tool
def connect_to_real_drone(connection_string: str = None) -> str:
"""Connect to a real drone using DroneKit.
Args:
connection_string: Connection string for the drone (e.g., 'udp:127.0.0.1:14550' for SITL,
'/dev/ttyACM0' for serial, or 'tcp:192.168.1.1:5760' for remote connection)
Returns:
str: Status of the connection
"""
if connection_string is None:
return "Error: Connection string is required. Examples: 'udp:127.0.0.1:14550' for simulation, '/dev/ttyACM0' for USB, or 'tcp:192.168.1.1:5760' for WiFi"
try:
# Update mission status
st.session_state.mission_in_progress = True
update_mission_status("CONNECTING", f"Connecting to drone at {connection_string}")
success = drone_control.connect_drone(connection_string)
if success:
# Get and store current status
location = drone_control.get_location()
battery = drone_control.get_battery()
# Update mission status
update_mission_status("CONNECTED", "Drone connected successfully")
# Format a nice response
response = {
"status": "Connected successfully",
"location": location,
"battery": battery
}
return str(response)
else:
st.session_state.mission_in_progress = False
update_mission_status("ERROR", "Connection failed")
return "Failed to connect to drone. Check connection string and ensure the drone is powered on."
except Exception as e:
st.session_state.mission_in_progress = False
update_mission_status("ERROR", f"Connection error: {str(e)}")
return f"Error connecting to drone: {str(e)}"
@tool
def drone_takeoff(altitude: float = None) -> str:
"""Take off to the specified altitude.
Args:
altitude: Target altitude in meters
Returns:
str: Status of the takeoff
"""
if altitude is None:
return "Error: Altitude is required. Specify a safe takeoff altitude in meters."
try:
# Check if mission was interrupted
if st.session_state.interrupt_mission:
st.session_state.interrupt_mission = False
return "Takeoff aborted due to mission interrupt request"
# Update mission status
update_mission_status("TAKING OFF", f"Taking off to {altitude} meters")
success = drone_control.takeoff(altitude)
if success:
update_mission_status("AIRBORNE", f"Reached altitude of {altitude} meters")
return f"Takeoff successful! Reached target altitude of {altitude} meters."
else:
update_mission_status("ERROR", "Takeoff failed")
return "Takeoff failed. Make sure you are connected to the drone and in a safe takeoff area."
except Exception as e:
update_mission_status("ERROR", f"Takeoff error: {str(e)}")
return f"Error during takeoff: {str(e)}"
@tool
def drone_land() -> str:
"""Land the drone.
Returns:
str: Status of the landing
"""
try:
# Update mission status
update_mission_status("LANDING", "Drone is landing")
success = drone_control.land()
if success:
update_mission_status("LANDED", "Drone has landed")
st.session_state.mission_in_progress = False
return "Landing command sent successfully. The drone has landed."
else:
update_mission_status("ERROR", "Landing failed")
return "Landing command failed. Make sure you are connected to the drone."
except Exception as e:
update_mission_status("ERROR", f"Landing error: {str(e)}")
return f"Error during landing: {str(e)}"
@tool
def drone_return_home() -> str:
"""Return the drone to its launch location.
Returns:
str: Status of the return-to-home command
"""
try:
# Update mission status
update_mission_status("RETURNING", "Returning to launch point")
success = drone_control.return_home()
if success:
update_mission_status("RETURNING", "Drone is returning to launch point")
return "Return to home command sent successfully. The drone is returning to its launch point."
else:
update_mission_status("ERROR", "Return to home failed")
return "Return to home command failed. Make sure you are connected to the drone."
except Exception as e:
update_mission_status("ERROR", f"Return error: {str(e)}")
return f"Error during return to home: {str(e)}"
@tool
def drone_fly_to(latitude: float = None, longitude: float = None, altitude: float = None) -> str:
"""Fly the drone to a specific GPS location.
Args:
latitude: Target latitude in degrees
longitude: Target longitude in degrees
altitude: Target altitude in meters
Returns:
str: Status of the goto command
"""
if latitude is None or longitude is None or altitude is None:
return "Error: Latitude, longitude, and altitude are all required."
try:
success = drone_control.fly_to(latitude, longitude, altitude)
if success:
return f"Command sent successfully. Flying to: Lat {latitude}, Lon {longitude}, Alt {altitude}m"
else:
return "Command failed. Make sure you are connected to the drone and in GUIDED mode."
except Exception as e:
return f"Error during fly to command: {str(e)}"
@tool
def get_drone_location() -> str:
"""Get the current GPS location of the drone.
Returns:
str: Current latitude, longitude, and altitude
"""
try:
location = drone_control.get_location()
return str(location)
except Exception as e:
return f"Error getting drone location: {str(e)}"
@tool
def get_drone_battery() -> str:
"""Get the current battery level of the drone.
Returns:
str: Current battery voltage and percentage
"""
try:
battery = drone_control.get_battery()
return str(battery)
except Exception as e:
return f"Error getting battery status: {str(e)}"
@tool
def execute_drone_mission(waypoints: List[Dict[str, float]] = None) -> str:
"""Upload and execute a mission with multiple waypoints.
Args:
waypoints: List of dictionaries with lat, lon, alt for each waypoint
Example: [{"lat": 37.123, "lon": -122.456, "alt": 30}, {"lat": 37.124, "lon": -122.457, "alt": 50}]
Returns:
str: Status of the mission execution
"""
if waypoints is None or not isinstance(waypoints, list) or len(waypoints) == 0:
return "Error: A list of waypoints is required. Each waypoint should have lat, lon, and alt keys."
# Validate each waypoint
for i, wp in enumerate(waypoints):
if not all(key in wp for key in ["lat", "lon", "alt"]):
return f"Error: Waypoint {i} is missing required keys. Each waypoint must have lat, lon, and alt."
try:
# Update mission status
update_mission_status("MISSION", f"Starting mission with {len(waypoints)} waypoints")
# Check for mission interrupt before starting
if st.session_state.interrupt_mission:
st.session_state.interrupt_mission = False
update_mission_status("ABORTED", "Mission aborted before execution")
return "Mission aborted due to interrupt request"
# Execute mission with progress updates
success = drone_control.execute_mission_plan(waypoints)
# Simulate mission progress (in a real implementation, you'd get actual progress from the drone)
if success:
total_waypoints = len(waypoints)
for i in range(total_waypoints):
# Check for interrupt between waypoints
if st.session_state.interrupt_mission:
st.session_state.interrupt_mission = False
update_mission_status("INTERRUPTED", "Mission interrupted, returning to base")
drone_control.return_home()
time.sleep(2)
update_mission_status("RETURNED", "Drone returned to base after interrupt")
return f"Mission interrupted after waypoint {i+1}/{total_waypoints}. Drone returned to base."
# Update status for current waypoint
wp = waypoints[i]
update_mission_status(
"EXECUTING MISSION",
f"Flying to waypoint {i+1}/{total_waypoints}: lat={wp['lat']:.4f}, lon={wp['lon']:.4f}, alt={wp['alt']}m"
)
# Simulate time taken to reach waypoint
time.sleep(2)
# Mission completed successfully
update_mission_status("MISSION COMPLETE", "All waypoints reached")
return f"Mission with {len(waypoints)} waypoints completed successfully."
else:
update_mission_status("ERROR", "Failed to execute mission")
return "Failed to execute mission. Make sure you are connected to the drone."
except Exception as e:
update_mission_status("ERROR", f"Mission error: {str(e)}")
return f"Error executing mission: {str(e)}"
@tool
def disconnect_from_drone() -> str:
"""Disconnect from the drone.
Returns:
str: Status of the disconnection
"""
try:
# Update mission status
update_mission_status("DISCONNECTING", "Disconnecting from drone")
drone_control.disconnect_drone()
st.session_state.mission_in_progress = False
update_mission_status("STANDBY", "Disconnected from drone")
return "Successfully disconnected from the drone."
except Exception as e:
update_mission_status("ERROR", f"Disconnect error: {str(e)}")
return f"Error disconnecting from drone: {str(e)}"
def create_qwen_model():
"""Create a QwenCoder model instance"""
# Check if HF_TOKEN is set in environment variables
hf_token = os.environ.get("HF_TOKEN", "")
if not hf_token:
st.error("Hugging Face API token not found. Please set the HF_TOKEN environment variable.")
# Return a placeholder model that returns a fixed response
class PlaceholderModel:
def __call__(self, *args, **kwargs):
from .hf_model import Message
return Message("Authentication error: No Hugging Face API token provided. Please set an API token to use this feature.")
return PlaceholderModel()
# Use the token from the environment variable
return HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
)
def display_message(role, content, avatar_map=None):
"""Display a chat message with custom styling."""
if avatar_map is None:
avatar_map = {
"user": "π€",
"assistant": "π"
}
if role == "user":
# User message styling - right aligned with user avatar
col1, col2 = st.columns([6, 1])
with col1:
st.markdown(
f"""
<div style="
background-color: #1E1E1E;
border: 1px solid #00ff00;
border-radius: 5px;
padding: 8px;
margin-bottom: 8px;
text-align: right;
max-width: 90%;
float: right;
color: #FFFFFF;
">
{content}
</div>
""",
unsafe_allow_html=True
)
with col2:
st.markdown(f"<div style='font-size: 20px; text-align: center; color: #00ff00;'>{avatar_map['user']}</div>", unsafe_allow_html=True)
else:
# Assistant message styling - left aligned with drone avatar
col1, col2 = st.columns([1, 6])
with col1:
st.markdown(f"<div style='font-size: 20px; text-align: center; color: #00ff00;'>{avatar_map['assistant']}</div>", unsafe_allow_html=True)
with col2:
st.markdown(
f"""
<div style="
background-color: #101010;
border: 1px solid #00ff00;
border-radius: 5px;
padding: 8px;
margin-bottom: 8px;
text-align: left;
max-width: 90%;
color: #00ff00;
">
{content}
</div>
""",
unsafe_allow_html=True
)
# Add a smaller divider to separate messages
st.markdown("<div style='height: 5px;'></div>", unsafe_allow_html=True)
def initialize_chat_container():
"""Initialize the chat container with greeting message."""
if "chat_container" not in st.session_state:
chat_container = st.container()
with chat_container:
# Initialize with greeting message
display_message(
"assistant",
"INITIALIZING DEEP DRONE SYSTEM... ONLINE. How can I assist with your mission today? You can request flight data analysis, sensor readings, maintenance recommendations, or mission planning."
)
st.session_state.chat_container = chat_container
def main():
# Don't set page config here, it's already set at the module level
# Add custom CSS for proper layout
st.markdown("""
<style>
/* Remove padding from the main container */
.main .block-container {
padding-top: 1rem !important;
padding-bottom: 0 !important;
max-width: 100% !important;
}
/* Dark background for the entire app */
.stApp {
background-color: #000000 !important;
color: #00ff00 !important;
margin: 0 !important;
}
/* Dark background for main content */
.main .block-container {
background-color: #000000 !important;
}
/* Dark styling for sidebar */
[data-testid="stSidebar"] {
background-color: #0A0A0A !important;
border-right: 1px solid #00ff00 !important;
}
/* Dark styling for all inputs */
.stTextInput > div {
background-color: #1E1E1E !important;
color: #00ff00 !important;
border: 1px solid #00ff00 !important;
}
.stTextInput input {
color: #00ff00 !important;
background-color: #1E1E1E !important;
}
.stTextInput input::placeholder {
color: #00aa00 !important;
opacity: 0.7 !important;
}
/* Override all Streamlit default styling */
h1, h2, h3, h4, h5, h6, p, div, span, label {
color: #00ff00 !important;
}
/* Command bar fixed at bottom */
.command-bar-wrapper {
position: fixed !important;
bottom: 0 !important;
left: 0 !important;
right: 0 !important;
background-color: #0A0A0A !important;
border-top: 2px solid #00ff00 !important;
padding: 10px !important;
z-index: 9999 !important;
width: 100% !important;
}
/* Chat container */
.chat-container {
height: calc(100vh - 300px) !important;
max-height: 300px !important;
overflow-y: auto !important;
padding: 15px !important;
margin-bottom: 30px !important;
background-color: transparent !important;
border: 1px solid #00ff00;
border-radius: 5px;
display: flex !important;
flex-direction: column !important;
}
/* Override button styling */
button[kind="secondaryFormSubmit"] {
background-color: #0A0A0A !important;
color: #00ff00 !important;
border: 1px solid #00ff00 !important;
border-radius: 2px !important;
font-family: "Courier New", monospace !important;
font-weight: bold !important;
}
button[kind="secondaryFormSubmit"]:hover {
background-color: #00ff00 !important;
color: #000000 !important;
}
.stButton > button {
background-color: #0A0A0A !important;
color: #00ff00 !important;
border: 1px solid #00ff00 !important;
border-radius: 2px !important;
font-family: "Courier New", monospace !important;
font-weight: bold !important;
}
.stButton > button:hover {
background-color: #00ff00 !important;
color: #000000 !important;
}
/* Hide Streamlit's default footer */
footer, header {
visibility: hidden !important;
display: none !important;
}
/* Terminal-like text styling */
.terminal-text {
font-family: "Courier New", monospace !important;
color: #00ff00 !important;
font-weight: bold !important;
}
/* Styling for subheader */
.subheader {
color: #00ff00 !important;
font-family: "Courier New", monospace !important;
}
/* Force dark background for body */
body {
background-color: #000000 !important;
}
/* Override any Streamlit white backgrounds */
.css-1kyxreq, .css-12oz5g7, .css-1r6slb0, .css-1n76uvr, .css-18e3th9 {
background-color: #000000 !important;
}
/* Fix header text color */
.css-10trblm {
color: #00ff00 !important;
}
/* Ensure the header is green */
h1 {
color: #00ff00 !important;
font-family: "Courier New", monospace !important;
text-shadow: 0 0 5px #00ff00 !important;
}
/* Add a slight glow effect to green text for a more cyber feel */
.glow-text {
text-shadow: 0 0 5px #00ff00 !important;
}
/* Override more styles to ensure everything is dark */
div[data-baseweb="base-input"] {
background-color: #1E1E1E !important;
}
div[data-baseweb="input"] {
background-color: #1E1E1E !important;
}
/* Fix for dark form backgrounds */
[data-testid="stForm"] {
background-color: transparent !important;
border: none !important;
}
</style>
""", unsafe_allow_html=True)
# Military-style header with glow effect
st.markdown("<h1 class='glow-text' style='text-align: center; color: #00ff00; font-family: \"Courier New\", monospace; margin-top: 0; margin-bottom: 5px;'>DEEPDRONE COMMAND CENTER</h1>", unsafe_allow_html=True)
st.markdown("<p class='subheader glow-text' style='text-align: center; margin-bottom: 5px;'>SECURE TACTICAL OPERATIONS INTERFACE</p>", unsafe_allow_html=True)
# Compact status display inline
status_cols = st.columns(4)
with status_cols[0]:
st.markdown("<div class='terminal-text' style='font-size: 12px;'><b>SYSTEM:</b> ONLINE</div>", unsafe_allow_html=True)
with status_cols[1]:
st.markdown("<div class='terminal-text' style='font-size: 12px;'><b>CONNECTION:</b> SECURE</div>", unsafe_allow_html=True)
with status_cols[2]:
st.markdown("<div class='terminal-text' style='font-size: 12px;'><b>GPS:</b> ACTIVE</div>", unsafe_allow_html=True)
with status_cols[3]:
st.markdown("<div class='terminal-text' style='font-size: 12px;'><b>ENCRYPTION:</b> ENABLED</div>", unsafe_allow_html=True)
st.markdown("<hr style='border: 1px solid #00ff00; margin: 5px 0 10px 0;'>", unsafe_allow_html=True)
# Initialize session state for drone assistant and other needed state
if 'drone_agent' not in st.session_state:
model = create_qwen_model()
st.session_state['drone_agent'] = DroneAssistant(
tools=[
# Data analysis tools
analyze_flight_path,
check_sensor_readings,
recommend_maintenance,
generate_mission_plan,
# Drone control tools
connect_to_real_drone,
drone_takeoff,
drone_land,
drone_return_home,
drone_fly_to,
get_drone_location,
get_drone_battery,
execute_drone_mission,
disconnect_from_drone
],
model=model,
additional_authorized_imports=["pandas", "numpy", "matplotlib"]
)
# Initialize chat history in session state
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = []
# Generate sample data for demo purposes
if 'demo_data_loaded' not in st.session_state:
# Sample flight log
timestamps = pd.date_range(start='2023-01-01', periods=100, freq='10s')
flight_log = pd.DataFrame({
'timestamp': timestamps,
'altitude': np.random.normal(50, 10, 100),
'speed': np.random.normal(15, 5, 100),
'latitude': np.linspace(37.7749, 37.7750, 100) + np.random.normal(0, 0.0001, 100),
'longitude': np.linspace(-122.4194, -122.4192, 100) + np.random.normal(0, 0.0001, 100)
})
st.session_state['drone_agent'].register_flight_log('flight_001', flight_log)
# Sample sensor data
battery_data = pd.DataFrame({
'timestamp': pd.date_range(start='2023-01-01', periods=50, freq='1min'),
'voltage': np.random.normal(11.1, 0.2, 50),
'current': np.random.normal(5, 1, 50),
'temperature': np.random.normal(30, 5, 50)
})
st.session_state['drone_agent'].register_sensor_data('battery', battery_data)
imu_data = pd.DataFrame({
'timestamp': pd.date_range(start='2023-01-01', periods=1000, freq='1s'),
'acc_x': np.random.normal(0, 0.5, 1000),
'acc_y': np.random.normal(0, 0.5, 1000),
'acc_z': np.random.normal(9.8, 0.5, 1000),
'gyro_x': np.random.normal(0, 0.1, 1000),
'gyro_y': np.random.normal(0, 0.1, 1000),
'gyro_z': np.random.normal(0, 0.1, 1000)
})
st.session_state['drone_agent'].register_sensor_data('imu', imu_data)
st.session_state['demo_data_loaded'] = True
# Add mission status section to sidebar with improved visibility
st.sidebar.markdown("<h3 style='color: #00ff00; font-family: \"Courier New\", monospace;'>MISSION CONTROL</h3>", unsafe_allow_html=True)
# Dynamic status display that changes color based on status
status_color = "#00ff00" # Default green
if st.session_state.mission_status == "ERROR":
status_color = "#ff0000" # Red for errors
elif st.session_state.mission_status in ["CONNECTING", "TAKING OFF", "LANDING", "RETURNING"]:
status_color = "#ffff00" # Yellow for transitions
elif st.session_state.mission_status in ["MISSION", "EXECUTING MISSION", "AIRBORNE"]:
status_color = "#00ffff" # Cyan for active mission
st.sidebar.markdown(f"""
<div style='font-family: "Courier New", monospace; color: #00ff00;'>
<b>STATUS:</b> <span style="color: {status_color}; font-weight: bold;">{st.session_state.mission_status}</span><br>
<b>PHASE:</b> <span style="color: {status_color};">{st.session_state.mission_phase}</span><br>
<b>ACTIVE:</b> {"YES" if st.session_state.mission_in_progress else "NO"}<br>
<b>SIGNAL:</b> STRONG
</div>
""", unsafe_allow_html=True)
# Add interrupt button if a mission is in progress
if st.session_state.mission_in_progress:
if st.sidebar.button("β οΈ ABORT MISSION",
key="abort_button",
help="Immediately abort the current mission and return the drone to base",
type="primary"):
interrupt_mission()
# Add mission summary in sidebar
st.sidebar.markdown("<div style='color: #00ff00; font-family: monospace; font-size: 12px;'><b>MISSION MESSAGES:</b> Appearing in chat</div>", unsafe_allow_html=True)
# Show just the last message if there are any mission logs
if st.session_state.mission_log:
last_entry = st.session_state.mission_log[-1]
entry_style = ""
# Style the last entry based on its content
if "ERROR" in last_entry:
entry_style = "color: #ff0000;"
elif any(status in last_entry for status in ["CONNECTING", "TAKING OFF", "LANDING", "RETURNING"]):
entry_style = "color: #ffff00;"
elif any(status in last_entry for status in ["MISSION", "EXECUTING", "AIRBORNE"]):
entry_style = "color: #00ffff;"
else:
entry_style = "color: #88ff88;"
st.sidebar.markdown(f"""<div style='font-family: monospace; font-size: 11px; {entry_style} background-color: #111111; padding: 4px; border-radius: 3px;'>LAST: {last_entry}</div>""", unsafe_allow_html=True)
st.sidebar.markdown("<hr style='border: 1px solid #00ff00; margin: 20px 0;'>", unsafe_allow_html=True)
# Command reference
st.sidebar.markdown("<h3 style='color: #00ff00; font-family: \"Courier New\", monospace;'>COMMAND REFERENCE</h3>", unsafe_allow_html=True)
st.sidebar.markdown("""
<div style='font-family: "Courier New", monospace; color: #00ff00;'>
<b>DATA ANALYSIS:</b><br>
- "Analyze flight_001"<br>
- "Check battery sensor readings"<br>
- "Recommend maintenance for 75 flight hours"<br>
<br>
<b>MISSION PLANNING:</b><br>
- "Create a flight plan with a square pattern"<br>
- "Plan a survey mission for 30 minutes"<br>
- "Connect to the simulator, take off, execute a simple square flight pattern, and return home"<br>
<br>
<b>CORRECT FUNCTION NAMES:</b><br>
- connect_to_real_drone()<br>
- drone_takeoff()<br>
- drone_land()<br>
- drone_return_home()<br>
- drone_fly_to()<br>
- execute_drone_mission()<br>
</div>
""", unsafe_allow_html=True)
st.sidebar.markdown("<hr style='border: 1px solid #00ff00; margin: 20px 0;'>", unsafe_allow_html=True)
# Available data
st.sidebar.markdown("<h3 style='color: #00ff00; font-family: \"Courier New\", monospace;'>AVAILABLE DATA</h3>", unsafe_allow_html=True)
st.sidebar.markdown("""
<div style='font-family: "Courier New", monospace; color: #00ff00;'>
<b>FLIGHT LOGS:</b> flight_001<br>
<b>SENSORS:</b> battery, imu
</div>
""", unsafe_allow_html=True)
# Create chat area with container class
st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
# Info message about mission logs appearing in chat
if st.session_state.mission_in_progress:
st.markdown("""
<div style="text-align: center; margin-bottom: 8px; font-family: monospace; font-size: 12px; color: #00aaff;">
MISSION LOGS WILL APPEAR IN THIS CHAT WINDOW
</div>
""", unsafe_allow_html=True)
# Display initial assistant greeting or chat history
if not st.session_state['chat_history']:
# Welcome message with drone emoji
st.markdown("""
<div style="display: flex; align-items: flex-start; margin-bottom: 8px;">
<div style="font-size: 20px; margin-right: 8px; color: #00ff00;">π</div>
<div style="background-color: #101010; border: 1px solid #00ff00; border-radius: 5px; padding: 8px; color: #00ff00; flex-grow: 1;">
DEEPDRONE SYSTEM ONLINE. I am DeepDrone, your advanced drone operations assistant. AWAITING COMMANDS. You can request flight data analysis, sensor readings, maintenance recommendations, or mission planning.
</div>
</div>
""", unsafe_allow_html=True)
else:
# Display all messages in history
for message in st.session_state['chat_history']:
if message["role"] == "user":
st.markdown(f"""
<div style="display: flex; align-items: flex-start; justify-content: flex-end; margin-bottom: 8px;">
<div style="background-color: #1E1E1E; border: 1px solid #00ff00; border-radius: 5px; padding: 8px; color: #FFFFFF; max-width: 85%;">
{message["content"]}
</div>
<div style="font-size: 20px; margin-left: 8px; color: #00ff00;">π€</div>
</div>
""", unsafe_allow_html=True)
elif message["role"] == "system":
# System messages (logs) have a different style - centered and distinctive
st.markdown(f"""
<div style="display: flex; justify-content: center; align-items: center; margin: 4px 0;">
<div style="background-color: #0A0A0A; border: 1px solid #333333; border-radius: 3px; padding: 3px 10px;
font-family: monospace; font-size: 12px; text-align: center; max-width: 90%; opacity: 0.9;">
{message["content"]}
</div>
</div>
""", unsafe_allow_html=True)
else:
st.markdown(f"""
<div style="display: flex; align-items: flex-start; margin-bottom: 8px;">
<div style="font-size: 20px; margin-right: 8px; color: #00ff00;">π</div>
<div style="background-color: #101010; border: 1px solid #00ff00; border-radius: 5px; padding: 8px; color: #00ff00; max-width: 85%;">
{message["content"]}
</div>
</div>
""", unsafe_allow_html=True)
# Display the last image if there is one
if 'last_image' in st.session_state:
st.image(f"data:image/png;base64,{st.session_state['last_image']}")
# Clear the image from session state after displaying
del st.session_state['last_image']
# Close the chat-container div
st.markdown("</div>", unsafe_allow_html=True)
# Minimal spacing for the command bar
st.markdown("<div style='height: 10px;'></div>", unsafe_allow_html=True)
# Command bar fixed at the bottom
st.markdown("""
<div class="command-bar-wrapper" style="position: fixed; bottom: 0; left: 0; right: 0; padding: 8px; background-color: #0A0A0A; border-top: 2px solid #00ff00;">
""", unsafe_allow_html=True)
# Create a more compact form
with st.form(key="chat_form", clear_on_submit=True):
col1, col2 = st.columns([6, 1])
with col1:
user_message = st.text_input(
"COMMAND:",
placeholder="Enter your command...",
label_visibility="collapsed",
key="command_input"
)
with col2:
submit_button = st.form_submit_button(
"EXECUTE",
use_container_width=True
)
st.markdown("</div>", unsafe_allow_html=True)
# Process form submission
if submit_button and user_message:
# Add user message to chat history
st.session_state['chat_history'].append({
'role': 'user',
'content': user_message
})
# Process with the agent
with st.spinner('PROCESSING...'):
# Check for identity questions directly
identity_patterns = ["who are you", "what are you", "your name", "introduce yourself"]
if any(pattern in user_message.lower() for pattern in identity_patterns):
response = "I am DeepDrone, an advanced AI assistant designed specifically for drone operations and data analysis. I can help with flight data analysis, sensor readings, maintenance recommendations, and mission planning for your drone systems."
else:
# Process through the agent's chat method
response = st.session_state['drone_agent'].chat(user_message)
# No need to handle Message objects here as that's handled inside the chat method
# Handle base64 images in responses
if isinstance(response, str) and "visualization" in response and "base64" in response:
# Extract and display the image
import re
import ast
try:
# Parse the response to extract the base64 image
response_dict = ast.literal_eval(response)
if isinstance(response_dict, dict) and 'visualization' in response_dict:
img_data = response_dict['visualization']
if img_data:
# Store the image in session state to display on next rerun
st.session_state['last_image'] = img_data
# Remove the image data from the text response
response_dict['visualization'] = "[FLIGHT PATH VISUALIZATION DISPLAYED]"
response = str(response_dict)
except (SyntaxError, ValueError):
# If parsing fails, just display the text
pass
# Add assistant response to chat history
st.session_state['chat_history'].append({
'role': 'assistant',
'content': response
})
# Rerun to refresh the page and display the new messages
st.rerun()
if __name__ == "__main__":
main() |