Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,33 +5,25 @@ import requests
|
|
5 |
import os
|
6 |
import datetime
|
7 |
import tempfile
|
8 |
-
import numpy as np
|
9 |
|
10 |
-
# Your Hugging Face API Token
|
11 |
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
12 |
|
13 |
-
# Mistral Inference API URL
|
14 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
15 |
|
16 |
-
# Headers for authentication
|
17 |
headers = {
|
18 |
"Authorization": f"Bearer {HF_TOKEN}"
|
19 |
}
|
20 |
|
21 |
-
# Function to query Mistral API
|
22 |
def query_mistral(question):
|
23 |
-
payload = {
|
24 |
-
"inputs": question,
|
25 |
-
"parameters": {"max_length": 256}
|
26 |
-
}
|
27 |
response = requests.post(API_URL, headers=headers, json=payload)
|
28 |
output = response.json()
|
29 |
return output[0]["generated_text"]
|
30 |
|
31 |
-
# API Key
|
32 |
POLYGON_API_KEY = os.getenv("POLYGON_API_KEY")
|
33 |
|
34 |
-
# Sector Averages
|
35 |
sector_averages = {
|
36 |
"Technology": {"P/E Ratio": 25, "P/S Ratio": 5, "P/B Ratio": 6},
|
37 |
"Healthcare": {"P/E Ratio": 20, "P/S Ratio": 4, "P/B Ratio": 3},
|
@@ -39,8 +31,6 @@ sector_averages = {
|
|
39 |
"Energy": {"P/E Ratio": 12, "P/S Ratio": 1.2, "P/B Ratio": 1.3},
|
40 |
}
|
41 |
|
42 |
-
|
43 |
-
# Safe Request Function
|
44 |
def safe_request(url):
|
45 |
try:
|
46 |
response = requests.get(url)
|
@@ -49,21 +39,16 @@ def safe_request(url):
|
|
49 |
except:
|
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
|
58 |
ps = market_cap / total_revenue if total_revenue else 0
|
59 |
pb = market_cap / book_value if book_value else 0
|
60 |
peg = pe / (growth * 100) if growth else 0
|
61 |
div_yield = (dividend_amount / price) * 100 if price else 0
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
beta = np.random.uniform(0.8, 1.5) # Placeholder: random for now
|
67 |
return {
|
68 |
'P/E Ratio': pe,
|
69 |
'P/S Ratio': ps,
|
@@ -100,15 +85,12 @@ def stock_research(symbol, eps=5.0, growth=0.1, book=500000000):
|
|
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 |
-
# New Recommendation Section
|
104 |
recommendation = "Hold"
|
105 |
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:
|
106 |
recommendation = "Buy"
|
107 |
elif ratios['P/E Ratio'] > 30 or ratios['Debt/Equity Ratio'] > 2.0 or ratios['Return on Equity (%)'] < 5:
|
108 |
recommendation = "Sell"
|
109 |
|
110 |
-
# Detailed report for summarization
|
111 |
-
# Build a full report
|
112 |
report = (
|
113 |
f"Company Overview:\n"
|
114 |
f"Name: {info['Name']}\n"
|
@@ -126,18 +108,15 @@ def stock_research(symbol, eps=5.0, growth=0.1, book=500000000):
|
|
126 |
f"Free Cash Flow: ${ratios['Free Cash Flow ($)']:,.2f}\n"
|
127 |
f"Beta (Volatility): {ratios['Beta (Volatility)']}\n"
|
128 |
)
|
129 |
-
|
130 |
-
# Summarize using Mistral
|
131 |
summary_prompt = f"Summarize this financial report clearly and briefly:\n\n{report}"
|
132 |
ai_summary = query_mistral(summary_prompt)
|
133 |
-
|
134 |
-
# Add recommendation into financial health table
|
135 |
financial_health = pd.concat([
|
136 |
financial_health,
|
137 |
-
pd.DataFrame([{
|
138 |
], ignore_index=True)
|
139 |
-
|
140 |
-
# Return everything
|
141 |
return ai_summary, info_table, ratios_table, financial_health, sector_comp, fig
|
142 |
|
143 |
|
|
|
5 |
import os
|
6 |
import datetime
|
7 |
import tempfile
|
8 |
+
import numpy as np
|
9 |
|
10 |
+
# Your Hugging Face API Token
|
11 |
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
12 |
|
|
|
13 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
14 |
|
|
|
15 |
headers = {
|
16 |
"Authorization": f"Bearer {HF_TOKEN}"
|
17 |
}
|
18 |
|
|
|
19 |
def query_mistral(question):
|
20 |
+
payload = {"inputs": question, "parameters": {"max_length": 256}}
|
|
|
|
|
|
|
21 |
response = requests.post(API_URL, headers=headers, json=payload)
|
22 |
output = response.json()
|
23 |
return output[0]["generated_text"]
|
24 |
|
|
|
25 |
POLYGON_API_KEY = os.getenv("POLYGON_API_KEY")
|
26 |
|
|
|
27 |
sector_averages = {
|
28 |
"Technology": {"P/E Ratio": 25, "P/S Ratio": 5, "P/B Ratio": 6},
|
29 |
"Healthcare": {"P/E Ratio": 20, "P/S Ratio": 4, "P/B Ratio": 3},
|
|
|
31 |
"Energy": {"P/E Ratio": 12, "P/S Ratio": 1.2, "P/B Ratio": 1.3},
|
32 |
}
|
33 |
|
|
|
|
|
34 |
def safe_request(url):
|
35 |
try:
|
36 |
response = requests.get(url)
|
|
|
39 |
except:
|
40 |
return None
|
41 |
|
|
|
|
|
|
|
|
|
42 |
def calculate_ratios(market_cap, total_revenue, price, dividend_amount, eps=5.0, growth=0.1, book_value=500000000):
|
43 |
pe = price / eps if eps else 0
|
44 |
ps = market_cap / total_revenue if total_revenue else 0
|
45 |
pb = market_cap / book_value if book_value else 0
|
46 |
peg = pe / (growth * 100) if growth else 0
|
47 |
div_yield = (dividend_amount / price) * 100 if price else 0
|
48 |
+
debt_equity = np.random.uniform(0.2, 2.0)
|
49 |
+
roe = np.random.uniform(5, 25)
|
50 |
+
free_cash_flow = np.random.uniform(50000000, 500000000)
|
51 |
+
beta = np.random.uniform(0.8, 1.5)
|
|
|
52 |
return {
|
53 |
'P/E Ratio': pe,
|
54 |
'P/S Ratio': ps,
|
|
|
85 |
ratios_table = pd.DataFrame(ratios.items(), columns=["Metric", "Value"])
|
86 |
financial_health = ratios_table[ratios_table["Metric"].isin(["Debt/Equity Ratio", "Return on Equity (%)", "Free Cash Flow ($)", "Beta (Volatility)"])]
|
87 |
|
|
|
88 |
recommendation = "Hold"
|
89 |
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:
|
90 |
recommendation = "Buy"
|
91 |
elif ratios['P/E Ratio'] > 30 or ratios['Debt/Equity Ratio'] > 2.0 or ratios['Return on Equity (%)'] < 5:
|
92 |
recommendation = "Sell"
|
93 |
|
|
|
|
|
94 |
report = (
|
95 |
f"Company Overview:\n"
|
96 |
f"Name: {info['Name']}\n"
|
|
|
108 |
f"Free Cash Flow: ${ratios['Free Cash Flow ($)']:,.2f}\n"
|
109 |
f"Beta (Volatility): {ratios['Beta (Volatility)']}\n"
|
110 |
)
|
111 |
+
|
|
|
112 |
summary_prompt = f"Summarize this financial report clearly and briefly:\n\n{report}"
|
113 |
ai_summary = query_mistral(summary_prompt)
|
114 |
+
|
|
|
115 |
financial_health = pd.concat([
|
116 |
financial_health,
|
117 |
+
pd.DataFrame([{"Metric": "Recommendation", "Value": recommendation}])
|
118 |
], ignore_index=True)
|
119 |
+
|
|
|
120 |
return ai_summary, info_table, ratios_table, financial_health, sector_comp, fig
|
121 |
|
122 |
|