Spaces:
Build error
Build error
Upload 7 files
Browse files- MCAP31122023.xlsx +0 -0
- app.py +72 -0
- document_preprocessor.py +13 -0
- gitignore +5 -0
- llm.py +54 -0
- prompt.py +10 -0
- requirements.txt +9 -0
MCAP31122023.xlsx
ADDED
|
Binary file (108 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import json
|
| 3 |
+
from document_preprocessor import generate_document
|
| 4 |
+
from llm import LLM
|
| 5 |
+
from prompt import stock_analysis_prompt
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from streamlit_searchbox import st_searchbox
|
| 8 |
+
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Stock_Picker",
|
| 11 |
+
page_icon="💰",
|
| 12 |
+
layout="wide",
|
| 13 |
+
initial_sidebar_state="expanded",
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
st.markdown("### 📈 Stock Picker")
|
| 17 |
+
|
| 18 |
+
left_co, cent_co,last_co = st.columns(3)
|
| 19 |
+
with cent_co:
|
| 20 |
+
st.image(image=".streamlit/stock-market.png", width=300)
|
| 21 |
+
|
| 22 |
+
st.markdown("---")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
stocks = pd.read_excel("MCAP31122023.xlsx").set_index('Company Name')
|
| 26 |
+
|
| 27 |
+
url = "https://ticker.finology.in/company/"
|
| 28 |
+
|
| 29 |
+
model = LLM(model_name="Gemini")
|
| 30 |
+
|
| 31 |
+
# function with list of labels
|
| 32 |
+
def search_stocks(searchterm: str):
|
| 33 |
+
if not searchterm:
|
| 34 |
+
return []
|
| 35 |
+
matching_stocks = stocks[stocks.index.str.contains(searchterm, case=False, na=False)]
|
| 36 |
+
return matching_stocks['Symbol'].tolist()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
selected_value = st_searchbox(
|
| 40 |
+
search_stocks,
|
| 41 |
+
key="wiki_searchbox",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if selected_value:
|
| 45 |
+
stock_url = f"https://ticker.finology.in/company/{selected_value}"
|
| 46 |
+
stock_fundamentals = generate_document(stock_url)
|
| 47 |
+
prompt = stock_analysis_prompt.replace(
|
| 48 |
+
"{stock_name}",selected_value).replace("{context}",stock_fundamentals.page_content)
|
| 49 |
+
result = model(prompt=prompt).replace('```',"")
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
res = json.loads(result)
|
| 53 |
+
confidence_score = res['buy']
|
| 54 |
+
analysis = res["detailed_analysis"]
|
| 55 |
+
|
| 56 |
+
if confidence_score >= 75:
|
| 57 |
+
st.success("High Confidence Score!")
|
| 58 |
+
elif confidence_score > 40:
|
| 59 |
+
st.warning("Moderate Confidence Score.")
|
| 60 |
+
else:
|
| 61 |
+
st.error("Low Confidence Score.")
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
col1, col2 = st.columns(2)
|
| 65 |
+
col1.write(f'**Buy Confidence Score:** {str(confidence_score)}')
|
| 66 |
+
|
| 67 |
+
with st.expander("See explanation"):
|
| 68 |
+
st.write(f"**Detailed Analysis:** {analysis}")
|
| 69 |
+
st.markdown(f"[Learn more about {selected_value}]({stock_url})")
|
| 70 |
+
|
| 71 |
+
except:
|
| 72 |
+
st.write(result)
|
document_preprocessor.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.document_loaders import UnstructuredURLLoader
|
| 2 |
+
from langchain.docstore.document import Document
|
| 3 |
+
from unstructured.cleaners.core import remove_punctuation,clean,clean_extra_whitespace
|
| 4 |
+
|
| 5 |
+
def generate_document(url):
|
| 6 |
+
"Given an URL, return a langchain Document to futher processing"
|
| 7 |
+
loader = UnstructuredURLLoader(urls=[url],
|
| 8 |
+
mode="elements",
|
| 9 |
+
post_processors=[clean,remove_punctuation,clean_extra_whitespace])
|
| 10 |
+
elements = loader.load()
|
| 11 |
+
selected_elements = [e for e in elements]
|
| 12 |
+
full_clean = " ".join([e.page_content for e in selected_elements])
|
| 13 |
+
return Document(page_content=full_clean, metadata={"source":url})
|
gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
*.ipynb
|
| 3 |
+
*.csv
|
| 4 |
+
__pycache__
|
| 5 |
+
*.json
|
llm.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
generation_config = {
|
| 8 |
+
"temperature": 0,
|
| 9 |
+
"top_k": 1,
|
| 10 |
+
"max_output_tokens": 4000,
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class LLM:
|
| 15 |
+
def __init__(self, model_name) -> None:
|
| 16 |
+
self.model_name = model_name
|
| 17 |
+
self.model = self.create_model(model_name)
|
| 18 |
+
|
| 19 |
+
def create_model(self, model_name):
|
| 20 |
+
match model_name:
|
| 21 |
+
case "GeminiVision":
|
| 22 |
+
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
| 23 |
+
return genai.GenerativeModel('gemini-pro-vision')
|
| 24 |
+
case "Gemini":
|
| 25 |
+
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
| 26 |
+
return genai.GenerativeModel(
|
| 27 |
+
'gemini-pro',generation_config=generation_config)
|
| 28 |
+
|
| 29 |
+
case _:
|
| 30 |
+
print("Not Implemented")
|
| 31 |
+
|
| 32 |
+
def __call__(self, prompt, image=None):
|
| 33 |
+
if self.model_name == 'GeminiVision':
|
| 34 |
+
response = self.model.generate_content(
|
| 35 |
+
[image, prompt]
|
| 36 |
+
)
|
| 37 |
+
elif self.model_name == "Gemini":
|
| 38 |
+
response = self.model.generate_content(
|
| 39 |
+
prompt)
|
| 40 |
+
# print(response.text)
|
| 41 |
+
return response.text
|
| 42 |
+
|
| 43 |
+
elif self.model_name == 'openai':
|
| 44 |
+
res = self.model.chat.completions.create(
|
| 45 |
+
model="gpt-3.5-turbo-1106",
|
| 46 |
+
response_format={"type": "json_object"},
|
| 47 |
+
messages=[
|
| 48 |
+
# {"role": "system", "content": "You are a helpful assistant."},
|
| 49 |
+
{"role": "user", "content": f"{prompt}"},
|
| 50 |
+
],
|
| 51 |
+
# seed=10,
|
| 52 |
+
temperature=0
|
| 53 |
+
)
|
| 54 |
+
return res.choices[0].message.content
|
prompt.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
stock_analysis_prompt = """
|
| 2 |
+
You are a financial advisor which help ascertain whether the fundamentals of the
|
| 3 |
+
stock is good enough for me to buy
|
| 4 |
+
Stock name : {stock_name}
|
| 5 |
+
The stock details is given below:
|
| 6 |
+
{context}
|
| 7 |
+
You should carry weight to latest news from the context for analysis
|
| 8 |
+
Your output should be in JSON format with keys buy whose values should be a confidence score between 0-100
|
| 9 |
+
and detailed analysis of your decision to buy or not
|
| 10 |
+
"""
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.30.0
|
| 2 |
+
streamlit-searchbox==0.1.6
|
| 3 |
+
google-generativeai==0.3.2
|
| 4 |
+
langchain==0.1.4
|
| 5 |
+
langchain-community==0.0.16
|
| 6 |
+
# unstructured[docx,pptx]
|
| 7 |
+
unstructured[all-docs]
|
| 8 |
+
python-dotenv==1.0.1
|
| 9 |
+
openpyxl==3.1.2
|