Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -50,10 +50,8 @@ def safe_request(url):
|
|
50 |
return None
|
51 |
|
52 |
# Fetch Functions
|
53 |
-
# (functions unchanged)
|
54 |
-
# ...
|
55 |
|
56 |
-
|
57 |
# Financial Calculations
|
58 |
def calculate_ratios(market_cap, total_revenue, price, dividend_amount, eps=5.0, growth=0.1, book_value=500000000):
|
59 |
pe = price / eps if eps else 0
|
@@ -78,6 +76,40 @@ def calculate_ratios(market_cap, total_revenue, price, dividend_amount, eps=5.0,
|
|
78 |
'Beta (Volatility)': beta
|
79 |
}
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
# Theme Selection
|
82 |
selected_theme = os.getenv("APP_THEME", "light")
|
83 |
if selected_theme == "dark":
|
@@ -204,31 +236,6 @@ def generate_summary(info, ratios):
|
|
204 |
summary_prompt = f"Summarize the following financial report clearly and briefly:\n\n{report}"
|
205 |
return query_mistral(summary_prompt)
|
206 |
|
207 |
-
def stock_research(symbol, eps=5.0, growth=0.1, book=500000000):
|
208 |
-
info = get_company_info(symbol)
|
209 |
-
price = get_current_price(symbol)
|
210 |
-
dividends = get_dividends(symbol)
|
211 |
-
dates, prices = get_historical_prices(symbol)
|
212 |
-
|
213 |
-
if not info or not price:
|
214 |
-
return "⚠️ Error fetching stock info", None, None, None, None
|
215 |
-
|
216 |
-
ratios = calculate_ratios(info['Market Cap'], info['Total Revenue'], price, dividends, eps, growth, book)
|
217 |
-
summary = generate_summary(info, ratios)
|
218 |
-
sector_comp = compare_to_sector(info['Sector'], ratios)
|
219 |
-
|
220 |
-
fig, ax = plt.subplots()
|
221 |
-
ax.plot(dates, prices)
|
222 |
-
ax.set_title(f"{symbol} Historical Price (1Y)")
|
223 |
-
ax.set_xlabel("Date")
|
224 |
-
ax.set_ylabel("Price ($)")
|
225 |
-
ax.grid(True)
|
226 |
-
|
227 |
-
info_table = pd.DataFrame(info.items(), columns=["Metric", "Value"])
|
228 |
-
ratios_table = pd.DataFrame(ratios.items(), columns=["Ratio", "Value"])
|
229 |
-
|
230 |
-
return summary, info_table, ratios_table, sector_comp, fig
|
231 |
-
|
232 |
|
233 |
|
234 |
# Gradio UI
|
@@ -246,6 +253,8 @@ with gr.Blocks(theme=theme) as iface:
|
|
246 |
output_info = gr.Dataframe()
|
247 |
with gr.Tab("Valuation Ratios"):
|
248 |
output_ratios = gr.Dataframe()
|
|
|
|
|
249 |
with gr.Tab("Sector Comparison"):
|
250 |
output_sector = gr.Dataframe()
|
251 |
with gr.Tab("Historical Price Chart"):
|
@@ -271,7 +280,12 @@ with gr.Blocks(theme=theme) as iface:
|
|
271 |
file_output = gr.File()
|
272 |
|
273 |
submit_btn.click(fn=stock_research, inputs=[symbol, eps, growth, book],
|
274 |
-
outputs=[output_summary, output_info, output_ratios, output_sector, output_chart])
|
|
|
|
|
|
|
|
|
|
|
275 |
|
276 |
def reset_fields():
|
277 |
return "", 5.0, 0.1, 500000000, "", "", "", "", None
|
|
|
50 |
return None
|
51 |
|
52 |
# Fetch Functions
|
|
|
|
|
53 |
|
54 |
+
|
55 |
# Financial Calculations
|
56 |
def calculate_ratios(market_cap, total_revenue, price, dividend_amount, eps=5.0, growth=0.1, book_value=500000000):
|
57 |
pe = price / eps if eps else 0
|
|
|
76 |
'Beta (Volatility)': beta
|
77 |
}
|
78 |
|
79 |
+
def stock_research(symbol, eps=5.0, growth=0.1, book=500000000):
|
80 |
+
info = {"Name": symbol, "Industry": "Tech", "Sector": "Technology", "Market Cap": np.random.randint(1000000000, 3000000000)}
|
81 |
+
price = np.random.uniform(100, 300)
|
82 |
+
dividends = np.random.uniform(0, 5)
|
83 |
+
dates = pd.date_range(datetime.date.today() - datetime.timedelta(days=365), periods=365)
|
84 |
+
prices = np.random.uniform(100, 300, size=365)
|
85 |
+
|
86 |
+
ratios = calculate_ratios(info['Market Cap'], info['Market Cap']/5, price, dividends, eps, growth, book)
|
87 |
+
ratios = {k: round(v, 2) for k, v in ratios.items()}
|
88 |
+
|
89 |
+
sector_comp = pd.DataFrame({"Metric": ["Example"], "Value": [0]})
|
90 |
+
|
91 |
+
smooth_prices = np.convolve(prices, np.ones(5)/5, mode='valid')
|
92 |
+
fig, ax = plt.subplots()
|
93 |
+
ax.plot(dates[:len(smooth_prices)], smooth_prices)
|
94 |
+
ax.set_title(f"{symbol} Historical Price (Smoothed)")
|
95 |
+
ax.set_xlabel("Date")
|
96 |
+
ax.set_ylabel("Price ($)")
|
97 |
+
ax.grid(True)
|
98 |
+
|
99 |
+
info_table = pd.DataFrame(info.items(), columns=["Metric", "Value"])
|
100 |
+
ratios_table = pd.DataFrame(ratios.items(), columns=["Metric", "Value"])
|
101 |
+
financial_health = ratios_table[ratios_table["Metric"].isin(["Debt/Equity Ratio", "Return on Equity (%)", "Free Cash Flow ($)", "Beta (Volatility)"])]
|
102 |
+
|
103 |
+
recommendation = "Hold"
|
104 |
+
if ratios['P/E Ratio'] < 15 and ratios['Debt/Equity Ratio'] < 1.0 and ratios['Return on Equity (%)'] > 10 and ratios['Beta (Volatility)'] < 1.2:
|
105 |
+
recommendation = "Buy"
|
106 |
+
elif ratios['P/E Ratio'] > 30 or ratios['Debt/Equity Ratio'] > 2.0 or ratios['Return on Equity (%)'] < 5:
|
107 |
+
recommendation = "Sell"
|
108 |
+
|
109 |
+
summary = f"Recommendation: {recommendation} based on financial health and valuation ratios."
|
110 |
+
|
111 |
+
return summary, info_table, ratios_table, financial_health, sector_comp, fig
|
112 |
+
|
113 |
# Theme Selection
|
114 |
selected_theme = os.getenv("APP_THEME", "light")
|
115 |
if selected_theme == "dark":
|
|
|
236 |
summary_prompt = f"Summarize the following financial report clearly and briefly:\n\n{report}"
|
237 |
return query_mistral(summary_prompt)
|
238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
|
241 |
# Gradio UI
|
|
|
253 |
output_info = gr.Dataframe()
|
254 |
with gr.Tab("Valuation Ratios"):
|
255 |
output_ratios = gr.Dataframe()
|
256 |
+
with gr.Tab("Financial Health"):
|
257 |
+
output_health = gr.Dataframe()
|
258 |
with gr.Tab("Sector Comparison"):
|
259 |
output_sector = gr.Dataframe()
|
260 |
with gr.Tab("Historical Price Chart"):
|
|
|
280 |
file_output = gr.File()
|
281 |
|
282 |
submit_btn.click(fn=stock_research, inputs=[symbol, eps, growth, book],
|
283 |
+
outputs=[output_summary, output_info, output_ratios, output_health, output_sector, output_chart])
|
284 |
+
|
285 |
+
def reset_fields():
|
286 |
+
return "", 5.0, 0.1, 500000000, "", "", "", "", "", None
|
287 |
+
|
288 |
+
reset_btn.click(fn=reset_fields, inputs=[], outputs=[symbol, eps, growth, book, output_summary, output_info, output_ratios, output_health, output_sector, output_chart])
|
289 |
|
290 |
def reset_fields():
|
291 |
return "", 5.0, 0.1, 500000000, "", "", "", "", None
|