Spaces:
Sleeping
Sleeping
File size: 8,101 Bytes
c150b51 a4d40ce c150b51 2275ef5 c150b51 2275ef5 c150b51 2275ef5 c150b51 2275ef5 c150b51 a4d40ce c150b51 a4d40ce c150b51 a4d40ce c150b51 05ae2f5 c150b51 a4d40ce c150b51 a4d40ce c150b51 a4d40ce c150b51 6d91d7c c150b51 6d91d7c c150b51 7227edd c150b51 a4d40ce c150b51 a4d40ce c150b51 a4d40ce c150b51 |
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 246 247 248 249 250 251 252 253 254 255 256 257 258 |
import streamlit as st
from datetime import datetime
import os
import json
from pathlib import Path
from json import JSONDecodeError
from FreeLLM import HuggingChatAPI, ChatGPTAPI, BingChatAPI
# Page Configuration
st.set_page_config(
page_title="FREE AUTOHUGGS π€",
page_icon="π€",
layout="wide",
menu_items={
"Get help": "[email protected]",
"About": "# *π€ FREE AUTOHUGGS π€*"
}
)
# Modern Dark Theme CSS
st.markdown("""
<style>
/* Base theme */
.main {
background-color: #0e1117;
color: #e0e0e0;
}
/* Sidebar styling */
.css-1d391kg {
background-color: #1e1e2e;
}
/* Input fields */
.stTextInput input, .stTextArea textarea, .stNumberInput input {
background-color: #262630;
color: #ffffff;
border: 1px solid #464660;
border-radius: 8px;
padding: 12px;
}
/* Buttons */
.stButton>button {
background: linear-gradient(90deg, #ff4b4b 0%, #ff6b6b 100%);
color: white;
border: none;
padding: 0.6em 1.2em;
border-radius: 8px;
font-weight: 600;
transition: all 0.3s ease;
}
.stButton>button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(255, 75, 75, 0.3);
}
/* Chat messages */
.chat-message {
padding: 1rem;
border-radius: 12px;
margin: 1rem 0;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.user-message {
background-color: #2a2a3a;
border-left: 4px solid #ff4b4b;
}
.assistant-message {
background-color: #323242;
border-left: 4px solid #4b8bff;
}
/* Headers */
h1, h2, h3 {
color: #ffffff;
}
</style>
""", unsafe_allow_html=True)
# Sidebar Configuration
with st.sidebar:
st.header("π οΈ Agent Configuration")
assistant_role_name = st.text_input("π€ Assistant Role", "Python Programmer")
user_role_name = st.text_input("π₯ User Role", "Stock Trader")
task = st.text_area("π Task", "Develop a trading bot for the stock market")
word_limit = st.slider("π€ Word Limit", 10, 1500, 50)
st.markdown("---")
st.header("βοΈ Advanced Options")
llm_choice = st.selectbox("π€ Select LLM API", ["HuggingChat", "ChatGPT", "BingChat"])
auto_save = st.checkbox("πΎ Auto-save conversation", value=True)
transcript_filename = st.text_input("π Transcript filename", "conversation_transcript.txt")
# Cookie File Validation
COOKIE_FILE = "cookiesHuggingChat.json"
try:
if not Path(COOKIE_FILE).exists():
st.error(f"β File '{COOKIE_FILE}' not found! Please create it with your cookies in JSON format.")
st.stop()
with open(COOKIE_FILE, "r") as file:
cookies = json.loads(file.read())
except JSONDecodeError:
st.error(f"β Invalid JSON in '{COOKIE_FILE}'. Please check your file format!")
st.stop()
except Exception as e:
st.error(f"β Error reading cookie file: {str(e)}")
st.stop()
# LLM API Initialization
def get_llm(api_choice: str):
llm_map = {
"HuggingChat": HuggingChatAPI.HuggingChat,
"ChatGPT": ChatGPTAPI.ChatGPT,
"BingChat": BingChatAPI.BingChat
}
if api_choice not in llm_map:
st.error("β Invalid LLM API selection.")
st.stop()
try:
return llm_map[api_choice](cookiepath=str(Path(COOKIE_FILE)))
except Exception as e:
st.error(f"β Error initializing {api_choice}: {str(e)}")
st.stop()
llm = get_llm(llm_choice)
# Initialize session state
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "conversation_start" not in st.session_state:
st.session_state.conversation_start = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Main chat interface
st.title("π¬ AI Chat Interface")
# Display chat history
for message in st.session_state.chat_history:
if message["role"] == "assistant":
st.markdown(
f"<div class='chat-message assistant-message'><strong>{assistant_role_name}:</strong> {message['content']}</div>",
unsafe_allow_html=True
)
elif message["role"] == "user":
st.markdown(
f"<div class='chat-message user-message'><strong>{user_role_name}:</strong> {message['content']}</div>",
unsafe_allow_html=True
)
else:
st.markdown(
f"<div class='chat-message'><em>{message['content']}</em></div>",
unsafe_allow_html=True
)
# Input area
user_input = st.text_input("π Your message:", key="user_input")
col1, col2, col3 = st.columns(3)
with col1:
if st.button("π€ Send"):
if user_input:
# Add user message to history
st.session_state.chat_history.append({"role": "user", "content": user_input})
# Get AI response
try:
with st.spinner("π€ AI is thinking..."):
response = llm(user_input)
st.session_state.chat_history.append({"role": "assistant", "content": response})
# Auto-save if enabled
if auto_save:
with open(transcript_filename, "a", encoding="utf-8") as f:
f.write(f":User {user_input}\n")
f.write(f"Assistant: {response}\n")
st.experimental_rerun()
except Exception as e:
st.error(f"β Error getting AI response: {str(e)}")
with col2:
if st.button("π Clear Chat"):
st.session_state.chat_history = []
st.experimental_rerun()
with col3:
if st.button("πΎ Save Transcript"):
try:
transcript = "\n".join([
f"{msg['role'].capitalize()}: {msg['content']}"
for msg in st.session_state.chat_history
])
with open(transcript_filename, "w", encoding="utf-8") as f:
f.write(transcript)
st.success("β
Transcript saved successfully!")
except Exception as e:
st.error(f"β Error saving transcript: {str(e)}")
# Additional Features
# Function to display the current time
def display_time():
current_time = datetime.now().strftime("%H:%M:%S")
st.sidebar.markdown(f"π Current Time: {current_time}")
# Call the function to display time
display_time()
# Function to handle user input and AI response
def handle_input(user_input):
if user_input:
st.session_state.chat_history.append({"role": "user", "content": user_input})
try:
with st.spinner("π€ AI is thinking..."):
response = llm(user_input)
st.session_state.chat_history.append({"role": "assistant", "content": response})
if auto_save:
with open(transcript_filename, "a", encoding="utf-8") as f:
f.write(f":User {user_input}\n")
f.write(f"Assistant: {response}\n")
st.experimental_rerun()
except Exception as e:
st.error(f"β Error getting AI response: {str(e)}")
# Input area with button to send message
if st.button("π€ Send"):
handle_input(user_input)
# Function to clear chat history
def clear_chat():
st.session_state.chat_history = []
st.experimental_rerun()
# Clear chat button
if st.button("π Clear Chat"):
clear_chat()
# Function to save transcript
def save_transcript():
try:
transcript = "\n".join([
f"{msg['role'].capitalize()}: {msg['content']}"
for msg in st.session_state.chat_history
])
with open(transcript_filename, "w", encoding="utf-8") as f:
f.write(transcript)
st.success("β
Transcript saved successfully!")
except Exception as e:
st.error(f"β Error saving transcript: {str(e)}")
# Save transcript button
if st.button("πΎ Save Transcript"):
save_transcript() |