pragnakalp commited on
Commit
1bff3b0
·
1 Parent(s): 03d5d72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -27
app.py CHANGED
@@ -38,36 +38,85 @@ repo = Repository(
38
  local_dir="que_gen_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
39
  )
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def generate_questions(article,num_que):
42
  result = ''
43
- print("num_que :", num_que)
44
- if num_que == None or num_que == '':
45
- num_que = 5
46
- else:
47
- num_que = num_que
48
- generated_questions_list = qg.generate(article, num_questions=int(num_que))
49
- summarized_data = {
50
- "generated_questions" : generated_questions_list
51
- }
52
- generated_questions = summarized_data.get("generated_questions",'')
53
- add_csv = [article, generated_questions, num_que]
54
- with open(DATA_FILE, "a") as f:
55
- writer = csv.writer(f)
56
- # write the data
57
- writer.writerow(add_csv)
58
- commit_url = repo.push_to_hub()
59
- print(commit_url)
60
- # with open(DATA_FILE, "r") as file:
61
- # data = json.load(file)
62
- # data.append(entry)
63
- # with open(DATA_FILE, "w") as file:
64
- # json.dump(data, file)
65
- # commit_url = repo.push_to_hub()
66
- for q in generated_questions:
67
- print(q)
68
- result = result + q + '\n'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- return result
71
 
72
  ## design 1
73
  inputs=gr.Textbox(lines=5, label="Article/Text",elem_id="inp_div")
 
38
  local_dir="que_gen_logs", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
39
  )
40
 
41
+ def get_device_ip_address():
42
+ result = {}
43
+ if os.name == "nt":
44
+ result = "Running on Windows"
45
+ hostname = socket.gethostname()
46
+ ip_address = socket.gethostbyname(hostname)
47
+ result['ip_addr'] = ip_address
48
+ result['host'] = hostname
49
+ return result
50
+ elif os.name == "posix":
51
+ gw = os.popen("ip -4 route show default").read().split()
52
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
53
+ s.connect((gw[2], 0))
54
+ ipaddr = s.getsockname()[0]
55
+ gateway = gw[2]
56
+ host = socket.gethostname()
57
+ result['ip_addr'] = ipaddr
58
+ result['host'] = host
59
+ return result
60
+ else:
61
+ result = os.name + " not supported yet."
62
+ return result
63
+
64
  def generate_questions(article,num_que):
65
  result = ''
66
+ try:
67
+ if num_que == None or num_que == '':
68
+ num_que = 5
69
+ else:
70
+ num_que = num_que
71
+ generated_questions_list = qg.generate(article, num_questions=int(num_que))
72
+ summarized_data = {
73
+ "generated_questions" : generated_questions_list
74
+ }
75
+ generated_questions = summarized_data.get("generated_questions",'')
76
+
77
+ for q in generated_questions:
78
+ print(q)
79
+ result = result + q + '\n'
80
+ save_data_and_sendmail(article,generated_questions,num_que,result)
81
+ return result
82
+ except Exception as e:
83
+ return "Error while generating question -->" + e
84
+ """
85
+ Save generated details
86
+ """
87
+ def save_data_and_sendmail(article,generated_questions,num_que,result):
88
+ try:
89
+ article = ''
90
+ generated_questions = []
91
+ num_que = ''
92
+ result = ''
93
+
94
+ add_csv = [article, generated_questions, num_que]
95
+ with open(DATA_FILE, "a") as f:
96
+ writer = csv.writer(f)
97
+ # write the data
98
+ writer.writerow(add_csv)
99
+ commit_url = repo.push_to_hub()
100
+ print("commit data :",commit_url)
101
+ except Exception as e:
102
+ return "Error while storing data -->" + e
103
+
104
+ try:
105
+ hostname = get_device_ip_address()
106
+ url = 'http://pragnakalpdev35.pythonanywhere.com/HF_space_que_gen'
107
+ myobj = {'article': article,'gen_que':result,'ip_addr':hostname.get("ip_addr",""),'host':hostname.get("host","")}
108
+ x = requests.post(url, json = myobj)
109
+ print(x)
110
+ # with open(DATA_FILE, "r") as file:
111
+ # data = json.load(file)
112
+ # data.append(entry)
113
+ # with open(DATA_FILE, "w") as file:
114
+ # json.dump(data, file)
115
+ # commit_url = repo.push_to_hub()
116
+ except Exception as e:
117
+ return "Error while sending mail"
118
 
119
+ return "Successfully save data"
120
 
121
  ## design 1
122
  inputs=gr.Textbox(lines=5, label="Article/Text",elem_id="inp_div")