Update app.py
Browse files
app.py
CHANGED
|
@@ -26,17 +26,17 @@ import os
|
|
| 26 |
import sys
|
| 27 |
import subprocess
|
| 28 |
|
| 29 |
-
|
| 30 |
import requests
|
| 31 |
from bs4 import BeautifulSoup
|
| 32 |
|
|
|
|
| 33 |
def scrape_website(url):
|
| 34 |
headers = {
|
| 35 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
| 36 |
}
|
| 37 |
-
|
| 38 |
response = requests.get(url, headers=headers)
|
| 39 |
-
|
| 40 |
if response.status_code == 200:
|
| 41 |
soup = BeautifulSoup(response.text, "html.parser")
|
| 42 |
return soup.prettify() # Returns the full parsed HTML
|
|
@@ -58,6 +58,7 @@ if uploaded_file is not None:
|
|
| 58 |
stock_data = json.load(uploaded_file)
|
| 59 |
st.success("Data loaded successfully!")
|
| 60 |
|
|
|
|
| 61 |
# Configuration Class
|
| 62 |
class Config:
|
| 63 |
ALPHA_VANTAGE_API_KEY = os.getenv("ALPHA_VANTAGE_API_KEY")
|
|
@@ -79,16 +80,18 @@ if uploaded_file is not None:
|
|
| 79 |
"Real Estate",
|
| 80 |
"Utilities"
|
| 81 |
]
|
| 82 |
-
|
|
|
|
| 83 |
# Create directories if they don't exist
|
| 84 |
os.makedirs(Config.STOCK_DATA_DIR, exist_ok=True)
|
| 85 |
os.makedirs(os.path.dirname(Config.OUTPUT_FILE), exist_ok=True)
|
| 86 |
os.makedirs(os.path.dirname(Config.SCENARIO_OUTPUT_FILE), exist_ok=True)
|
| 87 |
|
|
|
|
| 88 |
def configure_generative_ai():
|
| 89 |
"""Configures the generative AI model and starts a chat session."""
|
| 90 |
genai.configure(api_key=Config.GOOGLE_API_KEY)
|
| 91 |
-
|
| 92 |
generation_config = {
|
| 93 |
"temperature": 1,
|
| 94 |
"top_p": 0.95,
|
|
@@ -96,13 +99,15 @@ if uploaded_file is not None:
|
|
| 96 |
"max_output_tokens": 8192,
|
| 97 |
"response_mime_type": "text/plain",
|
| 98 |
}
|
| 99 |
-
|
| 100 |
model = genai.GenerativeModel(
|
| 101 |
model_name="gemini-2.0-flash-exp",
|
| 102 |
generation_config=generation_config,
|
| 103 |
)
|
| 104 |
-
|
| 105 |
return model.start_chat()
|
|
|
|
|
|
|
| 106 |
# Fetch stock data
|
| 107 |
st.write("Fetching stock data...")
|
| 108 |
stock_symbols = [value["symbol"] for value in stock_data.values()]
|
|
@@ -141,13 +146,50 @@ if uploaded_file is not None:
|
|
| 141 |
st.write("Extracting market scenarios...")
|
| 142 |
# context_data = asyncio.run(extract_text_from_website(url))
|
| 143 |
context_data = scrape_website(url)
|
|
|
|
| 144 |
st.success("Market context extracted successfully!")
|
| 145 |
|
| 146 |
# Generate scenario prompt
|
| 147 |
scenario_prompt = f"""
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
chat_session = configure_generative_ai()
|
| 153 |
|
|
@@ -176,4 +218,4 @@ if uploaded_file is not None:
|
|
| 176 |
st.write("Download Output Files")
|
| 177 |
for file in [Config.OUTPUT_FILE, Config.SCENARIO_OUTPUT_FILE, simulation_results_file]:
|
| 178 |
with open(file, "rb") as f:
|
| 179 |
-
st.download_button(label=f"Download {file.split('/')[-1]}", data=f, file_name=file.split('/')[-1])
|
|
|
|
| 26 |
import sys
|
| 27 |
import subprocess
|
| 28 |
|
|
|
|
| 29 |
import requests
|
| 30 |
from bs4 import BeautifulSoup
|
| 31 |
|
| 32 |
+
|
| 33 |
def scrape_website(url):
|
| 34 |
headers = {
|
| 35 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
| 36 |
}
|
| 37 |
+
|
| 38 |
response = requests.get(url, headers=headers)
|
| 39 |
+
|
| 40 |
if response.status_code == 200:
|
| 41 |
soup = BeautifulSoup(response.text, "html.parser")
|
| 42 |
return soup.prettify() # Returns the full parsed HTML
|
|
|
|
| 58 |
stock_data = json.load(uploaded_file)
|
| 59 |
st.success("Data loaded successfully!")
|
| 60 |
|
| 61 |
+
|
| 62 |
# Configuration Class
|
| 63 |
class Config:
|
| 64 |
ALPHA_VANTAGE_API_KEY = os.getenv("ALPHA_VANTAGE_API_KEY")
|
|
|
|
| 80 |
"Real Estate",
|
| 81 |
"Utilities"
|
| 82 |
]
|
| 83 |
+
|
| 84 |
+
|
| 85 |
# Create directories if they don't exist
|
| 86 |
os.makedirs(Config.STOCK_DATA_DIR, exist_ok=True)
|
| 87 |
os.makedirs(os.path.dirname(Config.OUTPUT_FILE), exist_ok=True)
|
| 88 |
os.makedirs(os.path.dirname(Config.SCENARIO_OUTPUT_FILE), exist_ok=True)
|
| 89 |
|
| 90 |
+
|
| 91 |
def configure_generative_ai():
|
| 92 |
"""Configures the generative AI model and starts a chat session."""
|
| 93 |
genai.configure(api_key=Config.GOOGLE_API_KEY)
|
| 94 |
+
|
| 95 |
generation_config = {
|
| 96 |
"temperature": 1,
|
| 97 |
"top_p": 0.95,
|
|
|
|
| 99 |
"max_output_tokens": 8192,
|
| 100 |
"response_mime_type": "text/plain",
|
| 101 |
}
|
| 102 |
+
|
| 103 |
model = genai.GenerativeModel(
|
| 104 |
model_name="gemini-2.0-flash-exp",
|
| 105 |
generation_config=generation_config,
|
| 106 |
)
|
| 107 |
+
|
| 108 |
return model.start_chat()
|
| 109 |
+
|
| 110 |
+
|
| 111 |
# Fetch stock data
|
| 112 |
st.write("Fetching stock data...")
|
| 113 |
stock_symbols = [value["symbol"] for value in stock_data.values()]
|
|
|
|
| 146 |
st.write("Extracting market scenarios...")
|
| 147 |
# context_data = asyncio.run(extract_text_from_website(url))
|
| 148 |
context_data = scrape_website(url)
|
| 149 |
+
print(context_data)
|
| 150 |
st.success("Market context extracted successfully!")
|
| 151 |
|
| 152 |
# Generate scenario prompt
|
| 153 |
scenario_prompt = f"""
|
| 154 |
+
# TASK: Analyze market context and identify potential market scenarios.
|
| 155 |
+
|
| 156 |
+
# CONTEXT:
|
| 157 |
+
{context_data}
|
| 158 |
+
# END CONTEXT
|
| 159 |
+
|
| 160 |
+
# INSTRUCTION: Based on the provided market context, analyze and identify up to three plausible market scenarios.
|
| 161 |
+
# For each scenario, determine its name (e.g., "Moderate Downturn"), the general market direction ("up" or "down"), a major trigger point that could cause the scenario to unfold, and a list of sectors that would be significantly impacted. Each 'sector_impact' list should have less than or equal to 4 sectors.
|
| 162 |
+
|
| 163 |
+
# OUTPUT FORMAT: Provide the analysis in JSON format with the following structure.
|
| 164 |
+
# Use the sector names provided:
|
| 165 |
+
{sectors}
|
| 166 |
+
|
| 167 |
+
# EXAMPLE:
|
| 168 |
+
|
| 169 |
+
json
|
| 170 |
+
{{
|
| 171 |
+
"market_scenarios": {{
|
| 172 |
+
"scenario1": {{
|
| 173 |
+
"name": "Moderate Downturn",
|
| 174 |
+
"direction": "down",
|
| 175 |
+
"trigger": "Interest rate hike",
|
| 176 |
+
"sector_impact": [
|
| 177 |
+
"Financials",
|
| 178 |
+
"Energy"
|
| 179 |
+
]
|
| 180 |
+
}},
|
| 181 |
+
"scenario2": {{
|
| 182 |
+
"name": "Bullish Growth",
|
| 183 |
+
"direction": "up",
|
| 184 |
+
"trigger": "Successful vaccine rollout",
|
| 185 |
+
"sector_impact": [
|
| 186 |
+
"Health Care",
|
| 187 |
+
"Information Technology"
|
| 188 |
+
]
|
| 189 |
+
}}
|
| 190 |
+
}}
|
| 191 |
+
}}
|
| 192 |
+
"""
|
| 193 |
|
| 194 |
chat_session = configure_generative_ai()
|
| 195 |
|
|
|
|
| 218 |
st.write("Download Output Files")
|
| 219 |
for file in [Config.OUTPUT_FILE, Config.SCENARIO_OUTPUT_FILE, simulation_results_file]:
|
| 220 |
with open(file, "rb") as f:
|
| 221 |
+
st.download_button(label=f"Download {file.split('/')[-1]}", data=f, file_name=file.split('/')[-1])
|