CoolFace
Apppublic

pmv-hou/linkedin_post_generator

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py179 linesDownload Raw Back to root
1import re  # for cutting <ref> links out of Wi2import gradio as gr3import os4import pandas as pd5 6#Versión previa utiliza OpenAI7#from langchain.chat_models import ChatOpenAI8 9#En local, utilizo olama10#from langchain_community.llms import Ollama11 12#En la web, inference cliente13from huggingface_hub import InferenceClient14 15 16def Query_Model(prompt, temp, model):    17    response = client.text_generation(prompt=prompt, model=model, max_new_tokens=6000,return_full_text=False) #max_new_tokens=6000,temperature=temp,18    print("Model:", model, " Temp:", temp, "Prompt len:", len(prompt), " Answer len :", len (response))19    return response #.json()20 21path_to_file=""22 23dfStyles = pd.read_csv(path_to_file + "styles.csv", sep=";")24dfTypes = pd.read_csv(path_to_file + "types.csv",sep=";")25 26#def load_model ():27    ### elegir Modelo28 #   return Ollama(model="zephyr")29 30#Modelo por defecto31GPT_MODEL = "default"32 33def num_tokens(text: str, model: str) -> int:34    """Return the number of tokens in a string."""35    encoding = "Pendiente de escribir la funcion de tokens"36    return len(encoding)37 38#def Query_Model (temperature, prompt) -> str:39#    """Implementa la llamada al Modelo que seleccionemos"""40#   response=llm.invoke(input=prompt, temperature=0.1)41#    return response42 43def ask(44    query: str,    45    model: str = GPT_MODEL,46    token_budget: int = 4096 - 500,47    print_message: bool = False, 48    temp: float = 049) -> str:   50    51 52    response = Query_Model(temp=temp, prompt=query, model=model)53 54    #Esta rutina depende del modelo a utilizar55    #response_message = response["choices"][0]["message"]["content"]56    response_message=response57    tokens=num_tokens(response_message, "gamma")58    return [response_message, str(tokens)+" Tokens usados en la consulta"]59 60def create_prompt (tipo, estilo, context, num_words, tono, lang="1-English", url="None", adicional="") :61    62    prompt = f"""63    Perform the following actions: 64 65    Step 1 - {tipo}. The context is delimited by triple backticks.\66    Write in a {estilo} style. \67    Use a {tono} tone. \68    The text must have less than {num_words} words. 69    The text must have more than {num_words-20} words. 70    The text must be in {lang[2:]}.71    Don´t echo the prompt.72    """73 74    if len(adicional)>0:75        prompt=prompt+f"""The text must highlight this idea: {adicional} .76        """77 78    prompt= prompt + f"""    79    Step 2 - Create a title for the text.80    Step 3 - Select the 3 most relevant words of the text.81    Step 4 - Provide a prompt to describe an image that fits the text that have less than 30 words.82 83    Use the following format:84    Title: title85    Text: text86    URL: Literal, {url}87    Tags: list of relevant words precedeed by #88    Image: prompt for the image89    Disclaimer: Literal, \"Disclaimer, Content created by Generative AI (ChatGPT, Midjourney & Lexicart) using a Custom Built APP\"90  91    Context:92    \"\"\"{context}\"\"\"93    """94    prompt=context95    return prompt96 97 98 99def consultaGPT (tipo, estilo, context, viewPrompt,num_words, temp, lang, url, tono, adicional, modelo):100    101    prompt = create_prompt(tipo, estilo, context, num_words, tono,lang, url, adicional)102    103    tvalor=0104    if (temp[0]=="1"): #Conservador105        tvalor=0.1106    elif (temp[0]=="2"): #Focused107        tvalor=0.6108    else:     #libre, creativo109        tvalor=0.9110 111    if viewPrompt==0:112        response, tokens = ask(query=prompt, model=modelo, temp=tvalor)113        #pyperclip.copy(response)114        return [response, tokens, prompt]115    else:116        return ["",5, prompt]117 118with gr.Blocks() as demo:119 120    gr.Markdown("""121    <div display="inline-block">122    <img width="50" src="https://cdn-lfs.huggingface.co/repos/96/a2/96a2c8468c1546e660ac2609e49404b8588fcf5a748761fa72c154b2836b4c83/942cad1ccda905ac5a659dfd2d78b344fccfb84a8a3ac3721e08f488205638a0?response-content-disposition=inline%3B+filename*%3DUTF-8%27%27hf-logo.svg%3B+filename%3D%22hf-logo.svg%22%3B&response-content-type=image%2Fsvg%2Bxml&Expires=1710347057&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTcxMDM0NzA1N319LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy5odWdnaW5nZmFjZS5jby9yZXBvcy85Ni9hMi85NmEyYzg0NjhjMTU0NmU2NjBhYzI2MDllNDk0MDRiODU4OGZjZjVhNzQ4NzYxZmE3MmMxNTRiMjgzNmI0YzgzLzk0MmNhZDFjY2RhOTA1YWM1YTY1OWRmZDJkNzhiMzQ0ZmNjZmI4NGE4YTNhYzM3MjFlMDhmNDg4MjA1NjM4YTA%7EcmVzcG9uc2UtY29udGVudC1kaXNwb3NpdGlvbj0qJnJlc3BvbnNlLWNvbnRlbnQtdHlwZT0qIn1dfQ__&Signature=HnSZbSChBLRz3xCuIIG5moJYpVyQLFL8zqUsvZ8j6nDkHHrB0RZzYbfo7WYbbMMD7Yrky4sWZhkjfiJtcbfRnGa7TPUhOJjsMC%7E2au-a-QyWSv49C%7Ecn7%7EkM7BkEieMW2g6m-AEyXdGeD0VR4bDCHWeYUjeWa0iS210FEcFOlmOAc7pV2VqKaMfw4imZqR-IeKJhomyB9pOAm36WW2SZAHjxi2LEL7dLJEi2YUNm607mEY39RTsT7AbaF2jfcYD8o03rKpJBd7VhYfQmW8CsclWbhRCmk%7EOPHExZMsiS8gJsNHvgVLtzCLqX10Hmy3Ues%7EVvv1kqMb1z5USx1shlEQ__&Key-Pair-Id=KVTP0A1DKRTAX">123    </div>124    125    <h2> Linkedin Post Generator v1 </h2>126    <p>Create intelligent linkedin posts about a provided context. Set the tone and the intention of the post, copy & paste on linkedin. </p>127    <hr />128    <p><em>Pablo Medina</em></p>""")129 130    with gr.Row():131        txt_context = gr.Textbox(placeholder="Prompt",lines=3, max_lines=10,  label="Contexto ChatGPT. Pega una noticia, o escribe tu idea.")132    with gr.Row():133        txt_URL = gr.Textbox(placeholder="URL de contexto", label="URL del artículo original (opcional).")134 135    with gr.Row():136        #with gr.Column():137            #https://www.gptpromptshub.com/blog/best-chatgpt-prompts-for-linkedin138        cmb_stile=gr.Dropdown(dfStyles["style"].to_list() , label="Voz del Escritor")139        cmb_tipo=gr.Dropdown(dfTypes["type"].to_list(), label="Objetivo del Post")140    141    with gr.Row():142        txt_adicional = gr.Textbox(placeholder="Información Adicional para el Prompt (Objetivo)",lines=1, max_lines=3,  label="Contexto Adicional")143 144    with gr.Row():145        chkPrompt = gr.Checkbox(label="Only Prompt")146        snum_words=gr.Slider(maximum=400, minimum=30, label="Target Word Count for Post",value=50)147        cmb_temp =  gr.Dropdown(["1-Conservador", "2-Normal", "3-Libre"], label="Temperatura", value="2-Normal")148        cmb_lang =  gr.Dropdown(choices= ["1-English", "2-Spanish"], label="Idioma", value="2-Spanish")149        cmb_tono = gr.Dropdown(choices=["Positive", "Negative", "Sarcastic", "Sad", "Happy", "Neutral"],  label="Tono", value="Neutral")150    151    with gr.Row():152        with gr.Column(scale=1):153            with gr.Row():154                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")155            with gr.Row():    156                with gr.Column(scale=1):                    157                    tok=gr.Textbox(placeholder="Tokens", label="Numero de Tokens")158            with gr.Row(): 159                with gr.Column(scale=1):160                    btn = gr.Button("Respuesta")161            with gr.Row():162                btnGrabar = gr.Button("Grabar")163 164        with gr.Column(scale=4):165            out = gr.Textbox(lines=3, max_lines=10, label="Respuesta basada en documentos proporcionados")166            txtPrompt = gr.Textbox(lines=2, label="Prompt Utilizado")167    168    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])169    #btnGrabar.click(fn=write_to_prompt_history, inputs=[out, txtPrompt])170 171 172if 1==1:173    API_TOKEN = os.environ['HF_TOKEN']174    client = InferenceClient()175#else:    176#    llm=load_model()177 178demo.launch() 179