-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
153 lines (140 loc) · 5.49 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from flask import Flask,request
from flask_cors import CORS
import sqlalchemy as sa
from sqlalchemy.orm import DeclarativeBase
from openai import OpenAI
import requests
import time
import json
import threading
import re
import praw
import threading
import yfinance as yf
import requests
import mongoengine as mongoose
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
return "Flask server is running!"
@app.route('/api/save-tkr')
def send_json():
return getComments(reddit.subreddit("all"), company_name)
@app.route('/add_todo', methods=['POST'])
def add_todo():
todo_data = request.get_json()
new_todo = Todo(content=todo_data['content'])
sa.session.add(new_todo)
sa.session.commit()
return 'Done', 201
class Todo(DeclarativeBase):
id = sa.Column(sa.Integer, primary_key=True)
type = sa.Column(sa.String)
def extract_text(log):
# Use a regular expression to find the text after 'text': ' and before the next '
match = re.search(r"'text': '([^']*)'", log)
if match:
return match.group(1)
return None
def remove_emoji(text):
#removes regular emojis
RE_EMOJI = re.compile(u'([\U00002600-\U000027BF])|([\U0001f300-\U0001f64F])|([\U0001f680-\U0001f6FF])')
text = RE_EMOJI.sub(r'', text)
#returns the emojis of the format [emoji](img|string1|string2)
return re.sub(r'\[.*?\)', '', text)
@app.route('/submit', methods=['POST'])
def submit():
ticker = request.json
# print(json.dumps({"message": "Data received", "data": ticker}))
tkr = ticker.get('text')
valid_ticker=True
#accept input from react
company = yf.Ticker(tkr)
try:
company_name = company.info['longName'].split(" ")[0].split(".")[0].lower()
getComments(subreddit=reddit.subreddit("all"),text=company_name)
except:
valid_ticker=False
print("Ticker does not exist")
# print(company)
# getComments(subreddit=reddit.subreddit("all"),text=company)
return json.dumps({"message": "Data received", "data": ticker})
class ExampleSchema(mongoose.Document):
ticker = mongoose.StringField(required=True)
sentiments = mongoose.ListField(mongoose.IntField(), required=True)
latest_sentiment = mongoose.StringField(required=True)
meta = {
'collection': 'sentiment' # Name of the collection in MongoDB
}
def getComments(subreddit, text) -> None:
client = OpenAI(api_key=YOUR_API_KEY, base_url="https://api.perplexity.ai")
messages = [
{
"role": "system",
"content": (
"You are an expert in marketing and assessing the sentiment of reviews. I am going to give you reviews to respond with only 'positive', 'negative', or 'neutral' towards the use of " + text + ". If you cannot create content about the review, rate it as neutral"
),
},
{
"role": "user",
"content": (
""
),
},
]
for comment in subreddit.stream.comments():
if text in comment.body.lower():
comment.body = remove_emoji(comment.body)
messages[1]["content"] = comment.body
response = client.chat.completions.create(
model="llama-3.1-8b-instruct",
messages=messages,
)
analysis = response.choices[0].message.content
print(analysis)
if not (analysis == 'Neutral' or analysis == 'Positive' or analysis == 'Negative'):
continue
try:
number = 0 if analysis == 'Neutral' else 1 if analysis == 'Positive' else -1
sentiments.append(number)
# producer.send("redditcomments", value=comment_json)
comment_json = {
"tkr": tkr,
"sentiment": sentiments,
"latestSentiment": analysis
}
collection_name.insert_one(comment_json)
print(number)
# Save the document to the database
except Exception as e:
print("An error occurred:", str(e))
return json
# Run the Flask app and start the POST request loop in a separate thread
if __name__ == "__main__":
tkr = ""
reddit = praw.Reddit(
client_id="_lXa7uHKe5fOpKVnOQlktA",
client_secret="OsWBMbhI5QO6fQdBw-WsGYnwDiHeiw",
password="DJAJASFINANCIALSERVICES",
user_agent="testscript by u/fakebot3",
username="Sad_Warning869",
ratelimit_seconds=.75)
sentiments = []
YOUR_API_KEY = "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"
uri = "mongodb+srv://ashritramanala:X2f1pLPy48ZFal1s@vandyhackscluster.h7isr.mongodb.net/?retryWrites=true&w=majority&appName=VandyHacksCluster"
client = MongoClient(uri, server_api=ServerApi('1'))
db = client['test']
collection_name = db['sentiments']
# Save the document to the database
# example_document.save()
print("Document Saved")
# Start the POST request function in a background thread
# post_request_thread = threading.Thread(target=send_json_to_nodejs)
# post_request_thread.daemon = True # Ensure thread exits when Flask app stops
# post_request_thread.start()
# mongoose.connect("mongodb+srv://ashritramanala:X2f1pLPy48ZFal1s@vandyhackscluster.h7isr.mon godb.net/?retryWrites=true&w=majority&appName=VandyHacksCluster")
# Start the Flask app
app.run(debug=True)