import re # for cutting links out of Wi import gradio as gr import os import pandas as pd #Versión previa utiliza OpenAI #from langchain.chat_models import ChatOpenAI #En local, utilizo olama #from langchain_community.llms import Ollama #En la web, inference cliente from huggingface_hub import InferenceClient def Query_Model(prompt, temp, model): response = client.text_generation(prompt=prompt, model=model, max_new_tokens=6000,return_full_text=False) #max_new_tokens=6000,temperature=temp, print("Model:", model, " Temp:", temp, "Prompt len:", len(prompt), " Answer len :", len (response)) return response #.json() path_to_file="" dfStyles = pd.read_csv(path_to_file + "styles.csv", sep=";") dfTypes = pd.read_csv(path_to_file + "types.csv",sep=";") #def load_model (): ### elegir Modelo # return Ollama(model="zephyr") #Modelo por defecto GPT_MODEL = "default" def num_tokens(text: str, model: str) -> int: """Return the number of tokens in a string.""" encoding = "Pendiente de escribir la funcion de tokens" return len(encoding) #def Query_Model (temperature, prompt) -> str: # """Implementa la llamada al Modelo que seleccionemos""" # response=llm.invoke(input=prompt, temperature=0.1) # return response def ask( query: str, model: str = GPT_MODEL, token_budget: int = 4096 - 500, print_message: bool = False, temp: float = 0 ) -> str: response = Query_Model(temp=temp, prompt=query, model=model) #Esta rutina depende del modelo a utilizar #response_message = response["choices"][0]["message"]["content"] response_message=response tokens=num_tokens(response_message, "gamma") return [response_message, str(tokens)+" Tokens usados en la consulta"] def create_prompt (tipo, estilo, context, num_words, tono, lang="1-English", url="None", adicional="") : prompt = f""" Perform the following actions: Step 1 - {tipo}. The context is delimited by triple backticks.\ Write in a {estilo} style. \ Use a {tono} tone. \ The text must have less than {num_words} words. The text must have more than {num_words-20} words. The text must be in {lang[2:]}. Don´t echo the prompt. """ if len(adicional)>0: prompt=prompt+f"""The text must highlight this idea: {adicional} . """ prompt= prompt + f""" Step 2 - Create a title for the text. Step 3 - Select the 3 most relevant words of the text. Step 4 - Provide a prompt to describe an image that fits the text that have less than 30 words. Use the following format: Title: title Text: text URL: Literal, {url} Tags: list of relevant words precedeed by # Image: prompt for the image Disclaimer: Literal, \"Disclaimer, Content created by Generative AI (ChatGPT, Midjourney & Lexicart) using a Custom Built APP\" Context: \"\"\"{context}\"\"\" """ prompt=context return prompt def consultaGPT (tipo, estilo, context, viewPrompt,num_words, temp, lang, url, tono, adicional, modelo): prompt = create_prompt(tipo, estilo, context, num_words, tono,lang, url, adicional) tvalor=0 if (temp[0]=="1"): #Conservador tvalor=0.1 elif (temp[0]=="2"): #Focused tvalor=0.6 else: #libre, creativo tvalor=0.9 if viewPrompt==0: response, tokens = ask(query=prompt, model=modelo, temp=tvalor) #pyperclip.copy(response) return [response, tokens, prompt] else: return ["",5, prompt] with gr.Blocks() as demo: gr.Markdown("""

Linkedin Post Generator v1

Create intelligent linkedin posts about a provided context. Set the tone and the intention of the post, copy & paste on linkedin.


Pablo Medina

""") with gr.Row(): txt_context = gr.Textbox(placeholder="Prompt",lines=3, max_lines=10, label="Contexto ChatGPT. Pega una noticia, o escribe tu idea.") with gr.Row(): txt_URL = gr.Textbox(placeholder="URL de contexto", label="URL del artículo original (opcional).") with gr.Row(): #with gr.Column(): #https://www.gptpromptshub.com/blog/best-chatgpt-prompts-for-linkedin cmb_stile=gr.Dropdown(dfStyles["style"].to_list() , label="Voz del Escritor") cmb_tipo=gr.Dropdown(dfTypes["type"].to_list(), label="Objetivo del Post") with gr.Row(): txt_adicional = gr.Textbox(placeholder="Información Adicional para el Prompt (Objetivo)",lines=1, max_lines=3, label="Contexto Adicional") with gr.Row(): chkPrompt = gr.Checkbox(label="Only Prompt") snum_words=gr.Slider(maximum=400, minimum=30, label="Target Word Count for Post",value=50) cmb_temp = gr.Dropdown(["1-Conservador", "2-Normal", "3-Libre"], label="Temperatura", value="2-Normal") cmb_lang = gr.Dropdown(choices= ["1-English", "2-Spanish"], label="Idioma", value="2-Spanish") cmb_tono = gr.Dropdown(choices=["Positive", "Negative", "Sarcastic", "Sad", "Happy", "Neutral"], label="Tono", value="Neutral") with gr.Row(): with gr.Column(scale=1): with gr.Row(): cmb_model = gr.Dropdown(choices= ["tiiuae/falcon-7b", "google/gemma-7b", "meta-llama/Llama-2-7b", "meta-llama/Llama-2-70b", "mistralai/Mistral-7B-v0.1", "databricks/dolly-v2-12b"], label="Modelo", value="google/gemma-7b") with gr.Row(): with gr.Column(scale=1): tok=gr.Textbox(placeholder="Tokens", label="Numero de Tokens") with gr.Row(): with gr.Column(scale=1): btn = gr.Button("Respuesta") with gr.Row(): btnGrabar = gr.Button("Grabar") with gr.Column(scale=4): out = gr.Textbox(lines=3, max_lines=10, label="Respuesta basada en documentos proporcionados") txtPrompt = gr.Textbox(lines=2, label="Prompt Utilizado") btn.click(fn=consultaGPT, inputs=[cmb_tipo, cmb_stile, txt_context, chkPrompt,snum_words, cmb_temp, cmb_lang , txt_URL, cmb_tono, txt_adicional, cmb_model ], outputs=[out, tok, txtPrompt]) #btnGrabar.click(fn=write_to_prompt_history, inputs=[out, txtPrompt]) if 1==1: API_TOKEN = os.environ['HF_TOKEN'] client = InferenceClient() #else: # llm=load_model() demo.launch()