Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -23,17 +23,27 @@ sector_averages = {
|
|
23 |
}
|
24 |
|
25 |
# Helper Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def get_company_info(symbol):
|
27 |
api_key = os.getenv("POLYGON_API_KEY")
|
28 |
-
|
|
|
|
|
29 |
url = f"https://api.polygon.io/v3/reference/tickers/{symbol}?apiKey={api_key}"
|
30 |
print(f"DEBUG: Fetching company info from URL: {url}")
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
print(f"DEBUG: Company Info Response: {response.text}")
|
35 |
-
response.raise_for_status()
|
36 |
-
data = response.json()['results']
|
37 |
return {
|
38 |
'Name': data.get('name', 'N/A'),
|
39 |
'Industry': data.get('sic_description', 'N/A'),
|
@@ -41,54 +51,42 @@ def get_company_info(symbol):
|
|
41 |
'Market Cap': data.get('market_cap', 0),
|
42 |
'Total Revenue': data.get('total_employees', 0) * 100000
|
43 |
}
|
44 |
-
|
45 |
-
print(f"DEBUG: Error fetching company info: {e}")
|
46 |
-
return None
|
47 |
|
48 |
def get_current_price(symbol):
|
49 |
-
|
|
|
50 |
print(f"DEBUG: Fetching current price from URL: {url}")
|
51 |
-
|
52 |
-
|
53 |
-
print(f"DEBUG: Current Price Status Code: {response.status_code}")
|
54 |
-
print(f"DEBUG: Current Price Response: {response.text}")
|
55 |
-
response.raise_for_status()
|
56 |
data = response.json()['results'][0]
|
57 |
return float(data['c'])
|
58 |
-
|
59 |
-
print(f"DEBUG: Error fetching current price: {e}")
|
60 |
-
return None
|
61 |
|
62 |
def get_dividends(symbol):
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
data = response.json()['results'][0]
|
68 |
return {
|
69 |
'Dividend Amount': data.get('cash_amount', 0),
|
70 |
'Ex-Dividend Date': data.get('ex_dividend_date', 'N/A')
|
71 |
}
|
72 |
-
|
73 |
-
print(f"DEBUG: Error fetching dividends: {e}")
|
74 |
-
return {'Dividend Amount': 0, 'Ex-Dividend Date': 'N/A'}
|
75 |
|
76 |
def get_historical_prices(symbol):
|
|
|
77 |
end = datetime.date.today()
|
78 |
start = end - datetime.timedelta(days=365)
|
79 |
-
url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/range/1/day/{start}/{end}?adjusted=true&sort=asc&apiKey={
|
80 |
-
|
81 |
-
|
82 |
-
print(f"DEBUG: Historical Prices Status Code: {response.status_code}")
|
83 |
-
print(f"DEBUG: Historical Prices Response: {response.text}")
|
84 |
-
response.raise_for_status()
|
85 |
results = response.json()['results']
|
86 |
dates = [datetime.datetime.fromtimestamp(r['t']/1000) for r in results]
|
87 |
prices = [r['c'] for r in results]
|
88 |
return dates, prices
|
89 |
-
|
90 |
-
print(f"DEBUG: Error fetching historical prices: {e}")
|
91 |
-
return [], []
|
92 |
|
93 |
def generate_summary(info, ratios):
|
94 |
recommendation = "Hold"
|
@@ -97,7 +95,6 @@ def generate_summary(info, ratios):
|
|
97 |
elif ratios['P/E Ratio'] > 30 and ratios['P/B Ratio'] > 5 and ratios['PEG Ratio'] > 2.0:
|
98 |
recommendation = "Sell"
|
99 |
|
100 |
-
# Much better structured prompt
|
101 |
report = (
|
102 |
f"Company Overview:\n"
|
103 |
f"Name: {info['Name']}\n"
|
@@ -113,11 +110,9 @@ def generate_summary(info, ratios):
|
|
113 |
f"Recommended Investment Action: {recommendation}.\n\n"
|
114 |
f"Please provide a detailed financial analysis based on the information above."
|
115 |
)
|
116 |
-
|
117 |
summary = summarizer(report, max_length=250, min_length=100, do_sample=False)[0]['summary_text']
|
118 |
return summary
|
119 |
|
120 |
-
|
121 |
# (Rest of the code remains the same)
|
122 |
def calculate_ratios(market_cap, total_revenue, price, dividend_amount, assumed_eps=5.0, growth_rate=0.1, book_value=500000000):
|
123 |
pe_ratio = price / assumed_eps if assumed_eps else 0
|
|
|
23 |
}
|
24 |
|
25 |
# Helper Functions
|
26 |
+
def safe_request(url):
|
27 |
+
try:
|
28 |
+
response = requests.get(url)
|
29 |
+
response.raise_for_status()
|
30 |
+
return response
|
31 |
+
except requests.exceptions.HTTPError as http_err:
|
32 |
+
print(f"DEBUG: HTTP error occurred: {http_err}")
|
33 |
+
except Exception as err:
|
34 |
+
print(f"DEBUG: Other error occurred: {err}")
|
35 |
+
return None
|
36 |
+
|
37 |
def get_company_info(symbol):
|
38 |
api_key = os.getenv("POLYGON_API_KEY")
|
39 |
+
if not api_key:
|
40 |
+
print("DEBUG: API Key is missing!")
|
41 |
+
return None
|
42 |
url = f"https://api.polygon.io/v3/reference/tickers/{symbol}?apiKey={api_key}"
|
43 |
print(f"DEBUG: Fetching company info from URL: {url}")
|
44 |
+
response = safe_request(url)
|
45 |
+
if response:
|
46 |
+
data = response.json().get('results', {})
|
|
|
|
|
|
|
47 |
return {
|
48 |
'Name': data.get('name', 'N/A'),
|
49 |
'Industry': data.get('sic_description', 'N/A'),
|
|
|
51 |
'Market Cap': data.get('market_cap', 0),
|
52 |
'Total Revenue': data.get('total_employees', 0) * 100000
|
53 |
}
|
54 |
+
return None
|
|
|
|
|
55 |
|
56 |
def get_current_price(symbol):
|
57 |
+
api_key = os.getenv("POLYGON_API_KEY")
|
58 |
+
url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/prev?adjusted=true&apiKey={api_key}"
|
59 |
print(f"DEBUG: Fetching current price from URL: {url}")
|
60 |
+
response = safe_request(url)
|
61 |
+
if response:
|
|
|
|
|
|
|
62 |
data = response.json()['results'][0]
|
63 |
return float(data['c'])
|
64 |
+
return None
|
|
|
|
|
65 |
|
66 |
def get_dividends(symbol):
|
67 |
+
api_key = os.getenv("POLYGON_API_KEY")
|
68 |
+
url = f"https://api.polygon.io/v3/reference/dividends?ticker={symbol}&apiKey={api_key}"
|
69 |
+
response = safe_request(url)
|
70 |
+
if response:
|
71 |
data = response.json()['results'][0]
|
72 |
return {
|
73 |
'Dividend Amount': data.get('cash_amount', 0),
|
74 |
'Ex-Dividend Date': data.get('ex_dividend_date', 'N/A')
|
75 |
}
|
76 |
+
return {'Dividend Amount': 0, 'Ex-Dividend Date': 'N/A'}
|
|
|
|
|
77 |
|
78 |
def get_historical_prices(symbol):
|
79 |
+
api_key = os.getenv("POLYGON_API_KEY")
|
80 |
end = datetime.date.today()
|
81 |
start = end - datetime.timedelta(days=365)
|
82 |
+
url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/range/1/day/{start}/{end}?adjusted=true&sort=asc&apiKey={api_key}"
|
83 |
+
response = safe_request(url)
|
84 |
+
if response:
|
|
|
|
|
|
|
85 |
results = response.json()['results']
|
86 |
dates = [datetime.datetime.fromtimestamp(r['t']/1000) for r in results]
|
87 |
prices = [r['c'] for r in results]
|
88 |
return dates, prices
|
89 |
+
return [], []
|
|
|
|
|
90 |
|
91 |
def generate_summary(info, ratios):
|
92 |
recommendation = "Hold"
|
|
|
95 |
elif ratios['P/E Ratio'] > 30 and ratios['P/B Ratio'] > 5 and ratios['PEG Ratio'] > 2.0:
|
96 |
recommendation = "Sell"
|
97 |
|
|
|
98 |
report = (
|
99 |
f"Company Overview:\n"
|
100 |
f"Name: {info['Name']}\n"
|
|
|
110 |
f"Recommended Investment Action: {recommendation}.\n\n"
|
111 |
f"Please provide a detailed financial analysis based on the information above."
|
112 |
)
|
|
|
113 |
summary = summarizer(report, max_length=250, min_length=100, do_sample=False)[0]['summary_text']
|
114 |
return summary
|
115 |
|
|
|
116 |
# (Rest of the code remains the same)
|
117 |
def calculate_ratios(market_cap, total_revenue, price, dividend_amount, assumed_eps=5.0, growth_rate=0.1, book_value=500000000):
|
118 |
pe_ratio = price / assumed_eps if assumed_eps else 0
|