pragnakalp
commited on
Commit
•
964aca7
1
Parent(s):
307bd8b
Rename send_email_user.py to save_data.py
Browse files- save_data.py +125 -0
- send_email_user.py +0 -87
save_data.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import shutil
|
4 |
+
import requests
|
5 |
+
import re as r
|
6 |
+
from urllib.request import urlopen
|
7 |
+
from datetime import datetime
|
8 |
+
from datasets import Image
|
9 |
+
from huggingface_hub import Repository, upload_file
|
10 |
+
|
11 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
12 |
+
DATASET_NAME = "OCR-img-to-text"
|
13 |
+
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
|
14 |
+
DATASET_REPO_ID = "pragnakalp/OCR-img-to-text"
|
15 |
+
print("is none?", HF_TOKEN is None)
|
16 |
+
REPOSITORY_DIR = "data"
|
17 |
+
LOCAL_DIR = 'data_local'
|
18 |
+
os.makedirs(LOCAL_DIR,exist_ok=True)
|
19 |
+
|
20 |
+
repo = Repository(
|
21 |
+
local_dir="ocr_data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
22 |
+
)
|
23 |
+
repo.git_pull()
|
24 |
+
|
25 |
+
def getIP():
|
26 |
+
ip_address = ''
|
27 |
+
try:
|
28 |
+
d = str(urlopen('http://checkip.dyndns.com/')
|
29 |
+
.read())
|
30 |
+
|
31 |
+
return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1)
|
32 |
+
except Exception as e:
|
33 |
+
print("Error while getting IP address -->",e)
|
34 |
+
return ip_address
|
35 |
+
|
36 |
+
def get_location(ip_addr):
|
37 |
+
location = {}
|
38 |
+
try:
|
39 |
+
ip=ip_addr
|
40 |
+
|
41 |
+
req_data={
|
42 |
+
"ip":ip,
|
43 |
+
"token":"pkml123"
|
44 |
+
}
|
45 |
+
url = "https://demos.pragnakalp.com/get-ip-location"
|
46 |
+
|
47 |
+
# req_data=json.dumps(req_data)
|
48 |
+
# print("req_data",req_data)
|
49 |
+
headers = {'Content-Type': 'application/json'}
|
50 |
+
|
51 |
+
response = requests.request("POST", url, headers=headers, data=json.dumps(req_data))
|
52 |
+
response = response.json()
|
53 |
+
print("response======>>",response)
|
54 |
+
return response
|
55 |
+
except Exception as e:
|
56 |
+
print("Error while getting location -->",e)
|
57 |
+
return location
|
58 |
+
|
59 |
+
"""
|
60 |
+
Save generated details
|
61 |
+
"""
|
62 |
+
def dump_json(thing,file):
|
63 |
+
with open(file,'w+',encoding="utf8") as f:
|
64 |
+
json.dump(thing,f)
|
65 |
+
|
66 |
+
def flag(resume_file_path,get_pdf_name,final_output):
|
67 |
+
|
68 |
+
print("saving data------------------------")
|
69 |
+
# try:
|
70 |
+
adversarial_number = 0
|
71 |
+
adversarial_number = 0 if None else adversarial_number
|
72 |
+
|
73 |
+
ip_address= getIP()
|
74 |
+
print("ip_address :",ip_address)
|
75 |
+
location = get_location(ip_address)
|
76 |
+
print("location :",location)
|
77 |
+
|
78 |
+
metadata_name = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
|
79 |
+
SAVE_FILE_DIR = os.path.join(LOCAL_DIR,metadata_name)
|
80 |
+
os.makedirs(SAVE_FILE_DIR,exist_ok=True)
|
81 |
+
image_output_filename = os.path.join(SAVE_FILE_DIR,'image.png')
|
82 |
+
|
83 |
+
try:
|
84 |
+
Image.fromarray(input_image).save(image_output_filename)
|
85 |
+
# input_image.save(image_output_filename)
|
86 |
+
except Exception:
|
87 |
+
raise Exception(f"Had issues saving np array image to file")
|
88 |
+
|
89 |
+
# Write metadata.json to file
|
90 |
+
json_file_path = os.path.join(SAVE_FILE_DIR,'metadata.jsonl')
|
91 |
+
metadata= {'id':metadata_name,'method':Method,'file_name':'image.png',
|
92 |
+
'generated_text':text_output,'ip':ip_address, 'location':location
|
93 |
+
}
|
94 |
+
|
95 |
+
dump_json(metadata,json_file_path)
|
96 |
+
|
97 |
+
# Simply upload the image file and metadata using the hub's upload_file
|
98 |
+
# Upload the image
|
99 |
+
repo_image_path = os.path.join(REPOSITORY_DIR,os.path.join(metadata_name,'image.png'))
|
100 |
+
|
101 |
+
_ = upload_file(path_or_fileobj = image_output_filename,
|
102 |
+
path_in_repo =repo_image_path,
|
103 |
+
repo_id=DATASET_REPO_ID,
|
104 |
+
repo_type='dataset',
|
105 |
+
token=HF_TOKEN
|
106 |
+
)
|
107 |
+
|
108 |
+
# Upload the metadata
|
109 |
+
repo_json_path = os.path.join(REPOSITORY_DIR,os.path.join(metadata_name,'metadata.jsonl'))
|
110 |
+
_ = upload_file(path_or_fileobj = json_file_path,
|
111 |
+
path_in_repo =repo_json_path,
|
112 |
+
repo_id= DATASET_REPO_ID,
|
113 |
+
repo_type='dataset',
|
114 |
+
token=HF_TOKEN
|
115 |
+
)
|
116 |
+
adversarial_number+=1
|
117 |
+
repo.git_pull()
|
118 |
+
|
119 |
+
url = 'http://pragnakalpdev35.pythonanywhere.com/HF_space_image_to_text'
|
120 |
+
myobj = {'Method': Method,'text_output':text_output,'img':input_image.tolist(),'ip_address':ip_address, 'loc':location}
|
121 |
+
x = requests.post(url, json = myobj)
|
122 |
+
print("mail status code",x.status_code)
|
123 |
+
|
124 |
+
return "*****Logs save successfully!!!!"
|
125 |
+
|
send_email_user.py
DELETED
@@ -1,87 +0,0 @@
|
|
1 |
-
# from email.mime.multipart import MIMEMultipart
|
2 |
-
# from email.mime.text import MIMEText
|
3 |
-
# from email.mime.image import MIMEImage
|
4 |
-
import smtplib
|
5 |
-
|
6 |
-
def send_user_email():
|
7 |
-
|
8 |
-
sender="pragnakalp.dev33@gmail.com"
|
9 |
-
password="httscgatatbbxxur"
|
10 |
-
reciever="pragnakalp.dev35@gmail.com"
|
11 |
-
|
12 |
-
s = smtplib.SMTP('smtp.gmail.com', 587)
|
13 |
-
s.starttls()
|
14 |
-
s.login(sender,password)
|
15 |
-
|
16 |
-
|
17 |
-
message = """Subject : Appointment Booking\n\n
|
18 |
-
Hello,
|
19 |
-
Your OCR generated successfully"""
|
20 |
-
s.sendmail(sender, reciever, message)
|
21 |
-
s.quit()
|
22 |
-
mailsend=1
|
23 |
-
print("Send mail successfully")
|
24 |
-
return "Hellowwass"
|
25 |
-
|
26 |
-
fromadd = "pragnakalp.dev33@gmail.com"
|
27 |
-
password = "httscgatatbbxxur"
|
28 |
-
# SENDGRID_API_KEY = "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"
|
29 |
-
|
30 |
-
# def gmail_login():
|
31 |
-
# try:
|
32 |
-
# server = smtplib.SMTP('smtp.gmail.com', 587)
|
33 |
-
# server.starttls()
|
34 |
-
# server.login(fromadd, password)
|
35 |
-
# return server
|
36 |
-
# except Exception as e:
|
37 |
-
# print("*** MAIL ERROR ***"*10)
|
38 |
-
# print(e)
|
39 |
-
|
40 |
-
# def send_user_email(hostname,text_output,Method):
|
41 |
-
|
42 |
-
# try:
|
43 |
-
# # attachment = input_img
|
44 |
-
# html_body=""
|
45 |
-
# html_body = f"""<html>
|
46 |
-
# <body>
|
47 |
-
# <p>
|
48 |
-
# {hostname}<br/>
|
49 |
-
# Method: {Method}<br/>
|
50 |
-
# Generated text: {text_output}<br/>
|
51 |
-
# </p>
|
52 |
-
# <br />
|
53 |
-
# </body>
|
54 |
-
# </html>"""
|
55 |
-
|
56 |
-
# toadd = "pragnakalp.dev35@gmail.com"
|
57 |
-
# subject = "OCR generated text"
|
58 |
-
|
59 |
-
# msg = MIMEMultipart("alternative")
|
60 |
-
|
61 |
-
# msg['From'] = fromadd
|
62 |
-
# msg['To'] = toadd
|
63 |
-
# msg['Subject'] = subject
|
64 |
-
|
65 |
-
# # part1 = MIMEText(text, "plain")
|
66 |
-
# # part2 = MIMEText(html_body, "html")
|
67 |
-
# # msg.attach(part1)
|
68 |
-
|
69 |
-
# # part2 = MIMEText('<b>%s</b><br/><img src="cid:%s"/><br/>' % (html_body, attachment), 'html')
|
70 |
-
# part2 = MIMEText(html_body, "html")
|
71 |
-
# msg.attach(part2)
|
72 |
-
# # with open(attachment, 'rb') as fp:
|
73 |
-
# # img = MIMEImage(fp.read())
|
74 |
-
# # img.add_header('Content-ID', '<{}>'.format(attachment))
|
75 |
-
# # msg.attach(img)
|
76 |
-
|
77 |
-
# server = gmail_login()
|
78 |
-
# print("login success")
|
79 |
-
# temp = server.sendmail(fromadd, toadd, msg.as_string())
|
80 |
-
# print("temp=.", temp)
|
81 |
-
# print("email sent to", toadd)
|
82 |
-
|
83 |
-
# print("** Mail Successfully Sent !***")
|
84 |
-
# return True
|
85 |
-
# except Exception as e:
|
86 |
-
# print("Error while sending feedback email => ", e)
|
87 |
-
# return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|