Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import boto3
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
import random
|
| 6 |
+
import requests
|
| 7 |
+
from bs4 import BeautifulSoup
|
| 8 |
+
from flask import Flask, render_template, jsonify
|
| 9 |
+
import threading
|
| 10 |
+
|
| 11 |
+
# AWS Credentials and Region
|
| 12 |
+
AWS_ACCESS_KEY_ID = "AKIATFBMPLLTCCPHI56U" # Verify or replace
|
| 13 |
+
AWS_SECRET_ACCESS_KEY = "SzHSXlxassWvt5eRGHRoDTxStXH+Ay670QRrMQ44" # Verify or replace
|
| 14 |
+
AWS_REGION = "ap-south-1" # Try 'us-east-1' if issues persist
|
| 15 |
+
|
| 16 |
+
# Bedrock client setup
|
| 17 |
+
bedrock = boto3.client(
|
| 18 |
+
service_name='bedrock-runtime',
|
| 19 |
+
region_name=AWS_REGION,
|
| 20 |
+
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
| 21 |
+
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Flask app setup
|
| 25 |
+
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 26 |
+
|
| 27 |
+
# List of topics
|
| 28 |
+
TOPICS = [
|
| 29 |
+
"Market trends",
|
| 30 |
+
"IPO",
|
| 31 |
+
"Mutual funds",
|
| 32 |
+
"Cryptocurrency",
|
| 33 |
+
"Economy",
|
| 34 |
+
"Banking",
|
| 35 |
+
"Government policies and budget",
|
| 36 |
+
"Gold and Silver"
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
# Model configurations
|
| 40 |
+
MODELS = [
|
| 41 |
+
{
|
| 42 |
+
"modelId": "mistral.mixtral-8x7b-instruct-v0:1",
|
| 43 |
+
"contentType": "application/json",
|
| 44 |
+
"accept": "application/json",
|
| 45 |
+
"body_template": {
|
| 46 |
+
"prompt": "<s>[INST] {prompt} [/INST]",
|
| 47 |
+
"max_tokens": 100,
|
| 48 |
+
"temperature": 0.5,
|
| 49 |
+
"top_p": 0.9,
|
| 50 |
+
"top_k": 50
|
| 51 |
+
}
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"modelId": "mistral.mistral-7b-instruct-v0:2",
|
| 55 |
+
"contentType": "application/json",
|
| 56 |
+
"accept": "application/json",
|
| 57 |
+
"body_template": {
|
| 58 |
+
"prompt": "<s>[INST] {prompt} [/INST]",
|
| 59 |
+
"max_tokens": 100,
|
| 60 |
+
"temperature": 0.5,
|
| 61 |
+
"top_p": 0.9,
|
| 62 |
+
"top_k": 50
|
| 63 |
+
}
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"modelId": "meta.llama3-8b-instruct-v1:0",
|
| 67 |
+
"contentType": "application/json",
|
| 68 |
+
"accept": "application/json",
|
| 69 |
+
"body_template": {
|
| 70 |
+
"prompt": "{prompt}",
|
| 71 |
+
"max_gen_len": 128,
|
| 72 |
+
"temperature": 0.5,
|
| 73 |
+
"top_p": 0.9
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
WEBSITES = [
|
| 79 |
+
"https://economictimes.indiatimes.com/markets/stocks/news",
|
| 80 |
+
"https://www.moneycontrol.com/news/business/markets/",
|
| 81 |
+
"https://www.business-standard.com/markets-news",
|
| 82 |
+
"https://www.hindustantimes.com/business",
|
| 83 |
+
"https://indianexpress.com/section/business/",
|
| 84 |
+
"https://www.ndtvprofit.com/markets",
|
| 85 |
+
"https://www.zeebiz.com/markets",
|
| 86 |
+
"https://www.livemint.com/market/market-stats",
|
| 87 |
+
"https://www.financialexpress.com/market/",
|
| 88 |
+
"https://www.cnbctv18.com/market/",
|
| 89 |
+
"https://www.businesstoday.in/markets",
|
| 90 |
+
"https://economictimes.indiatimes.com/markets",
|
| 91 |
+
"https://www.indiainfoline.com/news/market",
|
| 92 |
+
"https://www.reuters.com/markets/asia/",
|
| 93 |
+
"https://www.bloomberg.com/asia"
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
# Global variable to store news articles
|
| 97 |
+
latest_articles = []
|
| 98 |
+
|
| 99 |
+
def fetch_from_websites(topic, current_time, time_24h_ago):
|
| 100 |
+
articles = []
|
| 101 |
+
headers = {'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'}
|
| 102 |
+
used_headlines = set()
|
| 103 |
+
|
| 104 |
+
for url in WEBSITES:
|
| 105 |
+
try:
|
| 106 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 107 |
+
response.raise_for_status()
|
| 108 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 109 |
+
|
| 110 |
+
news_items = soup.select('h1, h2, h3, .story, .article, .headline, .title')
|
| 111 |
+
for item in news_items:
|
| 112 |
+
if len(articles) >= 10:
|
| 113 |
+
break
|
| 114 |
+
title = item.get_text(strip=True)[:30]
|
| 115 |
+
if title and title not in used_headlines and topic.lower() in title.lower():
|
| 116 |
+
used_headlines.add(title)
|
| 117 |
+
time_diff = random.randint(0, 24 * 60 * 60)
|
| 118 |
+
pub_date = (current_time - timedelta(seconds=time_diff)).strftime("%Y-%m-%dT%H:%M:%SZ")
|
| 119 |
+
|
| 120 |
+
desc_elem = item.find_next('p') or item.find_next('div', class_=['summary', 'content', 'desc'])
|
| 121 |
+
raw_desc = desc_elem.get_text(strip=True) if desc_elem else f"Latest {topic} update from {url
|