|
|
|
|
|
import requests
|
|
|
from bs4 import BeautifulSoup
|
|
|
import nest_asyncio
|
|
|
import asyncio
|
|
|
import json
|
|
|
import re
|
|
|
from crawl4ai import *
|
|
|
import os
|
|
|
from dotenv import load_dotenv
|
|
|
import google.generativeai as genai
|
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
|
|
|
|
|
|
|
|
nest_asyncio.apply()
|
|
|
|
|
|
|
|
|
async def extract_text_from_website(url):
|
|
|
async with AsyncWebCrawler() as crawler:
|
|
|
result = await crawler.arun(url=url)
|
|
|
return result.markdown
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
genai.configure(api_key=GOOGLE_API_KEY)
|
|
|
|
|
|
generation_config = {
|
|
|
"temperature": 1,
|
|
|
"top_p": 0.95,
|
|
|
"top_k": 40,
|
|
|
"max_output_tokens": 8192,
|
|
|
"response_mime_type": "text/plain",
|
|
|
}
|
|
|
|
|
|
model = genai.GenerativeModel(
|
|
|
model_name="gemini-2.0-flash-exp",
|
|
|
generation_config=generation_config,
|
|
|
)
|
|
|
|
|
|
chat_session = model.start_chat()
|
|
|
|
|
|
|
|
|
def get_response(llm, prompt):
|
|
|
response = llm.send_message(prompt)
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_json_content(text):
|
|
|
match = re.search(r"```json\n(.*?)```", text, re.DOTALL)
|
|
|
if match:
|
|
|
return match.group(1).strip()
|
|
|
else:
|
|
|
return None
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
url = "https://www.livemint.com/market/stock-market-news/page-7"
|
|
|
context_data = asyncio.run(extract_text_from_website(url))
|
|
|
|
|
|
|
|
|
sectors = [
|
|
|
"Communication Services",
|
|
|
"Consumer Discretionary",
|
|
|
"Consumer Staples",
|
|
|
"Energy",
|
|
|
"Financials",
|
|
|
"Health Care",
|
|
|
"Industrials",
|
|
|
"Information Technology",
|
|
|
"Materials",
|
|
|
"Real Estate",
|
|
|
"Utilities",
|
|
|
]
|
|
|
|
|
|
prompt = f"""
|
|
|
# TASK: Analyze market context and identify potential market scenarios.
|
|
|
|
|
|
# CONTEXT:
|
|
|
{context_data}
|
|
|
# END CONTEXT
|
|
|
|
|
|
# INSTRUCTION: Based on the provided market context, analyze and identify up to three plausible market scenarios.
|
|
|
# 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.
|
|
|
|
|
|
# OUTPUT FORMAT: Provide the analysis in JSON format with the following structure.
|
|
|
# Use the sector names provided:
|
|
|
{sectors}
|
|
|
|
|
|
# EXAMPLE:
|
|
|
```json
|
|
|
{{
|
|
|
"market_scenarios": {{
|
|
|
"scenario1": {{
|
|
|
"name": "Moderate Downturn",
|
|
|
"direction": "down",
|
|
|
"trigger": "Interest rate hike",
|
|
|
"sector_impact": [
|
|
|
"Financials",
|
|
|
"Energy"
|
|
|
]
|
|
|
}},
|
|
|
"scenario2": {{
|
|
|
"name": "Bullish Growth",
|
|
|
"direction": "up",
|
|
|
"trigger": "Successful vaccine rollout",
|
|
|
"sector_impact": [
|
|
|
"Health Care",
|
|
|
"Information Technology"
|
|
|
]
|
|
|
}}
|
|
|
}}
|
|
|
}}
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
answer = get_response(chat_session, prompt)
|
|
|
|
|
|
|
|
|
json_output = extract_json_content(answer.text)
|
|
|
|
|
|
|
|
|
output_file = "output_files/scenario.json"
|
|
|
|
|
|
|
|
|
try:
|
|
|
analysis_json = json.loads(json_output)
|
|
|
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
|
|
with open(output_file, "w") as f:
|
|
|
json.dump(analysis_json, f, indent=4)
|
|
|
print(f"Analysis saved to '{output_file}'")
|
|
|
except json.JSONDecodeError:
|
|
|
print("Error: Could not decode the output from the model into JSON format.")
|
|
|
except Exception as e:
|
|
|
print(f"Error: {e}")
|
|
|
|