Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -25,9 +25,13 @@ sector_averages = {
|
|
25 |
# Helper Functions
|
26 |
def get_company_info(symbol):
|
27 |
api_key = os.getenv("POLYGON_API_KEY")
|
|
|
28 |
url = f"https://api.polygon.io/v3/reference/tickers/{symbol}?apiKey={api_key}"
|
|
|
29 |
try:
|
30 |
response = requests.get(url)
|
|
|
|
|
31 |
response.raise_for_status()
|
32 |
data = response.json()['results']
|
33 |
return {
|
@@ -43,8 +47,11 @@ def get_company_info(symbol):
|
|
43 |
|
44 |
def get_current_price(symbol):
|
45 |
url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/prev?adjusted=true&apiKey={POLYGON_API_KEY}"
|
|
|
46 |
try:
|
47 |
response = requests.get(url)
|
|
|
|
|
48 |
response.raise_for_status()
|
49 |
data = response.json()['results'][0]
|
50 |
return float(data['c'])
|
@@ -72,6 +79,8 @@ def get_historical_prices(symbol):
|
|
72 |
url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/range/1/day/{start}/{end}?adjusted=true&sort=asc&apiKey={POLYGON_API_KEY}"
|
73 |
try:
|
74 |
response = requests.get(url)
|
|
|
|
|
75 |
response.raise_for_status()
|
76 |
results = response.json()['results']
|
77 |
dates = [datetime.datetime.fromtimestamp(r['t']/1000) for r in results]
|
@@ -99,7 +108,6 @@ def compare_to_sector(sector, ratios):
|
|
99 |
averages = sector_averages.get(sector, None)
|
100 |
if not averages:
|
101 |
return pd.DataFrame({"Metric": ["Sector data not available"], "Value": ["N/A"]})
|
102 |
-
|
103 |
comparison = {}
|
104 |
for key in averages:
|
105 |
stock_value = ratios.get(key, 0)
|
@@ -108,11 +116,13 @@ def compare_to_sector(sector, ratios):
|
|
108 |
return pd.DataFrame({"Ratio": list(comparison.keys()), "Comparison": list(comparison.values())})
|
109 |
|
110 |
def generate_summary(info, ratios):
|
111 |
-
text = (
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
116 |
summary = summarizer(text, max_length=120, min_length=30, do_sample=False)[0]['summary_text']
|
117 |
return summary
|
118 |
|
@@ -161,6 +171,7 @@ def download_report(info_table, ratios_table, sector_comp, summary):
|
|
161 |
file_path = f.name
|
162 |
return file_path
|
163 |
|
|
|
164 |
with gr.Blocks() as iface:
|
165 |
with gr.Row():
|
166 |
symbol = gr.Textbox(label="Stock Symbol (e.g., AAPL)", info="Ticker symbol of the company to analyze.")
|
@@ -198,3 +209,4 @@ with gr.Blocks() as iface:
|
|
198 |
|
199 |
if __name__ == "__main__":
|
200 |
iface.launch()
|
|
|
|
25 |
# Helper Functions
|
26 |
def get_company_info(symbol):
|
27 |
api_key = os.getenv("POLYGON_API_KEY")
|
28 |
+
print(f"DEBUG: Using API Key: {api_key}")
|
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 |
try:
|
32 |
response = requests.get(url)
|
33 |
+
print(f"DEBUG: Company Info Status Code: {response.status_code}")
|
34 |
+
print(f"DEBUG: Company Info Response: {response.text}")
|
35 |
response.raise_for_status()
|
36 |
data = response.json()['results']
|
37 |
return {
|
|
|
47 |
|
48 |
def get_current_price(symbol):
|
49 |
url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/prev?adjusted=true&apiKey={POLYGON_API_KEY}"
|
50 |
+
print(f"DEBUG: Fetching current price from URL: {url}")
|
51 |
try:
|
52 |
response = requests.get(url)
|
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'])
|
|
|
79 |
url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/range/1/day/{start}/{end}?adjusted=true&sort=asc&apiKey={POLYGON_API_KEY}"
|
80 |
try:
|
81 |
response = requests.get(url)
|
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]
|
|
|
108 |
averages = sector_averages.get(sector, None)
|
109 |
if not averages:
|
110 |
return pd.DataFrame({"Metric": ["Sector data not available"], "Value": ["N/A"]})
|
|
|
111 |
comparison = {}
|
112 |
for key in averages:
|
113 |
stock_value = ratios.get(key, 0)
|
|
|
116 |
return pd.DataFrame({"Ratio": list(comparison.keys()), "Comparison": list(comparison.values())})
|
117 |
|
118 |
def generate_summary(info, ratios):
|
119 |
+
text = (
|
120 |
+
f"{info['Name']} operates in the {info['Industry']} sector. It has a market capitalization of "
|
121 |
+
f"${info['Market Cap']:,.2f}. The company exhibits a P/E ratio of {ratios['P/E Ratio']:.2f}, "
|
122 |
+
f"P/S ratio of {ratios['P/S Ratio']:.2f}, and P/B ratio of {ratios['P/B Ratio']:.2f}. "
|
123 |
+
f"Its dividend yield is {ratios['Dividend Yield (%)']:.2f}%. "
|
124 |
+
f"This suggests a {'potential undervaluation' if ratios['P/E Ratio'] < 20 else 'higher valuation'} relative to the market."
|
125 |
+
)
|
126 |
summary = summarizer(text, max_length=120, min_length=30, do_sample=False)[0]['summary_text']
|
127 |
return summary
|
128 |
|
|
|
171 |
file_path = f.name
|
172 |
return file_path
|
173 |
|
174 |
+
# Gradio UI
|
175 |
with gr.Blocks() as iface:
|
176 |
with gr.Row():
|
177 |
symbol = gr.Textbox(label="Stock Symbol (e.g., AAPL)", info="Ticker symbol of the company to analyze.")
|
|
|
209 |
|
210 |
if __name__ == "__main__":
|
211 |
iface.launch()
|
212 |
+
|