Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +68 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import openai
|
4 |
+
import newspaper
|
5 |
+
import json
|
6 |
+
import re
|
7 |
+
from transformers import GPT2Tokenizer
|
8 |
+
|
9 |
+
|
10 |
+
# define the text summarizer function
|
11 |
+
def text_prompt(request, page_url, contraseña, temp):
|
12 |
+
try:
|
13 |
+
page = newspaper.Article(url=page_url)
|
14 |
+
page.download()
|
15 |
+
page.parse()
|
16 |
+
except Exception as e:
|
17 |
+
return "", f"--- An error occurred while processing the URL: {e} ---", ""
|
18 |
+
|
19 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
20 |
+
|
21 |
+
tokens = tokenizer.tokenize(page.text)
|
22 |
+
num_tokens = len(tokens)
|
23 |
+
|
24 |
+
if num_tokens > 10 and num_tokens < 2000:
|
25 |
+
openai.api_key = contraseña
|
26 |
+
# get the response from openai API
|
27 |
+
try:
|
28 |
+
response = openai.Completion.create(
|
29 |
+
engine="text-davinci-003",
|
30 |
+
prompt=request + "\n\n" + page.text,
|
31 |
+
max_tokens=2048,
|
32 |
+
temperature=temp,
|
33 |
+
top_p=0.9,
|
34 |
+
)
|
35 |
+
# get the response text
|
36 |
+
response_text = response.choices[0].text
|
37 |
+
# clean the response text
|
38 |
+
response_text = re.sub(r'\s+', ' ', response_text)
|
39 |
+
return page.text, response_text, num_tokens
|
40 |
+
except Exception as e:
|
41 |
+
return page.text, f"--- An error occurred while processing the request: {e} ---", num_tokens
|
42 |
+
return page.text, "--- Max number of tokens ---", num_tokens
|
43 |
+
|
44 |
+
# define the gradio interface
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=text_prompt,
|
47 |
+
inputs=[gr.Textbox(lines=1, placeholder="Enter your prompt here...", label="Prompt:", type="text"),
|
48 |
+
gr.Textbox(lines=1, placeholder="Enter the URL here...", label="URL to parse:", type="text"),
|
49 |
+
gr.Textbox(lines=1, placeholder="Enter your API-key here...", label="API-Key:", type="password"),
|
50 |
+
gr.Slider(0.0,1.0, value=0.3, label="Temperature:")
|
51 |
+
],
|
52 |
+
outputs=[gr.Textbox(label="Input:"), gr.Textbox(label="Output:"), gr.Textbox(label="Tokens:")],
|
53 |
+
examples=[["Summarize the following text as a list:","https://blog.google/outreach-initiatives/google-org/our-commitment-on-using-ai-to-accelerate-progress-on-global-development-goals/","",0.3],
|
54 |
+
["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]
|
55 |
+
],
|
56 |
+
title="Information Extraction with ChatGPT Interface:",
|
57 |
+
# 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."
|
58 |
+
)
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
error_message = ""
|
63 |
+
|
64 |
+
try:
|
65 |
+
iface.launch()
|
66 |
+
except Exception as e:
|
67 |
+
error_message = "An error occurred: " + str(e)
|
68 |
+
iface.outputs[1].value = error_message
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
transformers
|
3 |
+
newspaper3k
|