Spaces:
Runtime error
Runtime error
File size: 4,726 Bytes
563fce0 95dff6e 7d1162a 95dff6e 8c44598 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a 563fce0 7d1162a |
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 |
import streamlit as st
from g4f.client import Client
import sqlite3
import pyperclip
import os
# --- KONFIGURASI GROQ (via environment variable) ---
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Set di .env atau environment
if not GROQ_API_KEY:
st.error("GROQ_API_KEY tidak ditemukan! Tambahkan di environment atau .env")
st.stop()
conn = sqlite3.connect('chat_history.db', check_same_thread=False)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS chat_history
(conversation_id INTEGER, role TEXT, content TEXT)''')
conn.commit()
def local_css(file_name):
try:
with open(file_name) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
except FileNotFoundError:
st.warning("style.css tidak ditemukan. Mengabaikan custom CSS.")
local_css("style.css")
# --- MODEL LIST (via g4f + Groq) ---
models = {
"π€ Qwen 2.5 Coder (72B)": "qwen2.5-coder-32b",
"π LLaMA 3.1 70B": "llama-3.1-70b-versatile",
"β‘ LLaMA 3.1 8B": "llama-3.1-8b-instant",
"π§ Qwen 2.5 72B": "qwen2.5-72b-instruct",
"βοΈ Mixtral 8x7B": "mixtral-8x7b-32768",
}
client = Client(
provider="groq",
api_key=GROQ_API_KEY
)
def main():
st.set_page_config(page_title="DarkGPT", page_icon="π€", initial_sidebar_state="expanded")
# Inisialisasi session state
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "conversation_id" not in st.session_state:
st.session_state.conversation_id = 1
# Header + Model Selector
col1, col2, col3 = st.columns([1, 1, 1])
with col1:
st.header("DarkGPT")
with col3:
selected_model_name = st.selectbox("Pilih Model", list(models.keys()), index=0)
selected_model = models[selected_model_name]
# Sidebar: New Chat
if st.sidebar.button("β¨ New Chat"):
st.session_state.chat_history = []
st.session_state.conversation_id += 1
st.rerun()
# Sidebar: Load History
st.sidebar.write("### Chat History")
c.execute("SELECT DISTINCT conversation_id FROM chat_history ORDER BY conversation_id DESC")
for (conv_id,) in c.fetchall():
c.execute("SELECT content FROM chat_history WHERE conversation_id = ? AND role = 'bot' LIMIT 1", (conv_id,))
first_msg = c.fetchone()
if first_msg:
preview = " ".join(first_msg[0].split()[:6]) + "..."
if st.sidebar.button(preview, key=f"load_{conv_id}"):
load_conversation(conv_id)
# Sidebar: Clear All
if st.sidebar.button("ποΈ Clear All History"):
c.execute("DELETE FROM chat_history")
conn.commit()
st.session_state.chat_history = []
st.rerun()
# Chat Input
st.markdown("---")
if user_input := st.chat_input("Ketik pesan..."):
with st.chat_message("user"):
st.markdown(user_input)
# Simpan user message
st.session_state.chat_history.append({"role": "user", "content": user_input})
# Generate response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
try:
response = client.chat.completions.create(
model=selected_model,
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.chat_history],
temperature=0.7,
max_tokens=2048
)
bot_response = response.choices[0].message.content
st.markdown(bot_response)
# Simpan bot response
st.session_state.chat_history.append({"role": "assistant", "content": bot_response})
# Simpan ke DB
for msg in st.session_state.chat_history[-2:]: # hanya 2 pesan terakhir
c.execute(
"INSERT INTO chat_history VALUES (?, ?, ?)",
(st.session_state.conversation_id, msg["role"], msg["content"])
)
conn.commit()
except Exception as e:
st.error(f"Error: {str(e)}")
for msg in st.session_state.chat_history[:-2]:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
def load_conversation(conv_id):
c.execute("SELECT role, content FROM chat_history WHERE conversation_id = ? ORDER BY rowid", (conv_id,))
st.session_state.chat_history = [{"role": r, "content": c} for r, c in c.fetchall()]
st.session_state.conversation_id = conv_id
st.rerun()
if __name__ == "__main__":
main() |