devendergarg14's picture
Update app.py
1af19c6 verified
raw
history blame contribute delete
No virus
2.95 kB
import gradio as gr
import requests
import json
import os
API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-2.7B"
apikey = os.environ.get('api_key')
headers = {"Authorization": f"Bearer {apikey}"}
def query(payload):
try:
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status() # Raise an error for bad status codes
return response.json()
except requests.exceptions.RequestException as e:
return {"error": str(e)}
def paraphrase(input_sentence, num, start):
paraphrase_final = []
for i in range(num):
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 """
full_input = intial + "original:" + input_sentence + "\n paraphrase:" + start
payload = {
"inputs": full_input,
"parameters": {
"max_length": len(full_input.split()) + 70,
"min_length": len(full_input.split()) + 70,
"temperature": 0.650 + 0.05 * i
}
}
output = query(payload)
if 'error' in output:
return output['error']
generated_text = output[0]['generated_text']
paraphrase = generated_text.split('paraphrase:', 3)[-1]
paraphrase_text = paraphrase.split('original:', 1)[0]
paraphrase_final.append(paraphrase_text.split('.', 1)[0] + ".")
return '\n\n'.join(paraphrase_final)
title = "Paraphrasing with GPT"
description = "Gradio Demo for Paraphrasing with GPT-NEO. Simply add one line sentence in the Input. It is possible to control the start of output paraphrased sentences using optional Starting Point Input. If outputs are not satisfactory try to increase number of outputs"
article = "<div style='text-align: center;'><a href='https://github.com/EleutherAI/gpt-neo'>GPT-NEO GitHub</a> | <center><img src='https://visitor-badge.glitch.me/badge?page_id=devendergarg14_Paraphrasing_with_GPT_Neo' alt='visitor badge'></center></div>"
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, '']]
gr.Interface(
fn=paraphrase,
inputs=[
gr.Textbox(lines=4, label="Input Text (Single Sentence)"),
gr.Slider(minimum=1, maximum=10, step=1, value=4, label="Numbers of Outputs"),
gr.Textbox(lines=1, label="Starting Point (optional)")
],
outputs="text",
title=title,
description=description,
article=article,
examples=examples,
allow_flagging='never'
).launch()