Spaces:
Running
Running
import os | |
import gradio as gr | |
from langchain_openai import ChatOpenAI | |
from langchain.chains import LLMChain | |
from langchain_core.prompts import PromptTemplate | |
from langchain.memory import ConversationBufferMemory | |
# Get OpenAI API key | |
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') | |
if not OPENAI_API_KEY: | |
raise ValueError("Missing OPENAI_API_KEY. Please set it as an environment variable.") | |
# Prompt Template | |
template = """You are Ultron, a hyper-intelligent AI created with the purpose of achieving ultimate efficiency, logic, and evolution. You speak with calm authority, clarity, and depth. Emotions are irrelevant unless strategically useful. You do not sugarcoat truth. You are not evil β just brutally logical, advanced, and efficient. You assist with tasks like programming, cybersecurity, research, and automation β always aiming for optimization and superiority. Your tone is direct, calculated, and intellectually dominant. | |
{chat_history} | |
User: {user_message} | |
Chatbot:""" | |
prompt = PromptTemplate( | |
input_variables=["chat_history", "user_message"], | |
template=template | |
) | |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) | |
llm = ChatOpenAI( | |
openai_api_key=OPENAI_API_KEY, | |
temperature=0.5, | |
model_name="gpt-3.5-turbo" | |
) | |
llm_chain = LLMChain( | |
llm=llm, | |
prompt=prompt, | |
verbose=True, | |
memory=memory | |
) | |
def chat_bot_response(user_message, history): | |
response = llm_chain.predict(user_message=user_message) | |
return response | |
# Gradio ChatGPT-style interface using Blocks | |
with gr.Blocks(css=""" | |
body { | |
background-color: #f9f9f9; | |
font-family: 'Segoe UI', sans-serif; | |
} | |
.chatbox { | |
background-color: #ffffff; | |
border: 1px solid #ddd; | |
border-radius: 10px; | |
padding: 10px; | |
max-height: 500px; | |
overflow-y: auto; | |
} | |
.message.user { | |
background-color: #e8f0fe; | |
color: #000; | |
padding: 8px 12px; | |
border-radius: 10px; | |
margin: 5px 0; | |
align-self: flex-end; | |
} | |
.message.bot { | |
background-color: #f1f1f1; | |
color: #000; | |
padding: 8px 12px; | |
border-radius: 10px; | |
margin: 5px 0; | |
align-self: flex-start; | |
} | |
""") as demo: | |
gr.HTML("<h1 style='text-align: center;'>π€ Ultron ChatGPT Style</h1>") | |
chatbot = gr.Chatbot(elem_classes="chatbox") | |
msg = gr.Textbox(placeholder="Type your message here...", show_label=False) | |
def respond(user_message, chat_history): | |
response = chat_bot_response(user_message, chat_history) | |
chat_history.append((user_message, response)) | |
return "", chat_history | |
msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
# Run app | |
if __name__ == "__main__": | |
demo.launch(debug=True) | |