File size: 2,388 Bytes
b3dd6eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c39e9b8
b3dd6eb
c39e9b8
 
b3dd6eb
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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)