Compare commits
11 Commits
master
...
presentati
Author | SHA1 | Date | |
---|---|---|---|
|
fbe41cfec9 | ||
|
c484359495 | ||
|
6fa42c9a0a | ||
|
83ff7d9144 | ||
|
96697e52aa | ||
|
09c3b3cccc | ||
|
a01fd48c64 | ||
|
d60f54ed35 | ||
|
a5f4159a49 | ||
|
45f4029509 | ||
|
aab31f21aa |
12
Dockerfile
12
Dockerfile
@ -1,12 +0,0 @@
|
|||||||
FROM python:3
|
|
||||||
ENV DOCKER_APP True
|
|
||||||
USER root
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY . .
|
|
||||||
#RUN chmod u+x setup_core.sh
|
|
||||||
#RUN bash setup_core.sh
|
|
||||||
RUN pip3 install -r requirements.txt
|
|
||||||
EXPOSE 5000/udp
|
|
||||||
EXPOSE 5000/tcp
|
|
||||||
CMD ["python3","-m", "ayct_backend.app"]
|
|
1
Procfile
Normal file
1
Procfile
Normal file
@ -0,0 +1 @@
|
|||||||
|
web: gunicorn -b 0.0.0.0:$PORT ayct_backend:app --timeout 120
|
@ -26,4 +26,6 @@ def create_app():
|
|||||||
def hello():
|
def hello():
|
||||||
return "Hello world!"
|
return "Hello world!"
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
app = create_app()
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
import os
|
|
||||||
from . import create_app
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app = create_app()
|
|
||||||
port = int(os.environ.get('PORT', 5000))
|
|
||||||
app.run(host = '0.0.0.0', port = port)
|
|
@ -3,6 +3,7 @@ import json
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from requests_oauthlib import OAuth1Session
|
from requests_oauthlib import OAuth1Session
|
||||||
from flask import Blueprint, current_app, request, jsonify
|
from flask import Blueprint, current_app, request, jsonify
|
||||||
|
from nltk import sent_tokenize
|
||||||
from ayct_backend.models import *
|
from ayct_backend.models import *
|
||||||
from ayct_backend.firebase import verify_token
|
from ayct_backend.firebase import verify_token
|
||||||
|
|
||||||
@ -44,47 +45,8 @@ def add_twitter_campaign():
|
|||||||
return "Content-type not supported!", 400
|
return "Content-type not supported!", 400
|
||||||
|
|
||||||
request_json = request.json
|
request_json = request.json
|
||||||
if 'campaign_name' not in request_json or 'user_input' not in request_json or 'twitter_account_id' not in request_json:
|
if 'campaign_name' not in request_json or 'twitter_account_id' not in request_json:
|
||||||
return "Invalid request!", 400
|
return "Invalid request!", 400
|
||||||
|
|
||||||
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
|
|
||||||
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
|
|
||||||
|
|
||||||
twitter_account = TwitterAccount.query.filter_by(twitter_account_id=request_json['twitter_account_id']).first()
|
|
||||||
|
|
||||||
# generate campaign content
|
|
||||||
core_url = 'http://65.108.80.28:4999/generate'
|
|
||||||
payload = {
|
|
||||||
"data": request_json['user_input'],
|
|
||||||
"length": 420
|
|
||||||
}
|
|
||||||
|
|
||||||
#response = requests.post(core_url, headers={"Content-Type":"application/json"}, data=json.dumps(payload))
|
|
||||||
#generated_content = response.content.decode("utf-8").replace('<|endoftext|>', '').replace(' ', ' ')
|
|
||||||
|
|
||||||
# trimmed_text = generated_content[:270]
|
|
||||||
# dot_index = trimmed_text.rfind(".")
|
|
||||||
# space_index = trimmed_text.rfind(" ")
|
|
||||||
# if dot_index > 0:
|
|
||||||
# tweet_text = trimmed_text[:dot_index + 1].capitalize()
|
|
||||||
# else:
|
|
||||||
# tweet_text = trimmed_text[:space_index + 1].capitalize()
|
|
||||||
|
|
||||||
random_part = str(uuid4().hex)
|
|
||||||
tweet_text = "This is placeholder content generated by our application. Normal generation doesn't work." + random_part
|
|
||||||
|
|
||||||
# create post on twitter
|
|
||||||
oauth = OAuth1Session(
|
|
||||||
consumer_key,
|
|
||||||
client_secret=consumer_secret,
|
|
||||||
resource_owner_key=twitter_account.access_token,
|
|
||||||
resource_owner_secret=twitter_account.access_token_secret,
|
|
||||||
)
|
|
||||||
|
|
||||||
response = oauth.post(
|
|
||||||
"https://api.twitter.com/2/tweets",
|
|
||||||
json={"text": tweet_text},
|
|
||||||
).json()['data']
|
|
||||||
|
|
||||||
# save campaign to database
|
# save campaign to database
|
||||||
twitter_campaign = TwitterCampaign(
|
twitter_campaign = TwitterCampaign(
|
||||||
@ -92,19 +54,9 @@ def add_twitter_campaign():
|
|||||||
user_id = user_id,
|
user_id = user_id,
|
||||||
campaign_name = request_json['campaign_name'],
|
campaign_name = request_json['campaign_name'],
|
||||||
twitter_account_id = request_json['twitter_account_id'],
|
twitter_account_id = request_json['twitter_account_id'],
|
||||||
user_input = request_json['user_input'],
|
|
||||||
)
|
|
||||||
|
|
||||||
twitter_campaign_post = TwitterPost(
|
|
||||||
id = str(uuid4()),
|
|
||||||
campaign_id = twitter_campaign.id,
|
|
||||||
user_id = user_id,
|
|
||||||
post_content = tweet_text,
|
|
||||||
twitter_post_id = response['id']
|
|
||||||
)
|
)
|
||||||
|
|
||||||
db.session.add(twitter_campaign)
|
db.session.add(twitter_campaign)
|
||||||
db.session.add(twitter_campaign_post)
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return "Campaign succesfully created.", 201
|
return "Campaign succesfully created.", 201
|
||||||
@ -126,10 +78,12 @@ def delete_twitter_campaign():
|
|||||||
return "Invalid request!", 400
|
return "Invalid request!", 400
|
||||||
|
|
||||||
twitter_campaign = TwitterCampaign.query.filter_by(user_id=user_id, id=request_json['campaign_id']).first()
|
twitter_campaign = TwitterCampaign.query.filter_by(user_id=user_id, id=request_json['campaign_id']).first()
|
||||||
|
campaign_posts = TwitterPost.query.filter_by(campaign_id=request_json['campaign_id'])
|
||||||
|
|
||||||
if not twitter_campaign:
|
if not twitter_campaign:
|
||||||
return "Capmaign not found!", 404
|
return "Capmaign not found!", 404
|
||||||
|
|
||||||
|
db.session.delete(campaign_posts)
|
||||||
db.session.delete(twitter_campaign)
|
db.session.delete(twitter_campaign)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
@ -162,3 +116,93 @@ def get_twitter_campaign_details(campaign_id):
|
|||||||
"twitter_account_id": twitter_campaign.twitter_account_id,
|
"twitter_account_id": twitter_campaign.twitter_account_id,
|
||||||
"posts": posts
|
"posts": posts
|
||||||
}), 200
|
}), 200
|
||||||
|
|
||||||
|
@campaign.route('/generate', methods=['POST'])
|
||||||
|
def generate_content():
|
||||||
|
decoded_token = verify_token(request.headers)
|
||||||
|
if not decoded_token:
|
||||||
|
return "Not authorised!", 401
|
||||||
|
|
||||||
|
content_type = request.headers.get('Content-Type')
|
||||||
|
if content_type != 'application/json':
|
||||||
|
return "Content-type not supported!", 400
|
||||||
|
|
||||||
|
request_json = request.json
|
||||||
|
if 'user_input' not in request_json:
|
||||||
|
return "Invalid request!", 400
|
||||||
|
|
||||||
|
core_url = 'http://s415366.projektstudencki.pl/generate'
|
||||||
|
payload = {
|
||||||
|
"data": request_json['user_input'],
|
||||||
|
"length": 420
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = requests.post(core_url, headers={"Content-Type":"application/json"}, data=json.dumps(payload))
|
||||||
|
except:
|
||||||
|
return 'Failed to generate content!', 400
|
||||||
|
|
||||||
|
generated_content = response.content.decode("utf-8").replace('<|endoftext|>', '').replace(' ', ' ')
|
||||||
|
content_sentences = sent_tokenize(generated_content)
|
||||||
|
trimmed_content = ''
|
||||||
|
|
||||||
|
for sentence in content_sentences:
|
||||||
|
if len(trimmed_content) + len(sentence) < 270:
|
||||||
|
trimmed_content += ' '
|
||||||
|
trimmed_content += sentence
|
||||||
|
|
||||||
|
if len(trimmed_content) < 1:
|
||||||
|
trimmed_content = generated_content[:270]
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
"content": trimmed_content
|
||||||
|
}), 200
|
||||||
|
|
||||||
|
@campaign.route('/post', methods=['POST'])
|
||||||
|
def create_post():
|
||||||
|
decoded_token = verify_token(request.headers)
|
||||||
|
if not decoded_token:
|
||||||
|
return "Not authorised!", 401
|
||||||
|
|
||||||
|
user_id = decoded_token['sub']
|
||||||
|
|
||||||
|
content_type = request.headers.get('Content-Type')
|
||||||
|
if content_type != 'application/json':
|
||||||
|
return "Content-type not supported!", 400
|
||||||
|
|
||||||
|
request_json = request.json
|
||||||
|
if 'user_input' not in request_json or 'content' not in request_json or 'campaign_id' not in request_json:
|
||||||
|
return "Invalid request!", 400
|
||||||
|
|
||||||
|
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
|
||||||
|
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
|
||||||
|
|
||||||
|
twitter_campaign = TwitterCampaign.query.filter_by(id=request_json['campaign_id']).first()
|
||||||
|
twitter_account = TwitterAccount.query.filter_by(id=twitter_campaign.twitter_account_id).first()
|
||||||
|
|
||||||
|
# create post on twitter
|
||||||
|
oauth = OAuth1Session(
|
||||||
|
consumer_key,
|
||||||
|
client_secret=consumer_secret,
|
||||||
|
resource_owner_key=twitter_account.access_token,
|
||||||
|
resource_owner_secret=twitter_account.access_token_secret,
|
||||||
|
)
|
||||||
|
|
||||||
|
response = oauth.post(
|
||||||
|
"https://api.twitter.com/2/tweets",
|
||||||
|
json={"text": request_json['content']},
|
||||||
|
).json()['data']
|
||||||
|
|
||||||
|
twitter_campaign_post = TwitterPost(
|
||||||
|
id = str(uuid4()),
|
||||||
|
campaign_id = twitter_campaign.id,
|
||||||
|
user_id = user_id,
|
||||||
|
user_input = request_json['user_input'],
|
||||||
|
post_content = request_json['content'],
|
||||||
|
twitter_post_id = response['id']
|
||||||
|
)
|
||||||
|
|
||||||
|
db.session.add(twitter_campaign_post)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return "Campaign post succesfully created.", 201
|
||||||
|
@ -19,7 +19,6 @@ class TwitterCampaign(db.Model):
|
|||||||
user_id = db.Column(db.String(64), nullable=False)
|
user_id = db.Column(db.String(64), nullable=False)
|
||||||
campaign_name = db.Column(db.String(64), nullable=False)
|
campaign_name = db.Column(db.String(64), nullable=False)
|
||||||
twitter_account_id = db.Column(db.String(32), nullable=False)
|
twitter_account_id = db.Column(db.String(32), nullable=False)
|
||||||
user_input = db.Column(db.String(100), nullable=False)
|
|
||||||
posts = db.relationship('TwitterPost', backref='campaign', lazy=True)
|
posts = db.relationship('TwitterPost', backref='campaign', lazy=True)
|
||||||
|
|
||||||
class TwitterPost(db.Model):
|
class TwitterPost(db.Model):
|
||||||
@ -28,5 +27,6 @@ class TwitterPost(db.Model):
|
|||||||
id = db.Column(db.String(36), primary_key=True)
|
id = db.Column(db.String(36), primary_key=True)
|
||||||
campaign_id = db.Column(db.String(36), db.ForeignKey(TwitterCampaign.id), nullable=False)
|
campaign_id = db.Column(db.String(36), db.ForeignKey(TwitterCampaign.id), nullable=False)
|
||||||
user_id = db.Column(db.String(64), nullable=False)
|
user_id = db.Column(db.String(64), nullable=False)
|
||||||
|
user_input = db.Column(db.String(100), nullable=False)
|
||||||
post_content = db.Column(db.String(300), nullable=False)
|
post_content = db.Column(db.String(300), nullable=False)
|
||||||
twitter_post_id = db.Column(db.String(32), unique=True, nullable=False)
|
twitter_post_id = db.Column(db.String(32), unique=True, nullable=False)
|
@ -1,3 +0,0 @@
|
|||||||
build:
|
|
||||||
docker:
|
|
||||||
web: Dockerfile
|
|
@ -5,3 +5,5 @@ requests_oauthlib==1.3.0
|
|||||||
psycopg2==2.9.3
|
psycopg2==2.9.3
|
||||||
pytest==6.2.5
|
pytest==6.2.5
|
||||||
google-api-python-client==2.36.0
|
google-api-python-client==2.36.0
|
||||||
|
nltk==3.7
|
||||||
|
gunicorn==20.1.0
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
git clone https://git.wmi.amu.edu.pl/s415366/pbr-ayct-core
|
|
||||||
cd pbr-ayct-core
|
|
||||||
pip3 install setuptools
|
|
||||||
python3 -m pip install --upgrade build
|
|
||||||
python3 -m build
|
|
||||||
pip3 install dist/pbrAyctCore-0.0.1.tar.gz
|
|
Loading…
Reference in New Issue
Block a user