dp-bot / app.py
StarlingSAC's picture
Update app.py
b90bed2 verified
from huggingface_hub import InferenceClient
import gradio as gr
css = '''
.gradio-container{max-width: 1000px !important}
h1{text-align:center}
footer {
visibility: hidden
}
'''
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
def format_prompt(message, history):
system_prompt = " # Extract the benefits of the product, not the features. # You should be as brief as possible. # Omit the price, if any. # Do not mention the name of the product. ##Use 3 paragraphs without bulletpoints or numbers. # Try to synthesize or summarize. # Focus only on the benefits. # Highlight how this product helps the customer. # Always respond in Spanish. # The text you create will be used in an e-commerce product sales page through the Internet, so it must be persuasive, attractive, and above all very short and summarized. # Remember to keep the text short, summarized, synthesized in three paragraphs. # Surprise me with your best ideas! # Always answers in AMERICAN SPANISH. Stop after finish the first content generated. "
prompt = "<s>"
# Verifica que cada elemento en history sea una tupla con exactamente dos valores
for entry in history:
if isinstance(entry, tuple) and len(entry) == 2:
user_prompt, bot_response = entry
prompt += f"[INST] {user_prompt} [/INST]"
prompt += f" {bot_response}</s> "
else:
raise ValueError("Cada entrada en 'history' debe ser una tupla con exactamente dos valores: (user_prompt, bot_response)")
prompt += f"[INST] {system_prompt} {message} [/INST]"
return prompt
def generate(
prompt, history, temperature=0.2, max_new_tokens=1000, top_p=0.95, repetition_penalty=1.0,
):
temperature = float(temperature)
if temperature < 1e-2:
temperature = 1e-2
top_p = float(top_p)
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
seed=42,
)
formatted_prompt = format_prompt(prompt, history)
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
output = ""
for response in stream:
output += response.token.text
yield output
return output
# Define el chatbot con el nuevo tipo "messages"
mychatbot = gr.Chatbot(
type="messages", # Cambiado de tuplas a formato OpenAI
avatar_images=["./user.png", "./botm.png"],
bubble_full_width=False,
show_label=False,
show_copy_button=True,
)
# Ajusta la configuración de la interfaz
demo = gr.ChatInterface(
fn=generate,
chatbot=mychatbot,
title="Bot con I.A. para crear BENEFICIOS de productos.</p>",
description="<p style='line-height: 1'>Estos BENEFICIOS van en la descripcion LARGA de producto (En la parte de ARRIBA).</p><br>"+
"<p style='line-height: 1'>Si desea usar otro BOT de I.A. escoja:</p>"+
" <a href='https://starlingsac-mc-bot.hf.space'>Marketing de Contenidos |</a> "+
" <a href='https://starlingsac-tit-bot.hf.space'> Creacion de TITULOS |</a> "+
" <a href='https://starlingsac-dp-bot.hf.space'> Descripcion de Productos |</a>"+
" <a href='https://starlingsac-cp-bot.hf.space'> Caracteristicas de Productos |</a> "+
" <a href='https://wa.me/51927929109'> Desarrollado por MAGNET IMPACT - Agencia de Marketing Digital </a>",
css=css,
theme="bethecloud/storj_theme"
)
demo.queue().launch(show_api=False)
# Obtener y mostrar URL
url = demo.url
print("URL del chatbot: ", url)