pls work
This commit is contained in:
commit
1432bab5e1
9
Dockerfile
Normal file
9
Dockerfile
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
FROM python:3.8
|
||||||
|
|
||||||
|
COPY api.py requirements.txt ./
|
||||||
|
RUN apt-get update
|
||||||
|
RUN apt-get install -y default-jdk
|
||||||
|
RUN pip install -r requirements.txt
|
||||||
|
|
||||||
|
EXPOSE 80:8000/tcp
|
||||||
|
CMD python api.py
|
39
api.py
Normal file
39
api.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from fastapi import FastAPI, UploadFile
|
||||||
|
from tika import parser
|
||||||
|
import uvicorn, re
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
|
||||||
|
def parse(pdf):
|
||||||
|
content = parser.from_buffer(pdf)
|
||||||
|
content = content['content'].split('\n')
|
||||||
|
content = [c for c in content if c != '']
|
||||||
|
total = float(re.findall(r'\d+[.]\d+', list(filter(lambda x: 'Razem' in x, content))[0])[-1])
|
||||||
|
content = content[content.index('Sprzedawca:') : content.index('Nabywca:')]
|
||||||
|
seller = content[1]
|
||||||
|
vat_id = content[-1].replace('NIP: ', '')
|
||||||
|
return {
|
||||||
|
'vat_id' : vat_id,
|
||||||
|
'seller' : seller,
|
||||||
|
'total' : total
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
origins = ["*"]
|
||||||
|
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=origins,
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.post('/invoice')
|
||||||
|
async def root(file: UploadFile):
|
||||||
|
return parse(file.file.read())
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
5
credentials_template.py
Normal file
5
credentials_template.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
aws_access_key_id=''
|
||||||
|
aws_secret_access_key=''
|
||||||
|
aws_session_token=''
|
||||||
|
|
||||||
|
default_vpc = ''
|
49
main.py
Normal file
49
main.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import boto3
|
||||||
|
from user_data import user_data
|
||||||
|
from credentials import (
|
||||||
|
aws_access_key_id,
|
||||||
|
aws_secret_access_key,
|
||||||
|
aws_session_token,
|
||||||
|
default_vpc
|
||||||
|
)
|
||||||
|
|
||||||
|
PREFIX = 'sassy'
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
ec2 = boto3.resource(
|
||||||
|
'ec2',
|
||||||
|
region_name='us-east-1',
|
||||||
|
aws_access_key_id=aws_access_key_id,
|
||||||
|
aws_secret_access_key=aws_secret_access_key,
|
||||||
|
aws_session_token=aws_session_token,
|
||||||
|
)
|
||||||
|
|
||||||
|
key_pair = ec2.create_key_pair(
|
||||||
|
KeyName=PREFIX + '-key',
|
||||||
|
KeyType='ed25519',
|
||||||
|
KeyFormat='pem',
|
||||||
|
)
|
||||||
|
|
||||||
|
security_group = ec2.create_security_group(
|
||||||
|
Description=PREFIX + '-group',
|
||||||
|
GroupName=PREFIX + '-group',
|
||||||
|
VpcId=default_vpc,
|
||||||
|
)
|
||||||
|
|
||||||
|
inbound_rules = security_group.authorize_ingress(
|
||||||
|
GroupId=security_group.group_id,
|
||||||
|
CidrIp='0.0.0.0/0',
|
||||||
|
IpProtocol='tcp',
|
||||||
|
FromPort=80,
|
||||||
|
ToPort=80,
|
||||||
|
)
|
||||||
|
|
||||||
|
instances = ec2.create_instances(
|
||||||
|
ImageId='ami-0b5eea76982371e91',
|
||||||
|
MinCount=1,
|
||||||
|
MaxCount=1,
|
||||||
|
InstanceType='t2.micro',
|
||||||
|
KeyName=key_pair.name,
|
||||||
|
UserData=user_data,
|
||||||
|
SecurityGroups=[security_group.group_name],
|
||||||
|
)
|
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
fastapi
|
||||||
|
tika
|
||||||
|
uvicorn
|
||||||
|
python-multipart
|
12
user_data.py
Normal file
12
user_data.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
user_data = '''
|
||||||
|
#!/bin/bash
|
||||||
|
sudo yum update -y
|
||||||
|
sudo yum install git -y
|
||||||
|
git clone https://git.wmi.amu.edu.pl/s444391/saas-chmurki.git
|
||||||
|
cd saas-chmurki
|
||||||
|
sudo yum install docker -y
|
||||||
|
sudo service docker start
|
||||||
|
sudo usermod -a -G docker ec2-user
|
||||||
|
sudo docker build -t invoice .
|
||||||
|
sudo docker run -d -p 80:8000 -t invoice
|
||||||
|
'''
|
22
web/index.html
Normal file
22
web/index.html
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script>
|
||||||
|
async function parse(){
|
||||||
|
document.getElementById("result").innerHTML = "Submitting..."
|
||||||
|
var data = new FormData()
|
||||||
|
data.append('file', document.getElementById("docpicker").files[0])
|
||||||
|
document.getElementById("result").innerHTML = "Fetching..."
|
||||||
|
resp = await fetch('http://54.205.36.47:80/invoice', {method: "POST", body: data})
|
||||||
|
document.getElementById("result").innerHTML = "Fetched..."
|
||||||
|
data = await resp.json()
|
||||||
|
var result = JSON.stringify(data)
|
||||||
|
document.getElementById("result").innerHTML = result
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<title>Makaron</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<input type="file" id="docpicker" accept=".pdf" onchange="parse()" />
|
||||||
|
<p id="result"></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
13
web/website-bucket-policy.json
Normal file
13
web/website-bucket-policy.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"Id": "MyPolicy",
|
||||||
|
"Version": "2023-01-22",
|
||||||
|
"Statement": [
|
||||||
|
{
|
||||||
|
"Sid": "PublicReadForGetBucketObjects",
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Principal": "*",
|
||||||
|
"Action": "s3:GetObject",
|
||||||
|
"Resource": "arn:aws:s3:::443930-bucket/*"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user