Spaces:
Running
Running
File size: 2,995 Bytes
9ad9766 4cc2899 9ad9766 17430a5 4cc2899 9ad9766 8b5fa62 a965c7d 9ad9766 4cc2899 9ad9766 4cc2899 9ad9766 4cc2899 9ad9766 4cc2899 9ad9766 8b5fa62 4cc2899 8b5fa62 9ad9766 49a1567 8b5fa62 1663ed5 49a1567 17430a5 49a1567 1663ed5 49a1567 1663ed5 17430a5 49a1567 17430a5 49a1567 17430a5 1663ed5 17430a5 1663ed5 49a1567 1663ed5 8b5fa62 1663ed5 8b5fa62 1663ed5 49a1567 8b5fa62 49a1567 9ad9766 8b5fa62 9ad9766 49a1567 |
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 |
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.")
# Ultron 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
)
# Chatbot response handler
def chat_bot_response(user_message, history):
response = llm_chain.predict(user_message=user_message)
history.append((user_message, response))
return "", history
def clear_chat():
return "", []
def clear_history_fn():
memory.clear()
return "", []
# Gradio ChatGPT-like interface
with gr.Blocks(css="""
body {
background-color: #0f172a;
font-family: 'Segoe UI', sans-serif;
color: #e5e7eb;
}
.chatbox {
background-color: #1f2937;
border: 1px solid #334155;
border-radius: 12px;
padding: 10px;
max-height: 500px;
overflow-y: auto;
}
.message.user {
background-color: #2563eb;
color: #fff;
padding: 8px 12px;
border-radius: 10px;
margin: 5px 0;
align-self: flex-end;
}
.message.bot {
background-color: #374151;
color: #fff;
padding: 8px 12px;
border-radius: 10px;
margin: 5px 0;
align-self: flex-start;
}
button {
margin-top: 10px;
}
""") as demo:
gr.HTML("<h1 style='text-align: center;'>π€ Ultron - ChatGPT Style Assistant</h1>")
chatbot = gr.Chatbot(elem_classes="chatbox", value=[])
msg = gr.Textbox(placeholder="Ask Ultron anything...", show_label=False)
with gr.Row():
clear_btn = gr.Button("Clear Chat")
reset_memory_btn = gr.Button("Clear History")
msg.submit(chat_bot_response, inputs=[msg, chatbot], outputs=[msg, chatbot])
clear_btn.click(clear_chat, outputs=[msg, chatbot])
reset_memory_btn.click(clear_history_fn, outputs=[msg, chatbot])
# Run the app
if __name__ == "__main__":
demo.launch(debug=True) |