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): page = newspaper.Article(url=page_url) page.download() page.parse() 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 response_text, num_tokens except Exception as e: return f"--- Ha ocurrido un error al procesar la solicitud: {e} ---", num_tokens return "--- 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:", 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="Output:"), gr.Textbox(label="Tokens:")], 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], ["Extract the main topics of the following text:", "https://ai.googleblog.com/2019/10/quantum-supremacy-using-programmable.html","",0.7] ] ) iface.launch(inline=False)