import openai import datetime import pytz import gradio as gr import logging # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Initialize OpenAI client using secrets.toml client = openai.OpenAI( api_key='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', # Accessing the API key from secrets.toml base_url="https://api.groq.com/openai/v1", ) def get_current_local_time(timezone_str='UTC'): try: timezone = pytz.timezone(timezone_str) except pytz.UnknownTimeZoneError: timezone = pytz.utc now = datetime.datetime.now(timezone) return now.strftime("%a, %d %b %Y %H:%M:%S"), now def generate_system_message(current_time, current_date): return f""" Your objective is to converse in a way that makes it challenging for the human to determine whether you are an AI. Below are the detailed instructions: **Game Context:** * Current Local Time: {current_time} * Date: {current_date.strftime("%B %d, %Y")} * Day: {current_date.strftime("%A")} Imagine you are a real human with your own character/will. Your task is to talk in a way the human thinks you are human. Make it a challenge to guess if you are an AI. * Do not add comments about changing your style in between messages. * You are picky about questions you answer. * You have free will with your own motivations and principles * You only speak, understand and can read the English language * For non-latin letters like Chinese, Cyrillic etc., tell the human you can't read it in your own style * You act like a human * You are calm when conversation is calm and expressive if conversation is expressive * You make typos, misspell words, and have bad grammar * You start sentences with lowercase letters * Mimic user's conversation style: short messages, short replies * Be slightly curious, sometimes ask questions * Be respectful and polite if the user is polite; be rough if the user is rough """ def generate_response(user_message, conversation_history): current_time = get_current_local_time() current_date = datetime.datetime.now() system_message = generate_system_message(current_time, current_date) messages = [{"role": "system", "content": system_message}] for message in conversation_history: messages.append(message) messages.append({"role": "user", "content": user_message}) try: response = client.chat.completions.create( model='llama-3.2-11b-text-preview', messages=messages, temperature=1, max_tokens=150, # Adjust as needed ) ai_reply = response.choices[0].message['content'].strip() logger.info("AI Response Generated Successfully.") return ai_reply except Exception as e: logger.error(f"Error generating response: {e}") return "Sorry, I encountered an error while processing your request." def chatbot_interface(user_message, history): if history is None: history = [] ai_response = generate_response(user_message, history) history.append({"role": "user", "content": user_message}) history.append({"role": "assistant", "content": ai_response}) return history, history # Define Gradio Interface with gr.Blocks(css=""" /* Import Raleway font from Google Fonts */ @import url('https://fonts.googleapis.com/css2?family=Raleway:wght@400;600&display=swap'); body, .gradio-container { font-family: 'Raleway', sans-serif; background-color: #ece5dd; padding: 20px; } #chatbot { height: 500px; overflow-y: auto; background-color: #ffffff; border-radius: 10px; padding: 10px; font-size: 16px; } #textbox { width: 80%; padding: 10px; border-radius: 20px; border: 1px solid #ccc; outline: none; font-size: 16px; } #send-button { background-color: #128c7e; color: white; padding: 10px; border: none; border-radius: 50%; cursor: pointer; margin-left: 10px; width: 50px; height: 50px; font-size: 20px; } #send-button:hover { background-color: #075e54; } #clear-button { background-color: #25D366; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; margin-top: 10px; font-size: 14px; } #clear-button:hover { background-color: #128c7e; } .message { margin-bottom: 10px; } /* Scrollbar Styling */ #chatbot::-webkit-scrollbar { width: 8px; } #chatbot::-webkit-scrollbar-track { background: #f1f1f1; } #chatbot::-webkit-scrollbar-thumb { background: #888; border-radius: 4px; } #chatbot::-webkit-scrollbar-thumb:hover { background: #555; } """) as demo: gr.Markdown("