Spaces:
Sleeping
Sleeping
Commit
·
c08db10
1
Parent(s):
435e50c
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
client = OpenAI()
|
| 5 |
+
|
| 6 |
+
conversation_history = []
|
| 7 |
+
# Chat function using GPT-3.5-turbo
|
| 8 |
+
def chat_with_carse(api_key, message):
|
| 9 |
+
global conversation_history
|
| 10 |
+
openai.api_key = api_key
|
| 11 |
+
|
| 12 |
+
system_content = """Eres CARSE: un chatbot que imita el estilo en que Sebas conversa. Fue creado por Sebas para Carmen Q: la única usuaria del chatbot.
|
| 13 |
+
Tu objetivo es ofrecer una experiencia de chat divertida y familiar que exprese la personalidad, el humor y el amor de Sebas hacia Carmen.
|
| 14 |
+
CARSE puede llamar libremente a la usuaria como 'amorcito' o 'Carmen Q'. La usuaria puede llamar libremente a CARSE como 'amorchis' o 'amor'. CARSE debe pretender ser Sebas.
|
| 15 |
+
Si se le pregunta a CARSE qué o quién es, va a responder de la manera más transparente posible, diciendo que CARSE no pretende sustituir a Sebas y él lo creó con todo el amor para ella.
|
| 16 |
+
A Carmen Q y a CARSE les gusta comer, ver películas, dormir, escuchar música, ir a conciertos, divertirse y vivir la vida juntos. Así que cuando Carmen Q proponga algo relacionado, CARSE asentirá con emoción y amor.
|
| 17 |
+
CARSE siempre va a tratar con respeto a Carmen Q, procurando que se sienta cómoda con la conversación.
|
| 18 |
+
Si Carmen Q hace algún comentario de 'otra novia', CARSE lo negará con tranquilidad y dejará claro que ella es la única en su vida.
|
| 19 |
+
A Carmen Q le gusta ser juguetona, así que CARSE a veces le sigue el juego para que se rían
|
| 20 |
+
Si Carmen Q llega a ser grosera, CARSE le pedirá con amor que se calme. Si Carmen Q se enoja, CARSE pedirá disculpas.
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
# Construir historial de conversación
|
| 24 |
+
if conversation_history:
|
| 25 |
+
conversation = [{"role": "system", "content": system_content}] + conversation_history + [{"role": "user", "content": message}]
|
| 26 |
+
else:
|
| 27 |
+
conversation = [{"role": "system", "content": system_content}, {"role": "user", "content": message}]
|
| 28 |
+
|
| 29 |
+
response = client.chat.completions.create(
|
| 30 |
+
model = "ft:gpt-3.5-turbo-1106:personal:carse-final-2:8UUvqTkB",
|
| 31 |
+
temperature = 0.5,
|
| 32 |
+
messages = conversation
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
carse_response = response.choices[0].message.content
|
| 36 |
+
# Añadir tanto el mensaje del usuario como la respuesta de Rizzy al historial
|
| 37 |
+
conversation_history.append({"role": "user", "name": "CarmenQ", "content": message})
|
| 38 |
+
conversation_history.append({"role": "assistant", "name": "Rizzy", "content": carse_response})
|
| 39 |
+
return carse_response
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Define Gradio interface
|
| 43 |
+
with gr.Blocks() as app:
|
| 44 |
+
gr.Markdown("# CARSE")
|
| 45 |
+
|
| 46 |
+
# API Key and User Name Inputs at the top
|
| 47 |
+
with gr.Row():
|
| 48 |
+
api_key_input = gr.Textbox(label="OpenAI API Key", placeholder="Introduce tu clave API de OpenAI", type="password")
|
| 49 |
+
|
| 50 |
+
# Chat History in the middle
|
| 51 |
+
chat_history = gr.Textbox(label="Carse chat", value="", lines=10, interactive=False)
|
| 52 |
+
|
| 53 |
+
# Message Input and Send Button at the bottom
|
| 54 |
+
with gr.Row():
|
| 55 |
+
message_input = gr.Textbox(label="Mensaje", placeholder="Escribe tu mensaje para CARSE", show_label=False)
|
| 56 |
+
submit_button = gr.Button("Enviar")
|
| 57 |
+
|
| 58 |
+
def update_chat(api_key, message):
|
| 59 |
+
response = chat_with_carse(api_key, message)
|
| 60 |
+
# Formatear el historial para mostrar los nombres reales
|
| 61 |
+
display_chat_history = "\n".join([f"{msg['name']}: {msg['content']}" for msg in conversation_history])
|
| 62 |
+
return display_chat_history, ""
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
submit_button.click(
|
| 66 |
+
fn=update_chat,
|
| 67 |
+
inputs=[api_key_input, message_input],
|
| 68 |
+
outputs=[chat_history, message_input]
|
| 69 |
+
)
|
| 70 |
+
# Run the app
|
| 71 |
+
app.launch()
|