Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
page = newspaper.Article(url=page_url)
|
13 |
+
page.download()
|
14 |
+
page.parse()
|
15 |
+
|
16 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
17 |
+
|
18 |
+
tokens = tokenizer.tokenize(page.text)
|
19 |
+
num_tokens = len(tokens)
|
20 |
+
|
21 |
+
if num_tokens > 10 and num_tokens < 2000:
|
22 |
+
openai.api_key = contraseña
|
23 |
+
# get the response from openai API
|
24 |
+
response = openai.Completion.create(
|
25 |
+
engine="text-davinci-003",
|
26 |
+
prompt=request + "\n\n" + page.text,
|
27 |
+
max_tokens=2048,
|
28 |
+
temperature=temp,
|
29 |
+
top_p=0.9,
|
30 |
+
)
|
31 |
+
# get the response text
|
32 |
+
response_text = response.choices[0].text
|
33 |
+
# clean the response text
|
34 |
+
response_text = re.sub(r'\s+', ' ', response_text)
|
35 |
+
return response_text, num_tokens
|
36 |
+
return "--- Something went wrong! ---", num_tokens
|
37 |
+
|
38 |
+
# define the gradio interface
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=text_prompt,
|
41 |
+
inputs=[gr.Textbox(lines=1, placeholder="Enter your prompt here...", label="Prompt:", type="text"),
|
42 |
+
gr.Textbox(lines=1, placeholder="Enter the URL here...", label="URL:", type="text"),
|
43 |
+
gr.Textbox(lines=1, placeholder="Enter your API-key here...", label="API-Key:", type="password"),
|
44 |
+
gr.Slider(0.0,1.0, value=0.3, label="Temperature:")
|
45 |
+
],
|
46 |
+
outputs=[gr.Textbox(label="Output:"), gr.Textbox(label="Tokens:")],
|
47 |
+
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],
|
48 |
+
["Extract the main topics of the following text:", "https://ai.googleblog.com/2019/10/quantum-supremacy-using-programmable.html","",0.7]
|
49 |
+
]
|
50 |
+
)
|
51 |
+
iface.launch()
|