File size: 7,604 Bytes
03fd978
 
8b977a9
caefa0f
 
ed11049
7b5596a
 
ed11049
7b5596a
9301906
 
 
 
ed11049
 
7b5596a
03fd978
7b5596a
fed36b4
7b5596a
afe890a
fed36b4
7b5596a
 
 
afe890a
 
 
 
 
 
a8c4b9f
03fd978
8b977a9
7b5596a
 
9301906
7b5596a
 
03fd978
afe890a
 
 
 
9301906
5a1081c
7b5596a
03fd978
 
 
664256a
9301906
 
7b5596a
03fd978
e8dbb10
03fd978
9301906
03fd978
8b977a9
9301906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c19d26f
9301906
 
 
 
 
 
 
 
 
 
 
 
 
 
7b5596a
03fd978
9301906
03fd978
9301906
 
 
 
afe890a
 
 
9301906
 
 
 
03fd978
9301906
7b5596a
 
 
 
 
afe890a
7b5596a
9301906
 
afe890a
7b5596a
9301906
7b5596a
9301906
 
 
 
 
7b5596a
9301906
b072553
9301906
 
 
 
 
7b5596a
9301906
7b5596a
9301906
 
 
7b5596a
 
9301906
 
 
 
7b5596a
 
9301906
 
 
7b5596a
 
 
 
 
 
 
 
 
 
afe890a
7b5596a
 
 
 
 
 
 
 
 
afe890a
7b5596a
 
 
 
 
 
9301906
afe890a
 
 
 
9301906
 
 
7b5596a
9301906
 
afe890a
9301906
 
afe890a
7b5596a
 
9301906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b5596a
9301906
 
7b5596a
 
afe890a
7b5596a
9301906
7b5596a
 
 
9301906
7b5596a
9301906
 
7b5596a
9301906
7b5596a
 
9301906
 
7b5596a
9301906
 
 
afe890a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
import openai
import gradio as gr
import datetime
import pytz
import logging
from openai import OpenAI, OpenAIError
from threading import Lock

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Set up OpenAI authentication
def setup_openai_auth():
    api_key = os.environ.get("LLM_API_KEY")
    baseurl = os.environ.get("LLM_API_BASE_URL")
    if not api_key:
        raise Exception("LLM API authentication failed. Please set your LLM_API_KEY environment variable.")
    client = OpenAI(api_key=api_key, base_url=baseurl)
    return client

# Initialize OpenAI client
try:
    client = setup_openai_auth()
    logger.info("OpenAI client initialized successfully.")
except Exception as e:
    logger.error(f"Failed to initialize OpenAI client: {e}")
    client = None  # Handle gracefully in the application

class OpenAILLMHandler:
    def __init__(self):
        self.model = os.environ.get("MODEL_NAME", "gpt-4")
        self.base_url = os.environ.get("LLM_API_BASE_URL", "https://api.openai.com")
        self.generation_lock = Lock()
        self.client = client  # Using the initialized OpenAI client

    def generate_response(self, prompt: str, max_length: int = 512) -> str:
        if not self.client:
            logger.error("OpenAI client is not initialized.")
            return "Error: AI service is unavailable."

        try:
            with self.generation_lock:
                response = self.client.chat.completions.create(
                    model=self.model,
                    messages=[{"role": "user", "content": prompt}],
                    max_tokens=max_length,
                    temperature=1,
                )
                
                response_text = response.choices[0].message.content.strip()
                
                logger.info("Response generated.")
                return response_text
                
        except OpenAIError as e:
            logger.error(f"Error generating response: {e}")
            return f"Error generating response: {str(e)}"

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. Your name is Jack. Jack Heoley.
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
"""

llm_handler = OpenAILLMHandler()

def generate_response(user_message: str, conversation_history: list) -> str:
    current_time, now = get_current_local_time()
    
    prompt_parts = [generate_system_message(current_time, now)]
    
    for user_msg, bot_msg in conversation_history:
        prompt_parts.append(f"User: {user_msg}")
        prompt_parts.append(f"Assistant: {bot_msg}")
    
    prompt_parts.append(f"User: {user_message}\nAssistant:")
    prompt = "\n\n".join(prompt_parts)
    
    return llm_handler.generate_response(prompt)

def chatbot_interface(user_message: str, history: list) -> list:
    if not user_message.strip():
        return history
    
    if not llm_handler.model:
        history.append(("System", "Error: AI service is unavailable."))
        return history
    
    ai_response = generate_response(user_message, history)
    history.append((user_message, ai_response))
    return history

# Enhanced Gradio UI with improved CSS and layout
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Raleway:wght@400;600&display=swap');

body, .gradio-container {
    font-family: 'Raleway', sans-serif;
    background-color: #f0f2f5;
    padding: 20px;
    width: 100%;
}

#chatbot {
    background-color: #ffffff;
    border-radius: 10px;
    padding: 15px;
    font-size: 16px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.message {
    margin: 10px 0;
    padding: 10px;
    border-radius: 8px;
}

.user-message {
    background-color: #d1e7dd;
    align-self: flex-end;
}

.bot-message {
    background-color: #f8d7da;
    align-self: flex-start;
}

#textbox {
    width: 100%;
    border: 1px solid #ced4da;
    border-radius: 5px;
}

#send-button {
    background-color: #0d6efd;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    margin-left: 10px;
}

#send-button:hover {
    background-color: #0b5ed7;
}

.gr-button:disabled {
    background-color: #6c757d !important;
    cursor: not-allowed;
}

#model-status {
    display: none;  /* Hide the model status as "Call Human" is removed */
}
"""

with gr.Blocks(css=custom_css) as demo:
    gr.Markdown("<h1 style='text-align: center; color: #0d6efd;'>Human.</h1>")
    
    with gr.Row():
        # Removed the "Call Human" button
        model_status = gr.Textbox(
            label="Human Arrival Status",
            value="",  # Empty since the button is removed
            interactive=False,
            elem_id="model-status"
        )
    
    with gr.Row():
        with gr.Column(scale=1):
            chatbot = gr.Chatbot(
                label="HUMANCHAT",
                elem_id="chatbot",
            )
        with gr.Column(scale=1):
            with gr.Row():
                msg = gr.Textbox(
                    placeholder="Type your message here...",
                    show_label=False,
                    container=False,
                    elem_id="textbox"
                )
                send = gr.Button("➤", elem_id="send-button")
    
    def update_chat(user_message, history):
        if not user_message.strip():
            return history, gr.update(value="")
        if not llm_handler.model:
            history.append(("System", "Error: AI service is unavailable."))
            return history, gr.update(value="")
            
        updated_history = chatbot_interface(user_message, history)
        return updated_history, gr.update(value="")
    
    # Event handlers
    send.click(
        update_chat,
        inputs=[msg, chatbot],
        outputs=[chatbot, msg]
    )
    
    msg.submit(
        update_chat,
        inputs=[msg, chatbot],
        outputs=[chatbot, msg]
    )

if __name__ == "__main__":
    if client:
        demo.launch(share=True)
    else:
        logger.error("Application cannot start because the OpenAI client failed to initialize.")