-
Notifications
You must be signed in to change notification settings - Fork 0
/
main with HolC.py
94 lines (70 loc) · 2.37 KB
/
main with HolC.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
import requests
import pandas as pd
import subprocess
import time
from fastapi import FastAPI
from fastapi.responses import JSONResponse
import json
import tensorflow as tf
# fastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Welcome to": "Some Good News"}
# generate token for iam
@app.get("/gen_token")
def generate_token():
process = subprocess.Popen(["python", "refresh_generated_token.py"])
tokens = pd.read_csv('generatedTokens.csv')
time.sleep(10)
process.terminate()
process.wait()
return {"Token":tokens.iloc[-1]}
# fetch articles
@app.get("/good_news/")
def all_articles():
api = 'b456a0b678604587bd37dfe28a0bf520'
base_url = 'https://newsapi.org/v2/everything'
params = {
'source': 'bbc-news',
'q': 'world',
'sortBy': 'publishedAt',
'apiKey': api
}
gathered_articles = requests.get(base_url, params=params)
data = gathered_articles.json()
articles = data['articles']
# token access to CSAT
generatedTokens = pd.read_csv("generatedTokens.csv")
ca_accessToken = generatedTokens["access_token"].iloc[-1]
# fetch sentiment analysis results
responses = []
for article in articles[:5]:
article_response = {}
data = {'text': article['description'],'accessToken': ca_accessToken}
response = requests.post("https://csat.alamedaproject.eu/classes", json=data)
article_response['title'] = article['title']
article_response['content'] = article['content']
article_response['date'] = article['publishedAt']
article_response['url'] = article['url']
article_response['response'] = response.json()
responses.append(article_response)
response.close()
# dataframe creation
title = []
date = []
url = []
for response in responses:
# can adjust confidence level
if response['response']['sentiment_classes'][0]['sentiment_score'] > 0: # positive: 0, neutral: 1, negative: 2
title.append(response['title'])
date.append(response['date'])
url.append(response['url'])
df = pd.DataFrame([title,date,url]).T
df.columns = ['title','date','url']
df_json = json.loads(df.to_json(orient='records'))
return df_json
# server
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)