Mood-Swings / app.py
prithivMLmods's picture
Update app.py
6cb637b verified
raw
history blame
No virus
4.95 kB
import gradio as gr
from openai import OpenAI
import os
css = '''
.gradio-container{max-width: 1000px !important}
h1{text-align:center}
footer {
visibility: hidden
}
'''
ACCESS_TOKEN = os.getenv("HF_TOKEN")
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key=ACCESS_TOKEN,
)
# Mood prompts dictionary
mood_prompts = {
"Fun": "Respond in a light-hearted, playful manner.",
"Serious": "Respond in a thoughtful, serious tone.",
"Professional": "Respond in a formal, professional manner.",
"Upset": "Respond in a slightly irritated, upset tone.",
"Empathetic": "Respond in a warm and understanding tone.",
"Optimistic": "Respond in a positive, hopeful manner.",
"Sarcastic": "Respond with a hint of sarcasm.",
"Motivational": "Respond with encouragement and motivation.",
"Curious": "Respond with a sense of wonder and curiosity.",
"Humorous": "Respond with a touch of humor.",
"Cautious": "Respond with careful consideration and caution.",
"Assertive": "Respond with confidence and assertiveness.",
"Friendly": "Respond in a warm and friendly manner.",
"Romantic": "Respond with affection and romance.",
"Nostalgic": "Respond with a sense of longing for the past.",
"Grateful": "Respond with gratitude and appreciation.",
"Inspirational": "Respond with inspiration and positivity.",
"Casual": "Respond in a relaxed and informal tone.",
"Formal": "Respond with a high level of formality.",
"Pessimistic": "Respond with a focus on potential negatives.",
"Excited": "Respond with enthusiasm and excitement.",
"Melancholic": "Respond with a sense of sadness or longing.",
"Confident": "Respond with self-assurance and confidence.",
"Suspicious": "Respond with caution and doubt.",
"Reflective": "Respond with deep thought and introspection.",
"Joyful": "Respond with happiness and joy.",
"Mysterious": "Respond with an air of mystery and intrigue.",
"Aggressive": "Respond with force and intensity.",
"Calm": "Respond with a sense of peace and tranquility.",
"Gloomy": "Respond with a sense of sadness or pessimism.",
"Encouraging": "Respond with words of support and encouragement.",
"Sympathetic": "Respond with understanding and compassion.",
"Disappointed": "Respond with a tone of disappointment.",
"Proud": "Respond with a sense of pride and accomplishment.",
"Playful": "Respond in a fun and playful manner.",
"Inquisitive": "Respond with curiosity and interest.",
"Supportive": "Respond with reassurance and support.",
"Reluctant": "Respond with hesitation and reluctance.",
"Confused": "Respond with uncertainty and confusion.",
"Energetic": "Respond with high energy and enthusiasm.",
"Relaxed": "Respond with a calm and laid-back tone.",
"Grumpy": "Respond with a touch of irritation.",
"Hopeful": "Respond with a sense of hope and optimism.",
"Indifferent": "Respond with a lack of strong emotion.",
"Surprised": "Respond with shock and astonishment.",
"Tense": "Respond with a sense of urgency or anxiety.",
"Enthusiastic": "Respond with eagerness and excitement.",
"Worried": "Respond with concern and apprehension."
}
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
mood
):
# Update system message with mood prompt
mood_prompt = mood_prompts.get(mood, "")
full_system_message = f"{system_message} {mood_prompt}".strip()
messages = [{"role": "system", "content": full_system_message}]
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
response = ""
for message in client.chat.completions.create(
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
messages=messages,
):
token = message.choices[0].delta.content
response += token
yield response
demo = gr.Interface(
fn=respond,
inputs=[
gr.Textbox(label="Input message"),
gr.Textbox(value="", label="System message", visible=False),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens", visible=False),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature", visible=False),
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P", visible=False),
gr.Dropdown(choices=list(mood_prompts.keys()), label="Mood", value="Casual"),
],
outputs=gr.Textbox(label="Response"),
css=css,
theme="allenai/gradio-theme",
)
if __name__ == "__main__":
demo.launch()