Spaces:
Sleeping
Sleeping
Faham
commited on
Commit
ยท
6da16d5
1
Parent(s):
139d9d7
UPDATE: remove redundant code
Browse files- Home.py +10 -80
- pages/System_Monitor.py +0 -4
- resource_monitor.py +1 -8
- terminal_client.py +1 -58
Home.py
CHANGED
|
@@ -6,6 +6,11 @@ import os
|
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import yfinance as yf
|
| 8 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
try:
|
| 11 |
from prophet import Prophet
|
|
@@ -22,11 +27,6 @@ from mcp import StdioServerParameters, types
|
|
| 22 |
try:
|
| 23 |
from resource_monitor import (
|
| 24 |
start_resource_monitoring,
|
| 25 |
-
stop_resource_monitoring,
|
| 26 |
-
get_resource_stats,
|
| 27 |
-
create_resource_dashboard,
|
| 28 |
-
get_resource_summary,
|
| 29 |
-
export_resource_data,
|
| 30 |
resource_monitor,
|
| 31 |
)
|
| 32 |
|
|
@@ -64,59 +64,10 @@ client = OpenAI(
|
|
| 64 |
discovered_tools = []
|
| 65 |
|
| 66 |
|
| 67 |
-
def extract_ticker_from_query(query: str) -> str:
|
| 68 |
-
"""Extract ticker symbol from user query."""
|
| 69 |
-
query_upper = query.upper()
|
| 70 |
-
|
| 71 |
-
# First try to find ticker in parentheses
|
| 72 |
-
paren_match = re.search(r"\(([A-Z]{1,5})\)", query_upper)
|
| 73 |
-
if paren_match:
|
| 74 |
-
return paren_match.group(1)
|
| 75 |
-
|
| 76 |
-
# Look for our predefined tickers in the query
|
| 77 |
-
predefined_tickers = ["AAPL", "TSLA", "MSFT", "GOOG"]
|
| 78 |
-
for ticker in predefined_tickers:
|
| 79 |
-
if ticker in query_upper:
|
| 80 |
-
return ticker
|
| 81 |
-
|
| 82 |
-
# Try to find any 2-5 letter uppercase sequence that might be a ticker
|
| 83 |
-
ticker_match = re.search(r"\b([A-Z]{2,5})\b", query_upper)
|
| 84 |
-
if ticker_match:
|
| 85 |
-
potential_ticker = ticker_match.group(1)
|
| 86 |
-
# Avoid common words that might be mistaken for tickers
|
| 87 |
-
if potential_ticker not in [
|
| 88 |
-
"THE",
|
| 89 |
-
"AND",
|
| 90 |
-
"FOR",
|
| 91 |
-
"HOW",
|
| 92 |
-
"WHAT",
|
| 93 |
-
"WHEN",
|
| 94 |
-
"WHERE",
|
| 95 |
-
"WHY",
|
| 96 |
-
]:
|
| 97 |
-
return potential_ticker
|
| 98 |
-
|
| 99 |
-
return None
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
def validate_ticker(ticker: str) -> bool:
|
| 103 |
-
"""Validate if ticker symbol is in correct format."""
|
| 104 |
-
if not ticker:
|
| 105 |
-
return False
|
| 106 |
-
# Basic validation: 1-5 uppercase letters
|
| 107 |
-
return bool(re.match(r"^[A-Z]{1,5}$", ticker))
|
| 108 |
-
|
| 109 |
-
|
| 110 |
async def get_news_data(ticker: str) -> str:
|
| 111 |
"""Get news data by calling the news server via MCP."""
|
| 112 |
try:
|
| 113 |
-
# Validate ticker
|
| 114 |
-
if not validate_ticker(ticker):
|
| 115 |
-
return f"Invalid ticker symbol: {ticker}. Please use a valid stock symbol (e.g., AAPL, TSLA)."
|
| 116 |
-
|
| 117 |
# Set up MCP server parameters
|
| 118 |
-
import os
|
| 119 |
-
import sys
|
| 120 |
|
| 121 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 122 |
news_server_path = os.path.join(current_dir, "news_server.py")
|
|
@@ -181,13 +132,7 @@ async def get_news_data(ticker: str) -> str:
|
|
| 181 |
async def get_stock_data(ticker: str) -> str:
|
| 182 |
"""Get stock data by calling the stock server via MCP."""
|
| 183 |
try:
|
| 184 |
-
# Validate ticker
|
| 185 |
-
if not validate_ticker(ticker):
|
| 186 |
-
return f"Invalid ticker symbol: {ticker}. Please use a valid stock symbol (e.g., AAPL, TSLA)."
|
| 187 |
-
|
| 188 |
# Set up MCP server parameters
|
| 189 |
-
import os
|
| 190 |
-
import sys
|
| 191 |
|
| 192 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 193 |
stock_server_path = os.path.join(current_dir, "stock_data_server.py")
|
|
@@ -321,7 +266,6 @@ def create_stock_chart(ticker: str):
|
|
| 321 |
last_historical_date = df["ds"].max()
|
| 322 |
|
| 323 |
# Add one day to ensure we start from tomorrow
|
| 324 |
-
from datetime import timedelta
|
| 325 |
|
| 326 |
tomorrow = last_historical_date + timedelta(days=1)
|
| 327 |
|
|
@@ -563,7 +507,6 @@ async def execute_tool_call(tool_call):
|
|
| 563 |
arguments = json.loads(arguments_str)
|
| 564 |
except json.JSONDecodeError:
|
| 565 |
# Try to find JSON within the string
|
| 566 |
-
import re
|
| 567 |
|
| 568 |
json_match = re.search(r"\{[^{}]*\}", arguments_str)
|
| 569 |
if json_match:
|
|
@@ -708,18 +651,6 @@ async def run_agent(user_query, selected_ticker):
|
|
| 708 |
def display_top_news(ticker: str):
|
| 709 |
"""Display top news headlines for the given ticker with clickable links."""
|
| 710 |
try:
|
| 711 |
-
import gnews
|
| 712 |
-
from bs4 import BeautifulSoup
|
| 713 |
-
import re
|
| 714 |
-
|
| 715 |
-
def preprocess_text(text):
|
| 716 |
-
"""A simple function to clean text by removing HTML and extra whitespace."""
|
| 717 |
-
if not text:
|
| 718 |
-
return ""
|
| 719 |
-
soup = BeautifulSoup(text, "html.parser")
|
| 720 |
-
clean_text = soup.get_text()
|
| 721 |
-
clean_text = re.sub(r"\s+", " ", clean_text).strip()
|
| 722 |
-
return clean_text
|
| 723 |
|
| 724 |
# Check if news is already cached
|
| 725 |
news_cache_key = f"news_data_{ticker}"
|
|
@@ -740,7 +671,11 @@ def display_top_news(ticker: str):
|
|
| 740 |
|
| 741 |
# Display top 5 articles
|
| 742 |
for i, article in enumerate(articles[:5], 1):
|
| 743 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 744 |
url = article.get("url", "")
|
| 745 |
publisher = article.get("publisher", {}).get("title", "Unknown Source")
|
| 746 |
|
|
@@ -762,9 +697,6 @@ def display_top_news(ticker: str):
|
|
| 762 |
|
| 763 |
def test_server_availability():
|
| 764 |
"""Test if the MCP servers are available and can be executed."""
|
| 765 |
-
import os
|
| 766 |
-
import subprocess
|
| 767 |
-
import time
|
| 768 |
|
| 769 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 770 |
|
|
@@ -781,8 +713,6 @@ def test_server_availability():
|
|
| 781 |
return False
|
| 782 |
|
| 783 |
# Test if servers can be executed by checking if they can be imported
|
| 784 |
-
import sys
|
| 785 |
-
import importlib.util
|
| 786 |
|
| 787 |
try:
|
| 788 |
# Test if news_server can be imported
|
|
|
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import yfinance as yf
|
| 8 |
import time
|
| 9 |
+
import sys
|
| 10 |
+
from datetime import timedelta
|
| 11 |
+
import gnews
|
| 12 |
+
from bs4 import BeautifulSoup
|
| 13 |
+
import importlib.util
|
| 14 |
|
| 15 |
try:
|
| 16 |
from prophet import Prophet
|
|
|
|
| 27 |
try:
|
| 28 |
from resource_monitor import (
|
| 29 |
start_resource_monitoring,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
resource_monitor,
|
| 31 |
)
|
| 32 |
|
|
|
|
| 64 |
discovered_tools = []
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
async def get_news_data(ticker: str) -> str:
|
| 68 |
"""Get news data by calling the news server via MCP."""
|
| 69 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
# Set up MCP server parameters
|
|
|
|
|
|
|
| 71 |
|
| 72 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 73 |
news_server_path = os.path.join(current_dir, "news_server.py")
|
|
|
|
| 132 |
async def get_stock_data(ticker: str) -> str:
|
| 133 |
"""Get stock data by calling the stock server via MCP."""
|
| 134 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
# Set up MCP server parameters
|
|
|
|
|
|
|
| 136 |
|
| 137 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 138 |
stock_server_path = os.path.join(current_dir, "stock_data_server.py")
|
|
|
|
| 266 |
last_historical_date = df["ds"].max()
|
| 267 |
|
| 268 |
# Add one day to ensure we start from tomorrow
|
|
|
|
| 269 |
|
| 270 |
tomorrow = last_historical_date + timedelta(days=1)
|
| 271 |
|
|
|
|
| 507 |
arguments = json.loads(arguments_str)
|
| 508 |
except json.JSONDecodeError:
|
| 509 |
# Try to find JSON within the string
|
|
|
|
| 510 |
|
| 511 |
json_match = re.search(r"\{[^{}]*\}", arguments_str)
|
| 512 |
if json_match:
|
|
|
|
| 651 |
def display_top_news(ticker: str):
|
| 652 |
"""Display top news headlines for the given ticker with clickable links."""
|
| 653 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
|
| 655 |
# Check if news is already cached
|
| 656 |
news_cache_key = f"news_data_{ticker}"
|
|
|
|
| 671 |
|
| 672 |
# Display top 5 articles
|
| 673 |
for i, article in enumerate(articles[:5], 1):
|
| 674 |
+
# Clean the title text
|
| 675 |
+
title = article.get("title", "")
|
| 676 |
+
if title:
|
| 677 |
+
soup = BeautifulSoup(title, "html.parser")
|
| 678 |
+
title = soup.get_text().strip()
|
| 679 |
url = article.get("url", "")
|
| 680 |
publisher = article.get("publisher", {}).get("title", "Unknown Source")
|
| 681 |
|
|
|
|
| 697 |
|
| 698 |
def test_server_availability():
|
| 699 |
"""Test if the MCP servers are available and can be executed."""
|
|
|
|
|
|
|
|
|
|
| 700 |
|
| 701 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 702 |
|
|
|
|
| 713 |
return False
|
| 714 |
|
| 715 |
# Test if servers can be executed by checking if they can be imported
|
|
|
|
|
|
|
| 716 |
|
| 717 |
try:
|
| 718 |
# Test if news_server can be imported
|
pages/System_Monitor.py
CHANGED
|
@@ -1,16 +1,12 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import time
|
| 3 |
|
| 4 |
# Import resource monitoring
|
| 5 |
try:
|
| 6 |
from resource_monitor import (
|
| 7 |
start_resource_monitoring,
|
| 8 |
-
stop_resource_monitoring,
|
| 9 |
get_resource_stats,
|
| 10 |
-
create_resource_dashboard,
|
| 11 |
get_resource_summary,
|
| 12 |
export_resource_data,
|
| 13 |
-
resource_monitor,
|
| 14 |
)
|
| 15 |
|
| 16 |
RESOURCE_MONITORING_AVAILABLE = True
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
| 3 |
# Import resource monitoring
|
| 4 |
try:
|
| 5 |
from resource_monitor import (
|
| 6 |
start_resource_monitoring,
|
|
|
|
| 7 |
get_resource_stats,
|
|
|
|
| 8 |
get_resource_summary,
|
| 9 |
export_resource_data,
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
RESOURCE_MONITORING_AVAILABLE = True
|
resource_monitor.py
CHANGED
|
@@ -1,15 +1,8 @@
|
|
| 1 |
import psutil
|
| 2 |
import time
|
| 3 |
import threading
|
| 4 |
-
import streamlit as st
|
| 5 |
-
import pandas as pd
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
-
from datetime import datetime
|
| 8 |
-
import os
|
| 9 |
-
import sys
|
| 10 |
-
import asyncio
|
| 11 |
-
import yfinance as yf
|
| 12 |
-
from typing import Dict, List, Optional
|
| 13 |
import json
|
| 14 |
|
| 15 |
|
|
|
|
| 1 |
import psutil
|
| 2 |
import time
|
| 3 |
import threading
|
|
|
|
|
|
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
+
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import json
|
| 7 |
|
| 8 |
|
terminal_client.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
import json
|
| 4 |
-
import re
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from openai import OpenAI
|
| 7 |
from mcp.client.session import ClientSession
|
|
@@ -31,57 +30,10 @@ client = OpenAI(
|
|
| 31 |
discovered_tools = []
|
| 32 |
|
| 33 |
|
| 34 |
-
def extract_ticker_from_query(query: str) -> str:
|
| 35 |
-
"""Extract ticker symbol from user query."""
|
| 36 |
-
query_upper = query.upper()
|
| 37 |
-
|
| 38 |
-
# First try to find ticker in parentheses
|
| 39 |
-
paren_match = re.search(r"\(([A-Z]{1,5})\)", query_upper)
|
| 40 |
-
if paren_match:
|
| 41 |
-
return paren_match.group(1)
|
| 42 |
-
|
| 43 |
-
# Look for our predefined tickers in the query
|
| 44 |
-
predefined_tickers = ["AAPL", "TSLA", "MSFT", "GOOG"]
|
| 45 |
-
for ticker in predefined_tickers:
|
| 46 |
-
if ticker in query_upper:
|
| 47 |
-
return ticker
|
| 48 |
-
|
| 49 |
-
# Try to find any 2-5 letter uppercase sequence that might be a ticker
|
| 50 |
-
ticker_match = re.search(r"\b([A-Z]{2,5})\b", query_upper)
|
| 51 |
-
if ticker_match:
|
| 52 |
-
potential_ticker = ticker_match.group(1)
|
| 53 |
-
# Avoid common words that might be mistaken for tickers
|
| 54 |
-
if potential_ticker not in [
|
| 55 |
-
"THE",
|
| 56 |
-
"AND",
|
| 57 |
-
"FOR",
|
| 58 |
-
"HOW",
|
| 59 |
-
"WHAT",
|
| 60 |
-
"WHEN",
|
| 61 |
-
"WHERE",
|
| 62 |
-
"WHY",
|
| 63 |
-
]:
|
| 64 |
-
return potential_ticker
|
| 65 |
-
|
| 66 |
-
return None
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def validate_ticker(ticker: str) -> bool:
|
| 70 |
-
"""Validate if ticker symbol is in correct format."""
|
| 71 |
-
if not ticker:
|
| 72 |
-
return False
|
| 73 |
-
# Basic validation: 1-5 uppercase letters
|
| 74 |
-
return bool(re.match(r"^[A-Z]{1,5}$", ticker))
|
| 75 |
-
|
| 76 |
-
|
| 77 |
async def get_news_data(ticker: str) -> str:
|
| 78 |
"""Get news data by calling the news server via MCP."""
|
| 79 |
try:
|
| 80 |
-
#
|
| 81 |
-
if not validate_ticker(ticker):
|
| 82 |
-
return f"Invalid ticker symbol: {ticker}. Please use a valid stock symbol (e.g., AAPL, TSLA)."
|
| 83 |
-
|
| 84 |
-
# Set up MCP server parameters
|
| 85 |
server_params = StdioServerParameters(command="python", args=["news_server.py"])
|
| 86 |
|
| 87 |
# Connect to the MCP server
|
|
@@ -121,10 +73,6 @@ async def get_news_data(ticker: str) -> str:
|
|
| 121 |
async def get_stock_data(ticker: str) -> str:
|
| 122 |
"""Get stock data by calling the stock server via MCP."""
|
| 123 |
try:
|
| 124 |
-
# Validate ticker
|
| 125 |
-
if not validate_ticker(ticker):
|
| 126 |
-
return f"Invalid ticker symbol: {ticker}. Please use a valid stock symbol (e.g., AAPL, TSLA)."
|
| 127 |
-
|
| 128 |
# Set up MCP server parameters
|
| 129 |
server_params = StdioServerParameters(
|
| 130 |
command="python", args=["stock_data_server.py"]
|
|
@@ -259,11 +207,6 @@ You are FORBIDDEN from responding without calling both tools. Always call both t
|
|
| 259 |
async def run_agent(user_query):
|
| 260 |
print(f"\n๐ User Query: {user_query}")
|
| 261 |
|
| 262 |
-
# Extract ticker from query for validation
|
| 263 |
-
extracted_ticker = extract_ticker_from_query(user_query)
|
| 264 |
-
if extracted_ticker:
|
| 265 |
-
print(f"๐ Detected ticker: {extracted_ticker}")
|
| 266 |
-
|
| 267 |
messages = [
|
| 268 |
{"role": "system", "content": system_prompt},
|
| 269 |
{"role": "user", "content": user_query},
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
import json
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from openai import OpenAI
|
| 6 |
from mcp.client.session import ClientSession
|
|
|
|
| 30 |
discovered_tools = []
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
async def get_news_data(ticker: str) -> str:
|
| 34 |
"""Get news data by calling the news server via MCP."""
|
| 35 |
try:
|
| 36 |
+
# Set up MCP server parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
server_params = StdioServerParameters(command="python", args=["news_server.py"])
|
| 38 |
|
| 39 |
# Connect to the MCP server
|
|
|
|
| 73 |
async def get_stock_data(ticker: str) -> str:
|
| 74 |
"""Get stock data by calling the stock server via MCP."""
|
| 75 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Set up MCP server parameters
|
| 77 |
server_params = StdioServerParameters(
|
| 78 |
command="python", args=["stock_data_server.py"]
|
|
|
|
| 207 |
async def run_agent(user_query):
|
| 208 |
print(f"\n๐ User Query: {user_query}")
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
messages = [
|
| 211 |
{"role": "system", "content": system_prompt},
|
| 212 |
{"role": "user", "content": user_query},
|