Text_Summarization / save_data.py
pragnakalp's picture
Update save_data.py
326bab4
raw
history blame
2.41 kB
import os
import csv
import json
import requests
import re as r
from urllib.request import urlopen
from huggingface_hub import Repository
HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_NAME = "text_summarization_dataset"
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
DATA_FILENAME = "txt_sum_logs.csv"
DATA_FILE = os.path.join("txt_sum_logs", DATA_FILENAME)
DATASET_REPO_ID = "pragnakalp/text_summarization_dataset"
print("is none?", HF_TOKEN is None)
try:
hf_hub_download(
repo_id=DATASET_REPO_ID,
filename=DATA_FILENAME,
cache_dir=DATA_DIRNAME,
force_filename=DATA_FILENAME
)
except:
print("file not found")
repo = Repository(
local_dir="txt_sum_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)
def getIP():
d = str(urlopen('http://checkip.dyndns.com/')
.read())
return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1)
def get_location(ip_addr):
ip=ip_addr
req_data={
"ip":ip,
"token":"pkml123"
}
url = "https://demos.pragnakalp.com/get-ip-location"
# req_data=json.dumps(req_data)
# print("req_data",req_data)
headers = {'Content-Type': 'application/json'}
response = requests.request("POST", url, headers=headers, data=json.dumps(req_data))
response = response.json()
print("response======>>",response)
return response
def save_data_and_sendmail(paragraph, sum_type, result_dic):
try:
print("welcome")
ip_address = ''
ip_address= getIP()
print(ip_address)
location = get_location(ip_address)
print(location)
add_csv = [paragraph, result_dic,sum_type, ip_address, location]
with open(DATA_FILE, "a") as f:
writer = csv.writer(f)
# write the data
writer.writerow(add_csv)
commit_url = repo.push_to_hub()
print("commit data :",commit_url)
url = 'https://pragnakalpdev33.pythonanywhere.com/HF_space_txt_sum'
myobj = {'para': paragraph,'result':result_dic,'sum_type':sum_type,'ip_addr':ip_address,"location":location}
x = requests.post(url, json = myobj)
print("Email status: ",x.status_code)
return "Successfully save data"
except Exception as e:
print("error")
return "Error while sending mail" + str(e)