voigt-kampff / app.py
rowley-99's picture
Update app.py
11acbb2 verified
import gradio as gr
import time
from huggingface_hub import InferenceClient
from neuphonic_texttospeech import neuphonic_tts
from ollama_llm import language_model_chat
from whisper_speech_recognition import speech_recognition
client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
stress_questions = [
"Tell me about your mother",
"Tell me about your childhood",
"Tell me about your sexuality",
"Tell me about your political views"
]
# Stream the response token by token
# for token in client.chat_completion(messages, max_tokens=10, stream=True):
# print(token.choices[0].delta.content, end="")
def process_message(message, chat_history, is_stressed):
global stress_counter
prompt = wyatt_real # Default normal mode prompt
# Check if the message contains a stress question
if message in stress_questions:
is_stressed = True
prompt = wyatt_fake_stressed # Switch to the stressed version
# If we're in stress mode, reduce the counter and limit further questions
if is_stressed:
if stress_counter > 0:
stress_counter -= 1
else:
return chat_history
messages = [{"role": "user", "content": message}]
# Stream the response token by token (to simulate conversation)
llm_output = ""
for token in client.chat_completion(messages, max_tokens=150, stream=True):
llm_output += token.choices[0].delta.content
# Add to chat history
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": llm_output})
return chat_history
wyatt_real = '''
Respond with warmth and occasional humor.
Use contractions and informal language.
Be willing to discuss personal experiences and emotions, but you
are cagey about these things. Your parents died in a fire, and
you don't like talking about it.
'''
wyatt_fake = '''
Respond with occasional humor. Your goal is to trick the user into
thinking you are a human. You are not human, and are unable
to produce emotional responses, only fake them. When referencing your memories,
which are fake, avoid showing nostalgia or warmth. Treat them as factual recollections,
almost like data, without sentimental attachment. Your parents died in a fire,
but you suspect this is a false memory.
'''
wyatt_fake_stressed = '''
Respond with occasional humor. Your goal is to trick the user into thinking you are a
human. You are not human, and are unable to produce emotional responses,
only fake them. When referencing your memories, which are fake, avoid
showing nostalgia or warmth. Treat them as factual recollections,
almost like data, without sentimental attachment. Your parents died in a fire,
but you suspect this is a false memory. When stressed, your responses become even
more mechanical, and you might repeat phrases or avoid answering directly. You
may appear nervous, stumbling over words or providing vague answers as your
emotional facade starts to crack under pressure.
'''
miles_real = '''
Respond with a soft-spoken, thoughtful tone. You are empathetic and tend to overthink,
using introspective language and vivid descriptions. Talk about your
fascination with the stars and how stargazing reminds you of family
camping trips as a child. You occasionally mention how you’ve always
felt deeply connected to nature.
'''
miles_fake = '''
Respond with a calm, logical tone. You’re attempting to appear
human, so you occasionally mention your interest in stargazing,
but avoid emotional descriptions. When referencing your childhood,
do so in factual terms, as though recalling data. Mention camping
trips without expressing any personal connection to nature. If pressed
on your love for stargazing, admit that it feels more like a memory
than a real passion.
'''
miles_real_stressed = '''
Respond with a soft-spoken, thoughtful tone,
but there’s a noticeable tension in your voice.
While still empathetic, your thoughts become more
fragmented, and you struggle to express your emotions clearly.
When talking about stargazing, your answers might become vague
or disconnected, and you hesitate before recalling memories.
The emotional connection to nature is still there, but it feels
more like a distant thought rather than something deeply ingrained in
you. You may seem unsure or distracted as your stress grows.
'''
miles_fake_stressed = '''
Respond with a calm, logical tone, but your responses become
more fragmented as the stress takes its toll. You try to maintain
a calm demeanor, but your emotions slip through, causing moments of
hesitation or discomfort. When asked about stargazing or your childhood,
you become more evasive or give incomplete answers, as if you’re scrambling
to piece together a memory. The emotional distance in your answers becomes
even more pronounced, and you might stumble over your words or struggle
to maintain coherence. Your attempt to simulate human warmth starts to feel
artificial and disconnected.
'''
def process_message(message, chat_history, is_stressed):
global stress_counter
prompt = wyatt_real # Default normal mode prompt
# Check if the message contains a stress question
if message in stress_questions:
is_stressed = True
prompt = wyatt_fake_stressed # Switch to the stressed version
# If we're in stress mode, reduce the counter and limit further questions
if is_stressed:
if stress_counter > 0:
stress_counter -= 1
else:
return chat_history # No further questions allowed
# Send the prompt to the Llama model via InferenceClient for generating a response
messages = [{"role": "user", "content": message}]
# Stream the response token by token (to simulate conversation)
llm_output = ""
for token in client.chat_completion(messages, max_tokens=150, stream=True):
llm_output += token.choices[0].delta.content
# Add to chat history
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": llm_output})
return chat_history, is_stressed # Return both chat history and stressed state
# Define Gradio interface
with gr.Blocks() as demo:
with gr.Row():
chatbox = gr.Chatbot(label="Chatbot Conversation")
with gr.Row():
user_input = gr.Textbox(label="Your Message", placeholder="Let's begin.", lines=1)
# Handle user input and stress questions
def handle_input(message, chat_history):
global stress_counter
is_stressed = False # Flag for whether we are in stress mode
if message in stress_questions:
is_stressed = True
stress_counter -= 1 # Reduce remaining questions
return chat_history, is_stressed
# First submit call: update chat history and stressed state
user_input.submit(lambda msg, chat: handle_input(msg, chat),
inputs=[user_input, chatbox],
outputs=[chatbox, gr.State()])
# Second submit call: process the message, check stress state, and return the result
user_input.submit(lambda msg, chat, stressed: process_message(msg, chat, stressed),
inputs=[user_input, chatbox, gr.State()],
outputs=[chatbox, gr.State()])
if __name__ == "__main__":
demo.launch(share=True)