from huggingface_hub import InferenceClient import gradio as gr css = ''' .gradio-container{max-width: 690px !important} h1{text-align:center} footer { visibility: hidden } ''' client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3") 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 format_prompt(message, history, system_prompt=None, mood=None): prompt = "" if mood: mood_description = mood_prompts.get(mood, "") prompt += f"[SYS] {mood_description} [/SYS] " for user_prompt, bot_response in history: prompt += f"[INST] {user_prompt} [/INST]" prompt += f" {bot_response} " if system_prompt: prompt += f"[SYS] {system_prompt} [/SYS]" prompt += f"[INST] {message} [/INST]" return prompt def generate( prompt, history, system_prompt=None, mood=None, temperature=0.2, max_new_tokens=1024, 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, system_prompt, mood) 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 def gradio_interface(): with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo: # Initialize state for history history = gr.State([]) # Row for mood selection with gr.Row(): mood = gr.Radio(choices=list(mood_prompts.keys()), value="Professional", label="Select Mood") # Row for system prompt and user prompt with gr.Row(): system_prompt = gr.Textbox(placeholder="System prompt (optional)", lines=1) prompt = gr.Textbox(placeholder="Enter your message", lines=2) # Row for generate button and output with gr.Row(): generate_btn = gr.Button("Generate") output = gr.Chatbot() # Connect button click to generate function generate_btn.click( generate, inputs=[prompt, history, system_prompt, mood], outputs=[output] ) demo.queue().launch(show_api=False) gradio_interface()