Spaces:
Runtime error
Runtime error
Commit
·
4a5886f
1
Parent(s):
3d526b8
Upload save_data.py
Browse files- save_data.py +82 -0
save_data.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import csv
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
import re as r
|
| 6 |
+
from urllib.request import urlopen
|
| 7 |
+
from huggingface_hub import Repository
|
| 8 |
+
|
| 9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 10 |
+
DATASET_NAME = "text_summarization_dataset"
|
| 11 |
+
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
|
| 12 |
+
DATA_FILENAME = "txt_sum_logs.csv"
|
| 13 |
+
DATA_FILE = os.path.join("txt_sum_logs", DATA_FILENAME)
|
| 14 |
+
DATASET_REPO_ID = "pragnakalp/text_summarization_dataset"
|
| 15 |
+
print("is none?", HF_TOKEN is None)
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
hf_hub_download(
|
| 19 |
+
repo_id=DATASET_REPO_ID,
|
| 20 |
+
filename=DATA_FILENAME,
|
| 21 |
+
cache_dir=DATA_DIRNAME,
|
| 22 |
+
force_filename=DATA_FILENAME
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
except:
|
| 26 |
+
print("file not found")
|
| 27 |
+
|
| 28 |
+
repo = Repository(
|
| 29 |
+
local_dir="txt_sum_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
def getIP():
|
| 33 |
+
d = str(urlopen('http://checkip.dyndns.com/')
|
| 34 |
+
.read())
|
| 35 |
+
|
| 36 |
+
return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1)
|
| 37 |
+
|
| 38 |
+
def get_location(ip_addr):
|
| 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 |
+
|
| 56 |
+
def save_data_and_sendmail(paragraph, sum_type, result_dic):
|
| 57 |
+
try:
|
| 58 |
+
print("welcome")
|
| 59 |
+
ip_address = ''
|
| 60 |
+
|
| 61 |
+
ip_address= getIP()
|
| 62 |
+
print(ip_address)
|
| 63 |
+
location = get_location(ip_address)
|
| 64 |
+
print(location)
|
| 65 |
+
add_csv = [paragraph, result_dic,sum_type, ip_address, location]
|
| 66 |
+
with open(DATA_FILE, "a") as f:
|
| 67 |
+
writer = csv.writer(f)
|
| 68 |
+
# write the data
|
| 69 |
+
writer.writerow(add_csv)
|
| 70 |
+
commit_url = repo.push_to_hub()
|
| 71 |
+
print("commit data :",commit_url)
|
| 72 |
+
|
| 73 |
+
url = 'https://pragnakalpdev33.pythonanywhere.com/HF_space_txt_sum'
|
| 74 |
+
myobj = {'para': paragraph,'result':result_dic,'sum_type':sum_type,'ip_addr':ip_address,"location":location}
|
| 75 |
+
x = requests.post(url, json = myobj)
|
| 76 |
+
print("Email status: ",x.status_code)
|
| 77 |
+
|
| 78 |
+
return "Successfully save data"
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print("error")
|
| 82 |
+
return "Error while sending mail" + str(e)
|