Pratyaksh / save_data.py
MrResilient's picture
Update save_data.py
fb61b26 verified
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 = "bert_based_qna_dataset"
DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
DATA_FILENAME = "qna_logs.csv"
DATA_FILE = os.path.join("qna_logs", DATA_FILENAME)
DATASET_REPO_ID = "pragnakalp/bert_based_qna_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="qna_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(language, paragraph, result_dic):
try:
print("welcome")
ip_address = ''
ip_address= getIP()
print(ip_address)
location = get_location(ip_address)
print(location)
add_csv = [language, paragraph, result_dic, 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_qna_gen'
myobj = {'lan':language,'para': paragraph,'result_dic':result_dic,'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)