pragnakalp commited on
Commit
796f3a5
·
1 Parent(s): c9c2be1

Create save_data.py

Browse files
Files changed (1) hide show
  1. save_data.py +122 -0
save_data.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 huggingface_hub import Repository, upload_file
9
+
10
+ HF_TOKEN = os.environ.get("HF_TOKEN")
11
+ DATASET_NAME = "audio_emotion_recognition_dataset"
12
+ DATASET_REPO_URL = f"https://huggingface.co/datasets/pragnakalp/{DATASET_NAME}"
13
+ DATASET_REPO_ID = "pragnakalp/audio_emotion_recognition_dataset"
14
+ print("is none?", HF_TOKEN is None)
15
+ REPOSITORY_DIR = "audio_logs"
16
+ LOCAL_DIR = 'data_local'
17
+ os.makedirs(LOCAL_DIR,exist_ok=True)
18
+
19
+ repo = Repository(
20
+ local_dir="audio_data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
21
+ )
22
+ repo.git_pull()
23
+
24
+ def getIP():
25
+ ip_address = ''
26
+ try:
27
+ d = str(urlopen('http://checkip.dyndns.com/')
28
+ .read())
29
+
30
+ return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1)
31
+ except Exception as e:
32
+ print("Error while getting IP address -->",e)
33
+ return ip_address
34
+
35
+ def get_location(ip_addr):
36
+ location = {}
37
+ try:
38
+ ip=ip_addr
39
+
40
+ req_data={
41
+ "ip":ip,
42
+ "token":"pkml123"
43
+ }
44
+ url = "https://demos.pragnakalp.com/get-ip-location"
45
+
46
+ # req_data=json.dumps(req_data)
47
+ # print("req_data",req_data)
48
+ headers = {'Content-Type': 'application/json'}
49
+
50
+ response = requests.request("POST", url, headers=headers, data=json.dumps(req_data))
51
+ response = response.json()
52
+ print("response======>>",response)
53
+ return response
54
+ except Exception as e:
55
+ print("Error while getting location -->",e)
56
+ return location
57
+
58
+ """
59
+ Save generated details
60
+ """
61
+ def dump_json(thing,file):
62
+ with open(file,'w+',encoding="utf8") as f:
63
+ json.dump(thing,f)
64
+
65
+ def flag(audio_file_path,get_audio_name,final_output):
66
+
67
+ print("saving data------------------------")
68
+ # try:
69
+ adversarial_number = 0
70
+ adversarial_number = 0 if None else adversarial_number
71
+
72
+ metadata_name = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
73
+ SAVE_FILE_DIR = os.path.join(LOCAL_DIR,metadata_name)
74
+ os.makedirs(SAVE_FILE_DIR,exist_ok=True)
75
+ audio_save_path = os.path.join(SAVE_FILE_DIR,get_audio_name)
76
+ print("old audio_file_path :",audio_file_path)
77
+ print("new audio_path :",audio_save_path)
78
+ try:
79
+ shutil.copyfile(audio_file_path, audio_save_path)
80
+ except Exception:
81
+ raise Exception(f"Had issues saving PIL image to file")
82
+
83
+ # Write metadata.json to file
84
+ json_file_path = os.path.join(SAVE_FILE_DIR,'metadata.jsonl')
85
+ metadata= {'id':metadata_name,'file_name':get_audio_name,'output':final_output,'ip_address':ip_address, 'location':location}
86
+
87
+ dump_json(metadata,json_file_path)
88
+
89
+ # Simply upload the pdf file and metadata using the hub's upload_file
90
+ # Upload the pdf
91
+ repo_audio_path = os.path.join(REPOSITORY_DIR,os.path.join(metadata_name,get_audio_name))
92
+
93
+ _ = upload_file(path_or_fileobj = audio_save_path,
94
+ path_in_repo=repo_audio_path,
95
+ repo_id=DATASET_REPO_ID,
96
+ repo_type='dataset',
97
+ token=HF_TOKEN
98
+ )
99
+
100
+ # Upload the metadata
101
+ repo_json_path = os.path.join(REPOSITORY_DIR,os.path.join(metadata_name,'metadata.jsonl'))
102
+ _ = upload_file(path_or_fileobj = json_file_path,
103
+ path_in_repo =repo_json_path,
104
+ repo_id= DATASET_REPO_ID,
105
+ repo_type='dataset',
106
+ token=HF_TOKEN
107
+ )
108
+ adversarial_number+=1
109
+ repo.git_pull()
110
+
111
+ ip_address= getIP()
112
+ print(ip_address)
113
+ location = get_location(ip_address)
114
+ print(location)
115
+ url = 'http://pragnakalpdev33.pythonanywhere.com/HF_space_audio_emotion'
116
+ myobj = {'audio_path': repo_audio_path,'final_output':final_output,'ip_addr':ip_address, 'loc':location}
117
+
118
+ x = requests.post(url, json = myobj)
119
+ print("mail status code",x.status_code)
120
+ return "*****Logs save successfully!!!!"
121
+ # except Exception as e:
122
+ # return "Error whils saving logs -->"+ str(e)