devendergarg14 commited on
Commit
73e87d2
1 Parent(s): fb8e95f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -7,15 +7,32 @@ API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-2.7B"
7
  apikey = os.environ.get('api_key')
8
  headers = {"Authorization": f"Bearer {apikey}"}
9
 
10
- def query(input_sentence, num, start):
 
 
 
 
 
 
 
 
11
  paraphrase_final = []
12
  for i in range(num):
13
  intial = """These are the few examples of converting original sentences into paraphrased sentences.\n original: The gray clouds were a warning of an approaching storm.\n paraphrase: The coming storm was foretold by the dark clouds.\n original: Giraffes like Acacia leaves and hay, and they can consume 75 pounds of food a day.\n paraphrase: A giraffe can eat up to 75 pounds of Acacia leaves and hay daily.\n """
14
  full_input = intial + "original:" + input_sentence + "\n paraphrase:" + start
15
- 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})
16
- response = requests.request("POST", API_URL, headers=headers, data=data)
17
- output = json.loads(response.content.decode("utf-8"))[0]['generated_text']
18
- paraphrase = output.split('paraphrase:', 3)[-1]
 
 
 
 
 
 
 
 
 
19
  paraphrase_text = paraphrase.split('original:', 1)[0]
20
  paraphrase_final.append(paraphrase_text.split('.', 1)[0] + ".")
21
  return '\n\n'.join(paraphrase_final)
@@ -26,7 +43,7 @@ article = "<div style='text-align: center;'><a href='https://github.com/Eleuther
26
  examples = [['The sky, at sunset, looked like a carnivorous flower.', 4, 'The coloured reddish'], ['Inside us there is something that has no name, that something is what we are.', 4, '']]
27
 
28
  gr.Interface(
29
- fn=query,
30
  inputs=[
31
  gr.Textbox(lines=4, label="Input Text (Single Sentence)"),
32
  gr.Slider(minimum=1, maximum=10, step=1, value=4, label="Numbers of Outputs"),
 
7
  apikey = os.environ.get('api_key')
8
  headers = {"Authorization": f"Bearer {apikey}"}
9
 
10
+ def query(payload):
11
+ try:
12
+ response = requests.post(API_URL, headers=headers, json=payload)
13
+ response.raise_for_status() # Raise an error for bad status codes
14
+ return response.json()
15
+ except requests.exceptions.RequestException as e:
16
+ return {"error": str(e)}
17
+
18
+ def paraphrase(input_sentence, num, start):
19
  paraphrase_final = []
20
  for i in range(num):
21
  intial = """These are the few examples of converting original sentences into paraphrased sentences.\n original: The gray clouds were a warning of an approaching storm.\n paraphrase: The coming storm was foretold by the dark clouds.\n original: Giraffes like Acacia leaves and hay, and they can consume 75 pounds of food a day.\n paraphrase: A giraffe can eat up to 75 pounds of Acacia leaves and hay daily.\n """
22
  full_input = intial + "original:" + input_sentence + "\n paraphrase:" + start
23
+ payload = {
24
+ "inputs": full_input,
25
+ "parameters": {
26
+ "max_length": len(full_input.split()) + 70,
27
+ "min_length": len(full_input.split()) + 70,
28
+ "temperature": 0.650 + 0.05 * i
29
+ }
30
+ }
31
+ output = query(payload)
32
+ if 'error' in output:
33
+ return output['error']
34
+ generated_text = output[0]['generated_text']
35
+ paraphrase = generated_text.split('paraphrase:', 3)[-1]
36
  paraphrase_text = paraphrase.split('original:', 1)[0]
37
  paraphrase_final.append(paraphrase_text.split('.', 1)[0] + ".")
38
  return '\n\n'.join(paraphrase_final)
 
43
  examples = [['The sky, at sunset, looked like a carnivorous flower.', 4, 'The coloured reddish'], ['Inside us there is something that has no name, that something is what we are.', 4, '']]
44
 
45
  gr.Interface(
46
+ fn=paraphrase,
47
  inputs=[
48
  gr.Textbox(lines=4, label="Input Text (Single Sentence)"),
49
  gr.Slider(minimum=1, maximum=10, step=1, value=4, label="Numbers of Outputs"),