Karthikeyen92
commited on
Commit
•
a260490
1
Parent(s):
f1b4891
Update app.py
Browse files
app.py
CHANGED
@@ -250,12 +250,14 @@ class StockAdviser:
|
|
250 |
return cmp_tkr
|
251 |
|
252 |
|
253 |
-
def process_historical_data(self, user_question):
|
254 |
cmp_tr = self.get_symbol(user_question)
|
255 |
|
256 |
# Initialize ChromaDB Database
|
257 |
-
chroma_db = DBStorage()
|
258 |
-
FAISS_DB_PATH = os.path.join(os.getcwd(), "faiss_HD")
|
|
|
|
|
259 |
chroma_db.load_vectors(FAISS_DB_PATH)
|
260 |
context_for_query = chroma_db.get_context_for_query(cmp_tr, k=5)
|
261 |
|
@@ -272,44 +274,54 @@ class StockAdviser:
|
|
272 |
print(f"\nFetching {days} days of stock data for {cmp_tr}...")
|
273 |
df, analysis = self.get_nse_stock_data(cmp_tr, days)
|
274 |
|
275 |
-
print(analysis)
|
276 |
-
|
277 |
-
|
278 |
-
col1, col2, col3 = st.columns(3)
|
279 |
-
|
280 |
-
# Simulate some metric data (replace with real data in production)
|
281 |
-
with col1:
|
282 |
-
self._create_metric_card(f"52-Week High on {analysis['week_52_high_date']}",
|
283 |
-
f"₹{analysis['week_52_high']:,.2f}",
|
284 |
-
self.format_percentage(analysis['pct_from_52w_high']))
|
285 |
-
with col2:
|
286 |
-
self._create_metric_card(f"52-Week Low on {analysis['week_52_low_date']}",
|
287 |
-
f"₹{analysis['week_52_low']:,.2f}",
|
288 |
-
self.format_percentage(analysis['pct_from_52w_low']))
|
289 |
-
with col3:
|
290 |
-
self._create_metric_card("Average Volume",
|
291 |
-
f"{int(analysis['avg_volume']):,}",
|
292 |
-
f"{self.format_percentage(analysis['volume_pct_diff'])}")
|
293 |
-
|
294 |
-
# Display price chart
|
295 |
-
st.plotly_chart(self.visualizer.create_price_chart(df, cmp_tr))
|
296 |
-
|
297 |
-
# Display volume chart
|
298 |
-
st.plotly_chart(self.visualizer.create_volume_chart(df, cmp_tr))
|
299 |
|
300 |
-
|
301 |
-
|
302 |
-
if sentiment == "Negative":
|
303 |
-
sentiment_score = np.random.uniform(-1, -0.75)
|
304 |
-
elif sentiment == "Neutral":
|
305 |
-
sentiment_score = np.random.uniform(-0.75, 0.25)
|
306 |
-
elif sentiment == "Positive":
|
307 |
-
sentiment_score = np.random.uniform(0.25, 1)
|
308 |
-
else:
|
309 |
-
sentiment_score = 0
|
310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
|
312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
|
314 |
def get_nse_stock_data(self,symbol, days):
|
315 |
"""
|
@@ -392,14 +404,14 @@ class StockAdviser:
|
|
392 |
|
393 |
except Exception as e:
|
394 |
print(f"Error fetching data: {str(e)}")
|
395 |
-
return
|
396 |
|
397 |
def format_percentage(self, value):
|
398 |
"""Format percentage with + or - sign"""
|
399 |
return f"+{value:.2f}%" if value > 0 else f"{value:.2f}%"
|
400 |
|
401 |
|
402 |
-
def process_realtime_data(self, cmp_tr):
|
403 |
if cmp_tr == "NOTICKER":
|
404 |
st.write("No valid company in the query.")
|
405 |
return
|
@@ -435,8 +447,12 @@ class StockAdviser:
|
|
435 |
query_context.extend(data_fetch.search_news(cmp_tr, 100))
|
436 |
|
437 |
# Process collected data
|
438 |
-
db_store = DBStorage()
|
439 |
-
FAISS_DB_PATH = os.path.join(os.getcwd(), "faiss_RD")
|
|
|
|
|
|
|
|
|
440 |
db_store.embed_vectors(to_documents(query_context), FAISS_DB_PATH)
|
441 |
|
442 |
db_store.load_vectors(FAISS_DB_PATH)
|
@@ -578,7 +594,7 @@ class StockAdviser:
|
|
578 |
return base_prompt + response_format + common_format
|
579 |
|
580 |
|
581 |
-
def main():
|
582 |
adviser = StockAdviser()
|
583 |
|
584 |
|
@@ -630,13 +646,13 @@ def main():
|
|
630 |
st.markdown("<h2 class='column-header'>Historical Analysis</h2>", unsafe_allow_html=True)
|
631 |
with st.container():
|
632 |
if user_question:
|
633 |
-
cmp_tr = adviser.process_historical_data(user_question)
|
634 |
|
635 |
with col2:
|
636 |
st.markdown("<h2 class='column-header'>Real-Time Analysis</h2>", unsafe_allow_html=True)
|
637 |
with st.container():
|
638 |
if user_question:
|
639 |
-
sentiment_response = adviser.process_realtime_data(cmp_tr)
|
640 |
|
641 |
if (str(cmp_tr) is not "NOTICKER"):
|
642 |
with st.container():
|
@@ -647,4 +663,5 @@ def main():
|
|
647 |
st.markdown("<p style='text-align: center; color: #666;'>© 2024 EY</p>", unsafe_allow_html=True)
|
648 |
|
649 |
if __name__ == "__main__":
|
650 |
-
|
|
|
|
250 |
return cmp_tkr
|
251 |
|
252 |
|
253 |
+
def process_historical_data(self, user_question, hugg = False):
|
254 |
cmp_tr = self.get_symbol(user_question)
|
255 |
|
256 |
# Initialize ChromaDB Database
|
257 |
+
chroma_db = DBStorage(hugg)
|
258 |
+
FAISS_DB_PATH = os.path.join(os.getcwd(), "Stock Sentiment Analysis", "faiss_HD")
|
259 |
+
if hugg:
|
260 |
+
FAISS_DB_PATH = os.path.join(os.getcwd(), "faiss_HD")
|
261 |
chroma_db.load_vectors(FAISS_DB_PATH)
|
262 |
context_for_query = chroma_db.get_context_for_query(cmp_tr, k=5)
|
263 |
|
|
|
274 |
print(f"\nFetching {days} days of stock data for {cmp_tr}...")
|
275 |
df, analysis = self.get_nse_stock_data(cmp_tr, days)
|
276 |
|
277 |
+
print("df,analysis")
|
278 |
+
print(len(df))
|
279 |
+
print(len(analysis))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
+
if len(analysis) != 0:
|
282 |
+
# Create metrics cards
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
|
284 |
+
col0, col1, col2, col3 = st.columns(4)
|
285 |
+
# Simulate some metric data (replace with real data in production)\
|
286 |
+
with col0:
|
287 |
+
st.markdown(f"""
|
288 |
+
<div class="metric-card">
|
289 |
+
<div class="metric-title">current price & volume</div>
|
290 |
+
<div class="metric-value">₹{analysis['current_price']:,}</div>
|
291 |
+
<div>{int(analysis['current_volume']):,}</div>
|
292 |
+
</div>
|
293 |
+
""", unsafe_allow_html=True)
|
294 |
+
with col1:
|
295 |
+
self._create_metric_card(f"52-Week High on {analysis['week_52_high_date']}",
|
296 |
+
f"₹{analysis['week_52_high']:,.2f}",
|
297 |
+
self.format_percentage(analysis['pct_from_52w_high']))
|
298 |
+
with col2:
|
299 |
+
self._create_metric_card(f"52-Week Low on {analysis['week_52_low_date']}",
|
300 |
+
f"₹{analysis['week_52_low']:,.2f}",
|
301 |
+
self.format_percentage(analysis['pct_from_52w_low']))
|
302 |
+
with col3:
|
303 |
+
self._create_metric_card("Average Volume",
|
304 |
+
f"{int(analysis['avg_volume']):,}",
|
305 |
+
f"{self.format_percentage(analysis['volume_pct_diff'])}")
|
306 |
+
|
307 |
+
# Display price chart
|
308 |
+
st.plotly_chart(self.visualizer.create_price_chart(df, cmp_tr))
|
309 |
|
310 |
+
# Display volume chart
|
311 |
+
st.plotly_chart(self.visualizer.create_volume_chart(df, cmp_tr))
|
312 |
+
|
313 |
+
# Display sentiment gauge (simulate sentiment score)
|
314 |
+
# Generating random score for Demo purpose
|
315 |
+
if sentiment == "Negative":
|
316 |
+
sentiment_score = np.random.uniform(-1, -0.75)
|
317 |
+
elif sentiment == "Neutral":
|
318 |
+
sentiment_score = np.random.uniform(-0.75, 0.25)
|
319 |
+
elif sentiment == "Positive":
|
320 |
+
sentiment_score = np.random.uniform(0.25, 1)
|
321 |
+
else:
|
322 |
+
sentiment_score = 0
|
323 |
+
|
324 |
+
st.plotly_chart(self.visualizer.create_sentiment_gauge(sentiment_score))
|
325 |
|
326 |
def get_nse_stock_data(self,symbol, days):
|
327 |
"""
|
|
|
404 |
|
405 |
except Exception as e:
|
406 |
print(f"Error fetching data: {str(e)}")
|
407 |
+
return [], []
|
408 |
|
409 |
def format_percentage(self, value):
|
410 |
"""Format percentage with + or - sign"""
|
411 |
return f"+{value:.2f}%" if value > 0 else f"{value:.2f}%"
|
412 |
|
413 |
|
414 |
+
def process_realtime_data(self, cmp_tr, hugg = False):
|
415 |
if cmp_tr == "NOTICKER":
|
416 |
st.write("No valid company in the query.")
|
417 |
return
|
|
|
447 |
query_context.extend(data_fetch.search_news(cmp_tr, 100))
|
448 |
|
449 |
# Process collected data
|
450 |
+
db_store = DBStorage(hugg)
|
451 |
+
FAISS_DB_PATH = os.path.join(os.getcwd(), "Stock Sentiment Analysis", "faiss_RD")
|
452 |
+
|
453 |
+
if hugg:
|
454 |
+
FAISS_DB_PATH = os.path.join(os.getcwd(), "faiss_RD")
|
455 |
+
|
456 |
db_store.embed_vectors(to_documents(query_context), FAISS_DB_PATH)
|
457 |
|
458 |
db_store.load_vectors(FAISS_DB_PATH)
|
|
|
594 |
return base_prompt + response_format + common_format
|
595 |
|
596 |
|
597 |
+
def main(hugg):
|
598 |
adviser = StockAdviser()
|
599 |
|
600 |
|
|
|
646 |
st.markdown("<h2 class='column-header'>Historical Analysis</h2>", unsafe_allow_html=True)
|
647 |
with st.container():
|
648 |
if user_question:
|
649 |
+
cmp_tr = adviser.process_historical_data(user_question, hugg)
|
650 |
|
651 |
with col2:
|
652 |
st.markdown("<h2 class='column-header'>Real-Time Analysis</h2>", unsafe_allow_html=True)
|
653 |
with st.container():
|
654 |
if user_question:
|
655 |
+
sentiment_response = adviser.process_realtime_data(cmp_tr, hugg)
|
656 |
|
657 |
if (str(cmp_tr) is not "NOTICKER"):
|
658 |
with st.container():
|
|
|
663 |
st.markdown("<p style='text-align: center; color: #666;'>© 2024 EY</p>", unsafe_allow_html=True)
|
664 |
|
665 |
if __name__ == "__main__":
|
666 |
+
hugg = False
|
667 |
+
main(hugg)
|