spital commited on
Commit
7270d4a
1 Parent(s): 952b62a

response err handl

Browse files
Files changed (1) hide show
  1. app.py +7 -2
app.py CHANGED
@@ -2,7 +2,8 @@ import gradio as gr
2
  import requests
3
  import json
4
  import os
5
- API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-2.7B" # EleutherAI/gpt-neo-1.3B
 
6
  apikey=os.environ.get('api_key')
7
  headers = {"Authorization": f"Bearer {apikey}"}
8
  def query(input_sentence,num,start):
@@ -12,7 +13,11 @@ def query(input_sentence,num,start):
12
  full_input=intial+"original:"+input_sentence + "\n paraphrase:"+start
13
  data = json.dumps({"inputs":full_input,"parameters":{"max_length":len(full_input.split())+70,"min_length":len(full_input.split())+70},"temperature":0.650+0.05*i})
14
  response = requests.request("POST", API_URL, headers=headers, data=data)
15
- output=json.loads(response.content.decode("utf-8"))[0]['generated_text']
 
 
 
 
16
  paraphrase=output.split('paraphrase:',3)[-1]
17
  paraphrase_text=paraphrase.split('original:',1)[0]
18
  paraphrase_final.append( paraphrase_text.split('.',1)[0]+".")
 
2
  import requests
3
  import json
4
  import os
5
+ #API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-2.7B"
6
+ API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-1.3B"
7
  apikey=os.environ.get('api_key')
8
  headers = {"Authorization": f"Bearer {apikey}"}
9
  def query(input_sentence,num,start):
 
13
  full_input=intial+"original:"+input_sentence + "\n paraphrase:"+start
14
  data = json.dumps({"inputs":full_input,"parameters":{"max_length":len(full_input.split())+70,"min_length":len(full_input.split())+70},"temperature":0.650+0.05*i})
15
  response = requests.request("POST", API_URL, headers=headers, data=data)
16
+ # https://stackoverflow.com/questions/16511337/correct-way-to-try-except-using-python-requests-module
17
+ if response.status_code != 200:
18
+ output = f'{response.status_code} problem, response={response.content.decode("utf-8")}'
19
+ else:
20
+ output=json.loads(response.content.decode("utf-8"))[0]['generated_text']
21
  paraphrase=output.split('paraphrase:',3)[-1]
22
  paraphrase_text=paraphrase.split('original:',1)[0]
23
  paraphrase_final.append( paraphrase_text.split('.',1)[0]+".")