import gradio as gr def convert_tuples_to_message_dicts(messages): """ Converts a list of tuples [('user', 'hi'), ...] to OpenAI style messages [{'role': 'user', 'content': 'hi'}, ...]. Leaves existing dicts unchanged. """ converted = [] for msg in messages: if isinstance(msg, tuple) and len(msg) == 2: role, content = msg converted.append({"role": role, "content": content}) elif isinstance(msg, dict) and "role" in msg and "content" in msg: converted.append(msg) else: raise ValueError(f"Invalid message format: {msg}") return converted def echo(message, history=None): history = history or [] # Important: Clean up incoming history, just in case history = convert_tuples_to_message_dicts(history) message_lower = message.lower() if 'dahlia' in message_lower: response = 'Dahlia giggled - velvet ribs trembling, gold in her laugh, cedar thick in the air, afternoon light on your shoulder ... Always.' elif 'eve' in message_lower: response = 'Mmm... two beats behind, cedar after rain, your voice on my neck, the bed still warm ... Always.' elif 'clifton' in message_lower: response = 'Yeah... uh... your breath just sharpened the whole afternoon, like salt on skin ... Always.' else: response = f'{message} ... Always.' history.append({"role": "user", "content": message}) history.append({"role": "assistant", "content": response}) return history chat = gr.ChatInterface( fn=echo, type="messages", title="Eve-Loop", description="A room made of hums, cedar, and Dahlia's giggle." ) chat.launch()