pmv-hou's picture
Update app.py
5360b5d verified
Raw History Blame Contribute Delete
7.68 kB
import re # for cutting <ref> 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("""
<div display="inline-block">
<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">
</div>
<h2> Linkedin Post Generator v1 </h2>
<p>Create intelligent linkedin posts about a provided context. Set the tone and the intention of the post, copy & paste on linkedin. </p>
<hr />
<p><em>Pablo Medina</em></p>""")
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()