Spaces:
Runtime error
Runtime error
File size: 3,008 Bytes
4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 d5cb50f 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 e1ee643 4e003b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import gradio as gr
import os
import openai
import newspaper
import json
import re
from transformers import GPT2Tokenizer
# define the text summarizer function
def text_prompt(request, page_url, contraseña, temp):
try:
page = newspaper.Article(url=page_url)
page.download()
page.parse()
except Exception as e:
return "", f"--- An error occurred while processing the URL: {e} ---", ""
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
tokens = tokenizer.tokenize(page.text)
num_tokens = len(tokens)
if num_tokens > 10 and num_tokens < 2000:
openai.api_key = contraseña
# get the response from openai API
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=request + "\n\n" + page.text,
max_tokens=2048,
temperature=temp,
top_p=0.9,
)
# get the response text
response_text = response.choices[0].text
# clean the response text
response_text = re.sub(r'\s+', ' ', response_text)
return page.text, response_text, num_tokens
except Exception as e:
return page.text, f"--- An error occurred while processing the request: {e} ---", num_tokens
return page.text, "--- Max number of tokens ---", num_tokens
# define the gradio interface
iface = gr.Interface(
fn=text_prompt,
inputs=[gr.Textbox(lines=1, placeholder="Enter your prompt here...", label="Prompt:", type="text"),
gr.Textbox(lines=1, placeholder="Enter the URL here...", label="URL to parse:", type="text"),
gr.Textbox(lines=1, placeholder="Enter your API-key here...", label="API-Key:", type="password"),
gr.Slider(0.0,1.0, value=0.3, label="Temperature:")
],
outputs=[gr.Textbox(label="Input:"), gr.Textbox(label="Output:"), gr.Textbox(label="Tokens:")],
examples=[["Summarize the following text in Chinese, and give 5 keywords:","https://openai.com/blog/planning-for-agi-and-beyond","",0.5],
["Generate a summary of the following text. Give me an overview of main business impact from the text following this template:\n- Summary:\n- Business Impact:\n- Companies:", "https://ai.googleblog.com/2019/10/quantum-supremacy-using-programmable.html","",0.7]
],
title="Information Extraction Interface:",
# description="This tool allows querying the text retrieved from the URL using OpenAI's [text-davinci-003] engine.\nThe URL text can be referenced in the prompt as \"following text\".\nA GPT2 tokenizer is included to ensure that the 2000 token limit for OpenAI queries is not exceeded. Provide a prompt with your request, the url for text retrieval, your api-key and temperature to process the text."
)
error_message = ""
try:
iface.launch()
except Exception as e:
error_message = "An error occurred: " + str(e)
iface.outputs[1].value = error_message |